new(whatsapp.gblib): FB Analytics.
This commit is contained in:
parent
fe1f5d5150
commit
be3ba92429
1 changed files with 51 additions and 47 deletions
|
@ -1540,7 +1540,9 @@ private async sendButtonList(to: string, buttons: string[]) {
|
||||||
return buf;
|
return buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async getLatestCampaignReport() {
|
|
||||||
|
|
||||||
|
public async getLatestCampaignReportUsingGraphQL() {
|
||||||
const businessAccountId = this.whatsappBusinessManagerId;
|
const businessAccountId = this.whatsappBusinessManagerId;
|
||||||
const userAccessToken = this.whatsappServiceKey;
|
const userAccessToken = this.whatsappServiceKey;
|
||||||
|
|
||||||
|
@ -1549,66 +1551,68 @@ private async sendButtonList(to: string, buttons: string[]) {
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Step 1: Fetch templates with edit time ordering
|
// GraphQL query for templates and insights
|
||||||
const statsResponse = await fetch(
|
const query = `
|
||||||
`https://graph.facebook.com/v21.0/${businessAccountId}/message_templates?` +
|
query WhatsAppBusinessAccountManagerTemplateDetailsInsightsContainerQuery {
|
||||||
`fields=id,name,category,language,status,created_time,last_edited_time&` +
|
businessAccount(id: "${businessAccountId}") {
|
||||||
`ordering=[{last_edited_time: 'DESC'}]`, {
|
messageTemplates {
|
||||||
headers: { Authorization: `Bearer ${userAccessToken}` }
|
id
|
||||||
|
name
|
||||||
|
category
|
||||||
|
language
|
||||||
|
status
|
||||||
|
lastEditedTime
|
||||||
|
insights(startDate: "2025-02-01", endDate: "2025-02-16") {
|
||||||
|
sent
|
||||||
|
delivered
|
||||||
|
read
|
||||||
|
clicked
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`;
|
||||||
|
|
||||||
|
const response = await fetch('https://graph.facebook.com/graphql', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Authorization': `Bearer ${userAccessToken}`,
|
||||||
|
'Content-Type': 'application/json'
|
||||||
|
},
|
||||||
|
body: JSON.stringify({
|
||||||
|
query
|
||||||
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
const data = await statsResponse.json();
|
const result = await response.json();
|
||||||
if (!statsResponse.ok) {
|
|
||||||
throw new Error(data.error.message);
|
if (!response.ok || result.errors) {
|
||||||
|
throw new Error(result.errors ? result.errors[0].message : 'Error fetching data');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!data.data || data.data.length === 0) {
|
const templates = result.data.businessAccount.messageTemplates;
|
||||||
throw new Error('No template statistics found');
|
|
||||||
}
|
|
||||||
|
|
||||||
// Filter for marketing templates and get the latest edited one
|
// Filter for marketing templates
|
||||||
const marketingTemplates = data.data
|
const marketingTemplates = templates
|
||||||
.filter(template => template.category?.toUpperCase() === 'MARKETING')
|
.filter(template => template.category?.toUpperCase() === 'MARKETING')
|
||||||
.sort((a, b) => new Date(b.last_edited_time).getTime() - new Date(a.last_edited_time).getTime());
|
.sort((a, b) => new Date(b.lastEditedTime).getTime() - new Date(a.lastEditedTime).getTime());
|
||||||
|
|
||||||
if (marketingTemplates.length === 0) {
|
if (marketingTemplates.length === 0) {
|
||||||
return 'No marketing templates found.';
|
return 'No marketing templates found.';
|
||||||
}
|
}
|
||||||
|
|
||||||
const latestTemplate = marketingTemplates[0];
|
const latestTemplate = marketingTemplates[0];
|
||||||
const templateId = latestTemplate.id;
|
const insights = latestTemplate.insights || {};
|
||||||
|
const sent = insights.sent || 0;
|
||||||
// Step 2: Fetch template analytics
|
const delivered = insights.delivered || 0;
|
||||||
const startTime = Math.floor(Date.now() / 1000) - 604800; // Last 7 days
|
const read = insights.read || 0;
|
||||||
const endTime = Math.floor(Date.now() / 1000);
|
const clicked = insights.clicked || 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 readRate = delivered > 0 ? ((read / delivered) * 100).toFixed(2) : 0;
|
||||||
const clickRate = delivered > 0 ? ((clicked / 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.lastEditedTime
|
||||||
? new Date(latestTemplate.last_edited_time).toLocaleDateString('en-US', {
|
? new Date(latestTemplate.lastEditedTime).toLocaleDateString('en-US', {
|
||||||
month: 'short',
|
month: 'short',
|
||||||
day: '2-digit',
|
day: '2-digit',
|
||||||
year: 'numeric'
|
year: 'numeric'
|
||||||
|
@ -1623,10 +1627,9 @@ 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()})*
|
Message Click Rate: *${clickRate}% (${clicked.toLocaleString()})*
|
||||||
Top Block Reason: *${latestTemplate.rejection_reason || '––'}*
|
|
||||||
Last Edited: *${lastEditedDate}*`;
|
Last Edited: *${lastEditedDate}*`;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error fetching WhatsApp template statistics:', error.message);
|
console.error('Error fetching WhatsApp template statistics using GraphQL:', error.message);
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1634,4 +1637,5 @@ Last Edited: *${lastEditedDate}*`;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue