new(whatsapp.gblib): FB Analytics.
This commit is contained in:
parent
9978e35f33
commit
8a8884da64
1 changed files with 40 additions and 14 deletions
|
@ -1539,6 +1539,7 @@ private async sendButtonList(to: string, buttons: string[]) {
|
||||||
let buf: any = Buffer.from(await res.arrayBuffer());
|
let buf: any = Buffer.from(await res.arrayBuffer());
|
||||||
return buf;
|
return buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async getLatestCampaignReport() {
|
public async getLatestCampaignReport() {
|
||||||
const businessAccountId = this.whatsappBusinessManagerId;
|
const businessAccountId = this.whatsappBusinessManagerId;
|
||||||
const userAccessToken = this.whatsappServiceKey;
|
const userAccessToken = this.whatsappServiceKey;
|
||||||
|
@ -1550,13 +1551,10 @@ private async sendButtonList(to: string, buttons: string[]) {
|
||||||
try {
|
try {
|
||||||
// Step 1: Fetch templates with edit time ordering
|
// Step 1: Fetch templates with edit time ordering
|
||||||
const statsResponse = await fetch(
|
const statsResponse = await fetch(
|
||||||
`https://graph.facebook.com/v20.0/${businessAccountId}/message_templates?` +
|
`https://graph.facebook.com/v21.0/${businessAccountId}/message_templates?` +
|
||||||
`fields=id,name,category,language,status,created_time,last_edited_time,` +
|
`fields=id,name,category,language,status,created_time,last_edited_time&` +
|
||||||
`delivered_24h,read_24h,rejection_reason&` +
|
|
||||||
`ordering=[{last_edited_time: 'DESC'}]`, {
|
`ordering=[{last_edited_time: 'DESC'}]`, {
|
||||||
headers: {
|
headers: { Authorization: `Bearer ${userAccessToken}` }
|
||||||
Authorization: `Bearer ${userAccessToken}`
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const data = await statsResponse.json();
|
const data = await statsResponse.json();
|
||||||
|
@ -1577,13 +1575,36 @@ private async sendButtonList(to: string, buttons: string[]) {
|
||||||
return 'No marketing templates found.';
|
return 'No marketing templates found.';
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the most recently edited marketing template
|
|
||||||
const latestTemplate = marketingTemplates[0];
|
const latestTemplate = marketingTemplates[0];
|
||||||
|
const templateId = latestTemplate.id;
|
||||||
|
|
||||||
// Calculate metrics
|
// Step 2: Fetch template analytics
|
||||||
const delivered = latestTemplate.delivered_24h || 0;
|
const startTime = Math.floor(Date.now() / 1000) - 86400; // Last 24h
|
||||||
const read = latestTemplate.read_24h || 0;
|
const endTime = Math.floor(Date.now() / 1000);
|
||||||
const readRate = delivered > 0 ? ((read / delivered) * 100).toFixed(0) : 0;
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
// Format the date
|
||||||
const lastEditedDate = latestTemplate.last_edited_time
|
const lastEditedDate = latestTemplate.last_edited_time
|
||||||
|
@ -1594,20 +1615,25 @@ private async sendButtonList(to: string, buttons: string[]) {
|
||||||
})
|
})
|
||||||
: 'Not available';
|
: 'Not available';
|
||||||
|
|
||||||
// Format the response exactly as requested
|
|
||||||
return `Template Name: *${latestTemplate.name}*
|
return `Template Name: *${latestTemplate.name}*
|
||||||
Category: *${latestTemplate.category?.toUpperCase()}*
|
Category: *${latestTemplate.category?.toUpperCase()}*
|
||||||
Language: *${latestTemplate.language?.replace('-', '_').toUpperCase() || 'pt_BR'}*
|
Language: *${latestTemplate.language?.replace('-', '_').toUpperCase() || 'pt_BR'}*
|
||||||
Status: *${latestTemplate.status?.toUpperCase()}*
|
Status: *${latestTemplate.status?.toUpperCase()}*
|
||||||
|
Messages Sent: *${sent.toLocaleString()}*
|
||||||
Messages Delivered: *${delivered.toLocaleString()}*
|
Messages Delivered: *${delivered.toLocaleString()}*
|
||||||
Message Read Rate: *${readRate}% (${read.toLocaleString()})*
|
Message Read Rate: *${readRate}% (${read.toLocaleString()})*
|
||||||
|
Message Click Rate: *${clickRate}% (${clicked.toLocaleString()})*
|
||||||
Top Block Reason: *${latestTemplate.rejection_reason || '––'}*
|
Top Block Reason: *${latestTemplate.rejection_reason || '––'}*
|
||||||
Last Edited: *${lastEditedDate}*`.trim();
|
Last Edited: *${lastEditedDate}*`;
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error fetching WhatsApp template statistics:', error.message);
|
console.error('Error fetching WhatsApp template statistics:', error.message);
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue