new(whatsapp.gblib): FB Analytics.

This commit is contained in:
Rodrigo Rodriguez 2025-02-16 21:01:10 -03:00
parent 4521f6c342
commit beffc53463

View file

@ -1556,19 +1556,24 @@ private async sendButtonList(to: string, buttons: string[]) {
`access_token=${userAccessToken}` `access_token=${userAccessToken}`
); );
const data = await statsResponse.json(); let data = await statsResponse.json();
if (!statsResponse.ok) { if (!statsResponse.ok) {
throw new Error(data.error?.message || 'Failed to fetch templates'); throw new Error(data.error?.message || 'Failed to fetch templates');
} }
console.log(GBUtil.toYAML(data));
// Check if data.data is an array (templates are under "data" not "message_templates") data = data.message_templates.data;
if (!Array.isArray(data.data)) { // Check if message_templates is an array
console.error('Expected data to be an array, but got:', data.data); if (!Array.isArray(data)) {
console.error('Expected message_templates to be an array, but got:', data.message_templates);
return 'Invalid response format for message templates.'; return 'Invalid response format for message templates.';
} }
// Filter templates in the "data" array if (data.length === 0) {
const marketingTemplates = data.data throw new Error('No template statistics found');
}
// Filter for marketing templates and get the latest edited one
const marketingTemplates = data
.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.last_edited_time).getTime() - new Date(a.last_edited_time).getTime());
@ -1576,8 +1581,6 @@ private async sendButtonList(to: string, buttons: string[]) {
return 'No marketing templates found.'; return 'No marketing templates found.';
} }
// Log YAML formatted data
console.log(GBUtil.toYAML(marketingTemplates));
const latestTemplate = marketingTemplates[0]; const latestTemplate = marketingTemplates[0];
const templateId = latestTemplate.id; const templateId = latestTemplate.id;
@ -1592,13 +1595,11 @@ private async sendButtonList(to: string, buttons: string[]) {
); );
const analyticsData = await analyticsResponse.json(); const analyticsData = await analyticsResponse.json();
console.log(GBUtil.toYAML(analyticsData));
if (!analyticsResponse.ok) { if (!analyticsResponse.ok) {
throw new Error(analyticsData.error?.message || 'Failed to fetch analytics'); throw new Error(analyticsData.error?.message || 'Failed to fetch analytics');
} }
// Log YAML formatted analytics data
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) {
return 'No analytics data available for the specified template.'; return 'No analytics data available for the specified template.';