Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Creative performance Facebook Scripting Engine script
Logger.log('Starting script to generate creatives impressions performance');
let creatives_performance_map = {};
const account = AdAccount.myAccount();
const campaigns = account.getCampaigns()
.loadName()
.loadId()
.setEffectiveStatus(['ACTIVE'])
.execute();
//Logger.log('Total ' + campaigns.length + ' campaigns found.');
for (const campaign of campaigns) {
//Logger.log(' Campaign: ' + campaign.name + ' ' + campaign.id);
const adSets = campaign.getAdSets()
.loadName()
.loadId()
.setEffectiveStatus(['ACTIVE'])
.execute();
for (const adSet of adSets) {
//Logger.log(' Ad Set: ' + adSet.name + ' ' + adSet.id);
const ads = adSet.getAds()
.loadName()
.setEffectiveStatus(['ACTIVE'])
.execute();
for (const ad of ads) {
//Logger.log(' Ad: ' + ad.name + ' ' + ad.id);
const adcreatives = ad.getAdCreatives().execute();
const insights = ad.getInsights()
.loadField('impressions')
.setDatePreset('last_30d')
.execute();
var impressions = 0;
for (const value of insights) {
//Logger.log(value.impressions + ' impressions.');
impressions += value.impressions;
}
for (const creative of adcreatives){
//Logger.log(' Creative: ' + creative.name + ' ' + creative.id);
if (creative.id in creatives_performance_map){
creatives_performance_map[creative.id] += impressions;
} else {
creatives_performance_map[creative.id] = impressions;
}
}
}
}
}
// use this to print impressions on script logs
var message = JSON.stringify(creatives_performance_map);
Logger.log(message);
// use this to send impressions via email
//const EMAIL_API_URL='https://api.sendgrid.com/api/mail.send.json?api_user=<sendgrid_username>&api_key=<sendgrid_password>&to=<your_email_address>&toname=Name&subject=Insights&text=' + message + '&from=info@yourdomain.com';
//const method = 'POST';
//const response = URLFetcher.fetch(EMAIL_API_URL, {method});
//Logger.log(response.getContentText());
Logger.log('Script finished successfully');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment