new(whatsapp.gblib): FB Analytics.
This commit is contained in:
parent
ffd7b52ade
commit
9978e35f33
1 changed files with 34 additions and 33 deletions
|
@ -1544,7 +1544,7 @@ private async sendButtonList(to: string, buttons: string[]) {
|
||||||
const userAccessToken = this.whatsappServiceKey;
|
const userAccessToken = this.whatsappServiceKey;
|
||||||
|
|
||||||
if (!(businessAccountId && userAccessToken)) {
|
if (!(businessAccountId && userAccessToken)) {
|
||||||
return 'No statistics available for General Bots WhatsApp templates.';
|
return 'No statistics available for marketing templates.';
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
@ -1552,10 +1552,7 @@ private async sendButtonList(to: string, buttons: string[]) {
|
||||||
const statsResponse = await fetch(
|
const statsResponse = await fetch(
|
||||||
`https://graph.facebook.com/v20.0/${businessAccountId}/message_templates?` +
|
`https://graph.facebook.com/v20.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,` +
|
||||||
`message_sends_24h,message_sends_7d,message_sends_30d,` +
|
`delivered_24h,read_24h,rejection_reason&` +
|
||||||
`delivered_24h,delivered_7d,delivered_30d,` +
|
|
||||||
`read_24h,read_7d,read_30d,content,components,` +
|
|
||||||
`quality_score,rejection_reason&` +
|
|
||||||
`ordering=[{last_edited_time: 'DESC'}]`, {
|
`ordering=[{last_edited_time: 'DESC'}]`, {
|
||||||
headers: {
|
headers: {
|
||||||
Authorization: `Bearer ${userAccessToken}`
|
Authorization: `Bearer ${userAccessToken}`
|
||||||
|
@ -1571,36 +1568,41 @@ private async sendButtonList(to: string, buttons: string[]) {
|
||||||
throw new Error('No template statistics found');
|
throw new Error('No template statistics found');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Process all templates and format them
|
// Filter for marketing templates and get the latest edited one
|
||||||
const templateReports = data.data.map(template => {
|
const marketingTemplates = data.data
|
||||||
// Calculate delivery and read rates
|
.filter(template => template.category?.toUpperCase() === 'MARKETING')
|
||||||
const delivered = template.delivered_24h || 0;
|
.sort((a, b) => new Date(b.last_edited_time).getTime() - new Date(a.last_edited_time).getTime());
|
||||||
const read = template.read_24h || 0;
|
|
||||||
const readRate = delivered > 0 ? ((read / delivered) * 100).toFixed(0) : 0;
|
|
||||||
|
|
||||||
const lastEditedDate = new Date(template.last_edited_time || template.created_time);
|
if (marketingTemplates.length === 0) {
|
||||||
|
return 'No marketing templates found.';
|
||||||
|
}
|
||||||
|
|
||||||
return `
|
// Get the most recently edited marketing template
|
||||||
Template Name: **${template.name}**
|
const latestTemplate = marketingTemplates[0];
|
||||||
Category: **${template.category}**
|
|
||||||
Language: **${template.language}**
|
|
||||||
Status: **${template.status}**
|
|
||||||
Messages Delivered: **${delivered.toLocaleString()}**
|
|
||||||
Message Read Rate: **${readRate}% (${read.toLocaleString()})**
|
|
||||||
Top Block Reason: **${template.rejection_reason || '––'}**
|
|
||||||
Last Edited: **${lastEditedDate.toLocaleDateString('en-US', {
|
|
||||||
month: 'short',
|
|
||||||
day: '2-digit',
|
|
||||||
year: 'numeric'
|
|
||||||
})}**
|
|
||||||
${template.content ? `Content: ${template.content}` : ''}
|
|
||||||
-------------------`;
|
|
||||||
});
|
|
||||||
|
|
||||||
// Join all template reports with double line breaks
|
// Calculate metrics
|
||||||
return `General Bots WhatsApp Analytics Report
|
const delivered = latestTemplate.delivered_24h || 0;
|
||||||
============================================
|
const read = latestTemplate.read_24h || 0;
|
||||||
${templateReports.join('\n\n')}`.trim();
|
const readRate = delivered > 0 ? ((read / delivered) * 100).toFixed(0) : 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';
|
||||||
|
|
||||||
|
// Format the response exactly as requested
|
||||||
|
return `Template Name: *${latestTemplate.name}*
|
||||||
|
Category: *${latestTemplate.category?.toUpperCase()}*
|
||||||
|
Language: *${latestTemplate.language?.replace('-', '_').toUpperCase() || 'pt_BR'}*
|
||||||
|
Status: *${latestTemplate.status?.toUpperCase()}*
|
||||||
|
Messages Delivered: *${delivered.toLocaleString()}*
|
||||||
|
Message Read Rate: *${readRate}% (${read.toLocaleString()})*
|
||||||
|
Top Block Reason: *${latestTemplate.rejection_reason || '––'}*
|
||||||
|
Last Edited: *${lastEditedDate}*`.trim();
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error fetching WhatsApp template statistics:', error.message);
|
console.error('Error fetching WhatsApp template statistics:', error.message);
|
||||||
|
@ -1608,5 +1610,4 @@ ${templateReports.join('\n\n')}`.trim();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue