Last active
June 29, 2019 04:51
-
-
Save vishwarajanand/8e5cb8e291ee91309b48f508e9f40628 to your computer and use it in GitHub Desktop.
Creative performance Facebook Scripting Engine script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 + '&[email protected]'; | |
//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