new(whatsapp.gblib): FB Analytics.
This commit is contained in:
parent
5183a2bdca
commit
091c9825bf
1 changed files with 25 additions and 12 deletions
|
@ -1542,7 +1542,7 @@ private async sendButtonList(to: string, buttons: string[]) {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public async getLatestCampaignReport() {
|
public async getLatestCampaignReportUsingGraphQL() {
|
||||||
const businessAccountId = this.whatsappBusinessManagerId;
|
const businessAccountId = this.whatsappBusinessManagerId;
|
||||||
const userAccessToken = this.whatsappServiceKey;
|
const userAccessToken = this.whatsappServiceKey;
|
||||||
|
|
||||||
|
@ -1551,7 +1551,7 @@ private async sendButtonList(to: string, buttons: string[]) {
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// GraphQL query for templates and insights
|
// GraphQL query without any date filters
|
||||||
const query = `
|
const query = `
|
||||||
query WhatsAppBusinessAccountManagerTemplateDetailsInsightsContainerQuery {
|
query WhatsAppBusinessAccountManagerTemplateDetailsInsightsContainerQuery {
|
||||||
businessAccount(id: "${businessAccountId}") {
|
businessAccount(id: "${businessAccountId}") {
|
||||||
|
@ -1562,7 +1562,7 @@ private async sendButtonList(to: string, buttons: string[]) {
|
||||||
language
|
language
|
||||||
status
|
status
|
||||||
lastEditedTime
|
lastEditedTime
|
||||||
insights(startDate: "2025-02-01", endDate: "2025-02-16") {
|
insights { // No date filters here
|
||||||
sent
|
sent
|
||||||
delivered
|
delivered
|
||||||
read
|
read
|
||||||
|
@ -1573,26 +1573,38 @@ private async sendButtonList(to: string, buttons: string[]) {
|
||||||
}
|
}
|
||||||
`;
|
`;
|
||||||
|
|
||||||
const response = await fetch('https://graph.facebook.com/graphql', {
|
// Perform the fetch request with improved error handling
|
||||||
|
const response = await fetch('https://graph.facebook.com/v12.0/graphql', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
'Authorization': `Bearer ${userAccessToken}`,
|
'Authorization': `Bearer ${userAccessToken}`,
|
||||||
'Content-Type': 'application/json'
|
'Content-Type': 'application/json'
|
||||||
},
|
},
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({ query })
|
||||||
query
|
|
||||||
})
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Log response status and raw response data for debugging
|
||||||
|
console.log('Response Status:', response.status);
|
||||||
const result = await response.json();
|
const result = await response.json();
|
||||||
|
console.log('Raw Response:', result);
|
||||||
|
|
||||||
if (!response.ok || result.errors) {
|
if (!response.ok) {
|
||||||
|
// If response is not OK, throw an error
|
||||||
throw new Error(result.errors ? result.errors[0].message : 'Error fetching data');
|
throw new Error(result.errors ? result.errors[0].message : 'Error fetching data');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check if there are data or errors in the response
|
||||||
|
if (result.errors) {
|
||||||
|
throw new Error(result.errors.map(err => err.message).join(', '));
|
||||||
|
}
|
||||||
|
|
||||||
const templates = result.data.businessAccount.messageTemplates;
|
const templates = result.data.businessAccount.messageTemplates;
|
||||||
|
|
||||||
// Filter for marketing templates
|
if (!templates || templates.length === 0) {
|
||||||
|
return 'No marketing templates found.';
|
||||||
|
}
|
||||||
|
|
||||||
|
// Filter for marketing templates and sort by last edited time
|
||||||
const marketingTemplates = templates
|
const marketingTemplates = templates
|
||||||
.filter(template => template.category?.toUpperCase() === 'MARKETING')
|
.filter(template => template.category?.toUpperCase() === 'MARKETING')
|
||||||
.sort((a, b) => new Date(b.lastEditedTime).getTime() - new Date(a.lastEditedTime).getTime());
|
.sort((a, b) => new Date(b.lastEditedTime).getTime() - new Date(a.lastEditedTime).getTime());
|
||||||
|
@ -1629,6 +1641,7 @@ Message Read Rate: *${readRate}% (${read.toLocaleString()})*
|
||||||
Message Click Rate: *${clickRate}% (${clicked.toLocaleString()})*
|
Message Click Rate: *${clickRate}% (${clicked.toLocaleString()})*
|
||||||
Last Edited: *${lastEditedDate}*`;
|
Last Edited: *${lastEditedDate}*`;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
// Log and throw the error with the message for debugging
|
||||||
console.error('Error fetching WhatsApp template statistics using GraphQL:', error.message);
|
console.error('Error fetching WhatsApp template statistics using GraphQL:', error.message);
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue