AdWords
1.9K members online now
1.9K members online now
Dive into advanced features like Remarketing, Flexible Bid Strategies, AdWords Editor, and AdWords Scripts
Guide Me
star_border
Reply

Total monthly ad spend generated by AdWords Script incorrect

Visitor ✭ ✭ ✭
# 1
Visitor ✭ ✭ ✭

I've used a script that sends an e-mail to me when the total monthly ad spend reaches a certain amount e.g. 2.500 EUR.

When I check the log files of the script, I see there's a difference between the reported total monthly ad spend by the script and the actual monthly ad spend in the interface. Can someone check what could be wrong with the script ?

 

function main() {

var maxBudget = 2500; //Enter your monthly budget here
var emailAddress = "pieter@websauce.be" //Enter the email address you wish to send notifications to


// Get all campaigns with at least 1 impression last month.
var campaignsIterator = AdWordsApp.campaigns()
  .withCondition("Impressions >= 1")
  .forDateRange("THIS_MONTH")
  .get();

var currentMonthSpend = 0.0;

// Sums total campaign spend & Logs 
while (campaignsIterator.hasNext()) {
var campaign = campaignsIterator.next();
var stats = campaign.getStatsFor("THIS_MONTH");
currentMonthSpend = currentMonthSpend += stats.getCost();

}

// Send mail Campaigns if monthly budget reached or exceeded & Logs changes 
if ( currentMonthSpend >= maxBudget){

MailApp.sendEmail(emailAddress, "Adwords Script Alert: Maandelijks budget overschreden", "Totale kosten overschrijdt" 
   + " maandelijks budget en is nu €" + currentMonthSpend.toFixed(2));   

}

else {

Logger.log("No Changes, Current Month Spend is " + currentMonthSpend.toFixed(2));
}

}

 

1 Expert replyverified_user

Re: Total monthly ad spend generated by AdWords Script incorrect

Top Contributor
# 2
Top Contributor

Hi @Pieter De Weirt there's two likely reasons (I can't see any obvious problem with the code itself).

 

The first is time range.  Although (presumably) you're also looking at "This Month", the script will be running at another time and if you're spending all day, it's quite possible that when the script ran the spend was X and when you looked in the UI some more clicks had accrued and the spend was Y.  How great is the difference in reported values?


The second (possibly more likely) is that you're not looking at the same set of Campaigns.  The selector in this script will report stats for all Campaigns not including Shopping, that have at least one impression.  So, firstly, check you're not looking at Shopping Campaigns in the UI, as these won't be picked up by this script, secondly, if you've got any Campaigns that were paused earlier in the month, they'd will be picked up by this script but (depending on your view settings) may not be shown in the UI.

 

To debug, a good idea would be to add a log line for each Campaign so you can check the figures for each one, and see which Campaigns are actually being included.  Just add:

 

Logger.log("Campaign: " + campaign.getName() + ", Spend: " + stats.getCost());

after the line beginning "currentMonthSpend =" and you may find it easier to see what's going on.

 

Jon

 

 

 

AdWords Top Contributor Google+ Profile | Partner Profile | AdWords Audits

Re: Total monthly ad spend generated by AdWords Script incorrect

Visitor ✭ ✭ ✭
# 3
Visitor ✭ ✭ ✭
Hi @Jon_Gritton

Thanks for checking it!
I've checked the time range and this is this month in the AdWords interface (the script runs daily at 9 AM).

By adding the extra line of code you suggested, I was able to figure out that it was a YouTube campaign that wasn't included in the current month spend (although there was for a Display campaign a difference of 0,70 EUR which is probably due to a click in the meantime).

Any idea to make sure all campaigns (meaning the YouTube campaign too) are included for the calculation of the current month spend by the script ?

Thanks,

Pieter

Re: Total monthly ad spend generated by AdWords Script incorrect

Top Contributor
# 4
Top Contributor

Hi @Pieter De Weirt I presume this is an AdWords for Video Campaign rather than a Display Campaign showing Ads on youtube.com? (Display Campaigns should be included in the selector you've already got).


If so, it's a little more complex, unfortunately, as (for now) you'll need to use an API report to get the stats for Video Campaigns.  Add this function at the bottom of your script (after all other code):

function get_ytCosts(during) {
  var report = AdWordsApp.report(
      "SELECT CampaignName, Cost FROM VIDEO_PERFORMANCE_REPORT WHERE Impressions > 1 DURING " + during);
  return report.rows();
}

then add these lines of code directly after the new Logger line you've just added:

var ytCost = 0;
var ytCosts = get_ytCosts("THIS_MONTH");
while(ytCosts.hasNext()) {
  var thisRow = ytCosts.next();
  var ytCost += thisRow['Cost'];
  Logger.log("Campaign: " + thisRow['CampaignName'] + ", Spend: " + thisRow['Cost']);
}
currentMonthSpend += ytCost;

Preview this script and see if it now adds in your video Campaign(s).

 

Jon

 

 

 

 

 

 

AdWords Top Contributor Google+ Profile | Partner Profile | AdWords Audits

Re: Total monthly ad spend generated by AdWords Script incorrect

Visitor ✭ ✭ ✭
# 5
Visitor ✭ ✭ ✭

Hi @Jon_Gritton

 

I've the following code below now. AdWords says there's a ; missing just before var ytCost += thisRow['Cost']; which looks incorrect to me.

Because of this I haven't been able to save it yet to try it. Could have a look at it please ?

 

function main() {

var maxBudget = 2500; //Enter your monthly budget here
var emailAddress = "pieter@websauce.be" //Enter the email address you wish to send notifications to

// Get all campaigns with at least 1 impression last month.
var campaignsIterator = AdWordsApp.campaigns()
  .withCondition("Impressions >= 1")
  .forDateRange("THIS_MONTH")
  .get();

var currentMonthSpend = 0.0;

// Sums total campaign spend & Logs 
while (campaignsIterator.hasNext()) {
var campaign = campaignsIterator.next();
var stats = campaign.getStatsFor("THIS_MONTH");
currentMonthSpend = currentMonthSpend += stats.getCost();
Logger.log("Campaign: " + campaign.getName() + ", Spend: " + stats.getCost());  
var ytCost = 0;
var ytCosts = get_ytCosts("THIS_MONTH");
while(ytCosts.hasNext()) {
  var thisRow = ytCosts.next();
  var ytCost += thisRow['Cost'];
Logger.log("Campaign: " + thisRow['CampaignName'] + ", Spend: " + thisRow['Cost']);
}
currentMonthSpend += ytCost;
}

// Send mail Campaigns if monthly budget reached or exceeded & Logs changes 
if ( currentMonthSpend >= maxBudget){

MailApp.sendEmail(emailAddress, "Adwords Script Alert: Maandelijks budget overschreden", "Totale kosten overschrijdt" 
   + " maandelijks budget en is nu €" + currentMonthSpend.toFixed(2));   

}

else {

Logger.log("No Changes, Current Month Spend is " + currentMonthSpend.toFixed(2));
}

}
function get_ytCosts(during) {
  var report = AdWordsApp.report(
      "SELECT CampaignName, Cost FROM VIDEO_PERFORMANCE_REPORT WHERE Impressions > 1 DURING " + during);
  return report.rows();
}

Re: Total monthly ad spend generated by AdWords Script incorrect

Top Contributor
# 6
Top Contributor

Sorry @Pieter De Weirt, my fault, I made a last minute edit directly here without testing (slap on wrist).


You need to take out the "var" before the line:

 

var ytCost += thisRow['Cost'];

I've already defined ytCost earlier in the script, which is why it gives this rather unhelpful error message.

 

Jon 

AdWords Top Contributor Google+ Profile | Partner Profile | AdWords Audits

Re: Total monthly ad spend generated by AdWords Script incorrect

Visitor ✭ ✭ ✭
# 7
Visitor ✭ ✭ ✭

Hi @Jon_Gritton

 

The script is now saved, thanks. When running in preview mode I get a message

 

TypeError: Cannot find function toFixed in object 337.68084.317.71084.3198.2084.31351.29084.31197.21084.3111.37084.3133.17084.3154.31084.31109.28084.3167.67084.3172.46084.3158.05084.3186.62084.315.78084.31. (line 41)

 

It refers to this line:

Logger.log("No Changes, Current Month Spend is " + currentMonthSpend.toFixed(2));

Could it be because currentMonthSpend.toFixed(2) hasn't been defined before in the script ?

I'm not such a script wizard Smiley Wink

 

Thanks!

 

Pieter

 

Re: Total monthly ad spend generated by AdWords Script incorrect

Top Contributor
# 8
Top Contributor

Hi @Pieter De Weirt sorry, this is my fault again (although I am partially going to blame AdWords for a silly system).  Results for numerical figures from the API report can sometimes be unexpected types, and cost is one of them.  The value for "cost" returned from a report is a string rather than a number, so in the line:

 

ytCosts += thisRow['cost']

 

I'm actually creating a long string, not a total (number).  Replace the line so this bit reads as follows:

ytCost += parseFloat(thisRow['Cost']);

and that should solve your problem.  I'm hampered a little because I don't currently have access to any Campaigns running AdWords for Video.

 

Jon

 

 

 

AdWords Top Contributor Google+ Profile | Partner Profile | AdWords Audits

Re: Total monthly ad spend generated by AdWords Script incorrect

Visitor ✭ ✭ ✭
# 9
Visitor ✭ ✭ ✭
Hi Jon_Gritton

I guess we're almost there. When having a looking at the log files, I see that the script counts the ad spend of the AdWords for Video campaign every time it checks the monthly ad spend of a campaign. This means that for 15 campaigns (the AdWords for Video campaign included) 13 times extra the monthly ad spend of the AdWords for Video campaign is added.
It looks like the script doesn't limit itself in adding that specific campaign.

Pieter

Re: Total monthly ad spend generated by AdWords Script incorrect

Top Contributor
# 10
Top Contributor

Hi @Pieter De Weirt yep, it will, but it's an easy fix.  The problem is that the code to check the YouTube Campaign(s) is inside the while loop for checking the "ordinary" Campaigns.  The fix requires only that you move a single closing }.

 

Around lines 28/29 you should see:

currentMonthSpend += ytCost;
}

remove the } and insert it between these two lines (around lines 19/20)

Logger.log("Campaign: " + campaign.getName() + ", Spend: " + stats.getCost());  
var ytCost = 0;

So they look like this:

Logger.log("Campaign: " + campaign.getName() + ", Spend: " + stats.getCost());  
}
var ytCost = 0;

That should do you.


Sorry this has been a rather piecemeal fix; I should have been more careful from the start.

 

Jon

AdWords Top Contributor Google+ Profile | Partner Profile | AdWords Audits