new(whatsapp.gblib): FB Analytics.
This commit is contained in:
parent
0f2f369a2b
commit
23ed6f8932
1 changed files with 10 additions and 8 deletions
|
@ -1539,7 +1539,6 @@ 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;
|
||||||
|
@ -1553,12 +1552,12 @@ private async sendButtonList(to: string, buttons: string[]) {
|
||||||
const statsResponse = await fetch(
|
const statsResponse = await fetch(
|
||||||
`https://graph.facebook.com/v21.0/${businessAccountId}?` +
|
`https://graph.facebook.com/v21.0/${businessAccountId}?` +
|
||||||
`fields=message_templates{id,name,category,language,status,created_time,last_edited_time}&` +
|
`fields=message_templates{id,name,category,language,status,created_time,last_edited_time}&` +
|
||||||
`access_token=${userAccessToken}`,
|
`access_token=${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 templates');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!data.message_templates || data.message_templates.length === 0) {
|
if (!data.message_templates || data.message_templates.length === 0) {
|
||||||
|
@ -1573,6 +1572,7 @@ private async sendButtonList(to: string, buttons: string[]) {
|
||||||
if (marketingTemplates.length === 0) {
|
if (marketingTemplates.length === 0) {
|
||||||
return 'No marketing templates found.';
|
return 'No marketing templates found.';
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(GBUtil.toYAML(marketingTemplates));
|
console.log(GBUtil.toYAML(marketingTemplates));
|
||||||
const latestTemplate = marketingTemplates[0];
|
const latestTemplate = marketingTemplates[0];
|
||||||
const templateId = latestTemplate.id;
|
const templateId = latestTemplate.id;
|
||||||
|
@ -1584,13 +1584,14 @@ private async sendButtonList(to: string, buttons: string[]) {
|
||||||
const analyticsResponse = await fetch(
|
const analyticsResponse = await fetch(
|
||||||
`https://graph.facebook.com/v21.0/${businessAccountId}?` +
|
`https://graph.facebook.com/v21.0/${businessAccountId}?` +
|
||||||
`fields=template_analytics.start(${startTime}).end(${endTime}).granularity(DAY).metric_types(sent,delivered,read,clicked).template_ids([${templateId}])&` +
|
`fields=template_analytics.start(${startTime}).end(${endTime}).granularity(DAY).metric_types(sent,delivered,read,clicked).template_ids([${templateId}])&` +
|
||||||
`access_token=${userAccessToken}`,
|
`access_token=${userAccessToken}`
|
||||||
);
|
);
|
||||||
|
|
||||||
const analyticsData = await analyticsResponse.json();
|
const analyticsData = await analyticsResponse.json();
|
||||||
if (!analyticsResponse.ok) {
|
if (!analyticsResponse.ok) {
|
||||||
throw new Error(analyticsData.error.message);
|
throw new Error(analyticsData.error?.message || 'Failed to fetch analytics');
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(GBUtil.toYAML(analyticsData));
|
console.log(GBUtil.toYAML(analyticsData));
|
||||||
const dataPoints = analyticsData.template_analytics?.data[0]?.data_points || [];
|
const dataPoints = analyticsData.template_analytics?.data[0]?.data_points || [];
|
||||||
if (dataPoints.length === 0) {
|
if (dataPoints.length === 0) {
|
||||||
|
@ -1604,9 +1605,9 @@ private async sendButtonList(to: string, buttons: string[]) {
|
||||||
const clicked = latestDataPoint.clicked?.reduce((acc, item) => acc + item.count, 0) || 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.last_edited_time
|
||||||
? new Date(latestTemplate.last_edited_time).toLocaleDateString('en-US', {
|
? new Date(latestTemplate.last_edited_time).toLocaleDateString('en-US', {
|
||||||
month: 'short',
|
month: 'short',
|
||||||
day: '2-digit',
|
day: '2-digit',
|
||||||
|
@ -1624,9 +1625,10 @@ Message Read Rate: *${readRate}% (${read.toLocaleString()})*
|
||||||
Message Click Rate: *${clickRate}% (${clicked.toLocaleString()})*
|
Message Click Rate: *${clickRate}% (${clicked.toLocaleString()})*
|
||||||
Top Block Reason: *${latestTemplate.rejection_reason || '––'}*
|
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:', error.message);
|
||||||
throw error;
|
return `Error fetching statistics: ${error.message}`;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue