new(whatsapp.gblib): FB Analytics.
This commit is contained in:
parent
b8a9740792
commit
b9db5840c9
1 changed files with 23 additions and 73 deletions
|
@ -1541,99 +1541,49 @@ private async sendButtonList(to: string, buttons: string[]) {
|
||||||
}
|
}
|
||||||
|
|
||||||
public async getLatestCampaignReport() {
|
public async getLatestCampaignReport() {
|
||||||
const businessAccountId = this.whatsappBusinessManagerId;
|
const phoneNumberId = this.whatsappServiceNumber;
|
||||||
const userAccessToken = this.whatsappServiceKey;
|
const userAccessToken = this.whatsappServiceKey;
|
||||||
|
|
||||||
if (!(businessAccountId && userAccessToken)) {
|
if (!(phoneNumberId && userAccessToken)) {
|
||||||
return 'No statistics available for marketing templates.';
|
return 'No statistics available for marketing templates.';
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Step 1: Fetch templates with edit time ordering
|
// Step 1: Fetch analytics from the phone number
|
||||||
const statsResponse = await fetch(
|
const statsResponse = await fetch(
|
||||||
`https://graph.facebook.com/v21.0/${businessAccountId}/message_templates?` +
|
`https://graph.facebook.com/v20.0/${phoneNumberId}/analytics_message_report?metric=['sent','delivered','read','clicked']`, {
|
||||||
`fields=id,name,category,language,status,created_time,last_edited_time&` +
|
headers: {
|
||||||
`ordering=[{last_edited_time: 'DESC'}]`, {
|
Authorization: `Bearer ${userAccessToken}`
|
||||||
headers: { Authorization: `Bearer ${userAccessToken}` }
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
const data = await statsResponse.json();
|
const data = await statsResponse.json();
|
||||||
if (!statsResponse.ok) {
|
if (!statsResponse.ok) {
|
||||||
throw new Error(data.error.message);
|
throw new Error(data.error?.message || 'Failed to fetch analytics.');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!data.data || data.data.length === 0) {
|
// Extract message statistics
|
||||||
throw new Error('No template statistics found');
|
const sent = data.sent?.value || 0;
|
||||||
}
|
const delivered = data.delivered?.value || 0;
|
||||||
|
const read = data.read?.value || 0;
|
||||||
|
const clicked = data.clicked?.value || 0;
|
||||||
|
const readRate = delivered > 0 ? ((read / delivered) * 100).toFixed(0) : 0;
|
||||||
|
|
||||||
// Filter for marketing templates and get the latest edited one
|
// Format response
|
||||||
const marketingTemplates = data.data
|
return `📊 *WhatsApp Campaign Analytics*
|
||||||
.filter(template => template.category?.toUpperCase() === 'MARKETING')
|
📨 Sent: *${sent.toLocaleString()}*
|
||||||
.sort((a, b) => new Date(b.last_edited_time).getTime() - new Date(a.last_edited_time).getTime());
|
✅ Delivered: *${delivered.toLocaleString()}*
|
||||||
|
👀 Read: *${read.toLocaleString()}*
|
||||||
|
📩 Clicked: *${clicked.toLocaleString()}*
|
||||||
|
📊 Read Rate: *${readRate}%*`.trim();
|
||||||
|
|
||||||
if (marketingTemplates.length === 0) {
|
|
||||||
return 'No marketing templates found.';
|
|
||||||
}
|
|
||||||
console.log(GBUtil.toYAML(marketingTemplates));
|
|
||||||
const latestTemplate = marketingTemplates[0];
|
|
||||||
const templateId = latestTemplate.id;
|
|
||||||
|
|
||||||
// Step 2: Fetch template analytics
|
|
||||||
const startTime = Math.floor(Date.now() / 1000) - 86400; // Last 24h
|
|
||||||
const endTime = Math.floor(Date.now() / 1000);
|
|
||||||
|
|
||||||
const analyticsResponse = await fetch(
|
|
||||||
`https://graph.facebook.com/v21.0/${businessAccountId}/template_analytics?` +
|
|
||||||
`start=${startTime}&end=${endTime}&granularity=daily&metric_types=sent,delivered,read,clicked&template_ids=[${templateId}]`, {
|
|
||||||
headers: { Authorization: `Bearer ${userAccessToken}` }
|
|
||||||
});
|
|
||||||
|
|
||||||
const analyticsData = await analyticsResponse.json();
|
|
||||||
if (!analyticsResponse.ok) {
|
|
||||||
throw new Error(analyticsData.error.message);
|
|
||||||
}
|
|
||||||
console.log(GBUtil.toYAML(analyticsData));
|
|
||||||
const dataPoints = analyticsData.data[0]?.data_points || [];
|
|
||||||
if (dataPoints.length === 0) {
|
|
||||||
return 'No analytics data available for the specified template.';
|
|
||||||
}
|
|
||||||
|
|
||||||
const latestDataPoint = dataPoints[dataPoints.length - 1];
|
|
||||||
const sent = latestDataPoint.sent || 0;
|
|
||||||
const delivered = latestDataPoint.delivered || 0;
|
|
||||||
const read = latestDataPoint.read || 0;
|
|
||||||
const clicked = latestDataPoint.clicked?.reduce((acc, item) => acc + item.count, 0) || 0;
|
|
||||||
const readRate = delivered > 0 ? ((read / delivered) * 100).toFixed(2) : 0;
|
|
||||||
const clickRate = delivered > 0 ? ((clicked / delivered) * 100).toFixed(2) : 0;
|
|
||||||
|
|
||||||
// Format the date
|
|
||||||
const lastEditedDate = latestTemplate.last_edited_time
|
|
||||||
? new Date(latestTemplate.last_edited_time).toLocaleDateString('en-US', {
|
|
||||||
month: 'short',
|
|
||||||
day: '2-digit',
|
|
||||||
year: 'numeric'
|
|
||||||
})
|
|
||||||
: 'Not available';
|
|
||||||
|
|
||||||
return `Template Name: *${latestTemplate.name}*
|
|
||||||
Category: *${latestTemplate.category?.toUpperCase()}*
|
|
||||||
Language: *${latestTemplate.language?.replace('-', '_').toUpperCase() || 'pt_BR'}*
|
|
||||||
Status: *${latestTemplate.status?.toUpperCase()}*
|
|
||||||
Messages Sent: *${sent.toLocaleString()}*
|
|
||||||
Messages Delivered: *${delivered.toLocaleString()}*
|
|
||||||
Message Read Rate: *${readRate}% (${read.toLocaleString()})*
|
|
||||||
Message Click Rate: *${clickRate}% (${clicked.toLocaleString()})*
|
|
||||||
Top Block Reason: *${latestTemplate.rejection_reason || '––'}*
|
|
||||||
Last Edited: *${lastEditedDate}*`;
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error fetching WhatsApp template statistics:', error.message);
|
console.error('Error fetching WhatsApp campaign report:', error.message);
|
||||||
throw error;
|
return 'Error fetching campaign report.';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue