ga4-telegram-toolkit
v1.0.6
Published
Thư viện hỗ trợ tương tác với Google Analytics 4 và Telegram API
Downloads
15
Maintainers
Readme
GA4 Telegram Toolkit
Thư viện hỗ trợ tương tác với Google Analytics 4 và Telegram API.
Cài đặt
npm install ga4-telegram-toolkitSử dụng
Import thư viện
// Import đầy đủ
import { GA4Service, TelegramService, GAInsightsService } from 'ga4-telegram-toolkit';
// Hoặc import từng module riêng biệt
import { GA4Service } from 'ga4-telegram-toolkit';
import { TelegramService } from 'ga4-telegram-toolkit';Kết nối Google Analytics 4
// Khởi tạo GA4Service với Property ID
const ga4PropertyId = process.env.GA4_PROPERTY_ID || '489280622';
const ga4Service = new GA4Service(ga4PropertyId, 'MyWebsite');
// Lấy báo cáo người dùng theo quốc gia
const usersByCountry = await ga4Service.getUsersByCountry();
// Lấy phiên theo thiết bị
const deviceSessions = await ga4Service.getSessionsByDeviceCategory('yesterday', 'yesterday');
// Lấy trang phổ biến nhất (7 ngày qua)
const topPages = await ga4Service.getPopularPagesWithEngagement('7daysAgo', 'yesterday', 5);
// So sánh lưu lượng hôm nay và hôm qua
const trafficComparison = await ga4Service.compareTodayVsYesterday('sessions');Gửi tin nhắn qua Telegram
// Khởi tạo TelegramService (cần cấu hình biến môi trường)
// TELEGRAM_BOT_TOKEN=your_bot_token
// TELEGRAM_CHAT_ID=your_chat_id
const telegramService = new TelegramService();
// Gửi tin nhắn đơn giản
await telegramService.sendMessage('Hello from GA4 Telegram Toolkit!');
// Gửi tin nhắn với định dạng HTML
await telegramService.sendMessage('<b>Báo cáo GA4</b>\n<i>Thông tin quan trọng</i>', 'HTML');
// Kiểm tra kết nối
const isConnected = await telegramService.testConnection();Tạo báo cáo GA4 và gửi qua Telegram
// Khởi tạo service tích hợp
const gaInsightsService = new GAInsightsService();
// Gửi báo cáo GA4 hàng ngày
await gaInsightsService.sendDailyGAInsights();
// Kiểm tra kết nối
await gaInsightsService.testConnection();Sử dụng Proxy SOCKS5 (tùy chọn)
// Cấu hình biến môi trường để sử dụng proxy
// USE_PROXY=true
// SOCKS5_PROXY_URL=socks5://user:pass@host:portUsage with Service Account Object
You can now initialize GA4Service with a service account object (only dynamic fields required):
import { GA4Service } from './src/ga.service';
const serviceAccountObj = {
project_id: 'lynx-460617',
private_key_id: 'xxxx',
private_key: '-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----\n',
client_email: '[email protected]',
client_id: 'xxxx',
client_x509_cert_url: 'https://www.googleapis.com/robot/v1/metadata/x509/ga4-xxx%40lynx-460617.iam.gserviceaccount.com'
};
const ga4Service = new GA4Service(
'YOUR_GA4_PROPERTY_ID',
'GA4Service',
undefined, // keyFilePath
false, // debug
serviceAccountObj // pass the object here
);If you use a file path, the old way still works and the fixed fields will be auto-merged if missing.
Cấu hình môi trường
Tạo file .env với nội dung:
GA4_PROPERTY_ID=your_property_id
TELEGRAM_BOT_TOKEN=your_bot_token
TELEGRAM_CHAT_ID=your_chat_id
USE_PROXY=false
SOCKS5_PROXY_URL=Links
License
MIT
