The code run and don't stop

153 views
Skip to first unread message

Ramarosoa Faneva

unread,
Dec 17, 2012, 5:08:06 PM12/17/12
to adwords...@googlegroups.com

Hi,

Yes, it's again a problem of code by a nwebbie JS man :) Someone can help please ?!

I create this code to extract all keyword who had a CPA > 5 but the script run and never stop....

function main() {
  var keywordsIterator = AdWordsApp.keywords()
  .get();
  
  var conversions = 0;
  var cost = 0;
  var cpa = 0;
  
  Logger.log("Keywkord vs. CPA");
  while (keywordsIterator.hasNext()) {
    var keywordStats = keywordsIterator.next().getStatsFor("LAST_WEEK");
    cpa = cost / conversions
    
    if (cpa > 5)
      var keywordText = keyword.getText();

      }
}



I wanted to create a code who extract all keyword with a cpa > 5 and the average cpc of the keyword. My script run but never stop..... i think that i missed something in the script but i don't know what.

Ty

Martin Roettgerding

unread,
Dec 17, 2012, 9:14:55 PM12/17/12
to adwords...@googlegroups.com
Hey,
There are a couple of issues here, but I can't see why the script wouldn't stop. Does it really do that - run forever so that it's automatically stopped after 30 minutes?

I assume that, depending on the number of keywords you have, the script just takes a while without logging more than "Keywkord vs. CPA". Anyway, some pointers:

  var conversions = 0;
 
var cost = 0;
 
var cpa = 0;
 
 
Logger.log("Keywkord vs. CPA");
 
while (keywordsIterator.hasNext()) {
   
var keywordStats = keywordsIterator.next().getStatsFor("LAST_WEEK");
    cpa
= cost / conversions

The variables conversions and cost are set to 0 and never changed. So cpa will always be 0 / 0, which results in NaN (not a number). To do this right, you'd first have get the current keyword's cost and conversions, like cost = keywordStats.getCost(); and the same for conversions.

Still, cpa = cost / conversions only works if conversions is a number greater than zero. You could check for this before doing the division, but this basically means that you will only have to look at keywords with actual conversions. A more elegant way to do this is to use this condition with the original keyword iterator right at the start:

  var keywordsIterator = AdWordsApp.keywords()
 
.withCondition("Conversions > 0")
 
.forDateRange("LAST_WEEK")
 
.get();

And a small issue at the end: var keywordText = keyword.getText(); - this won't work, since keyword is not defined anywhere. At the moment this is no problem, since (0 / 0 > 5) is never true and this part is never executed.

In general, I'd recommend to always log variables when a script doesn't behave the way it is intended to do. In this case, I'd have started with Logger.log(cpa); to see what the variable holds, and then I'd have traced it back to the cost and conversions to understand what happened.

HTH
Martin


Ramarosoa Faneva

unread,
Dec 18, 2012, 3:29:00 PM12/18/12
to adwords...@googlegroups.com
Hi,

Thank you for your help, now i understand why it blocked. The script is now working :)

I m noob technical JS man so I had alot of problem to create my script....like a week to create a litle one haha.

Now, i try to extract the clicks and impression of every number of qualityscore but the Adwords give me alot of number because i used getQualityScore.
Like a thousand of QS....in case of 1-2-3-4-5-6-7-8-9-10.

How can fixe something like that ? Can someone help me ?

Ty

Tomas Kapler

unread,
Dec 18, 2012, 3:35:13 PM12/18/12
to adwords...@googlegroups.com
Maybe i do not understand, but aren't you trying to do something like this: https://developers.google.com/adwords/scripts/docs/tutorials/keyword-performance-report

If so, you can simply learn from the code

Ramarosoa Faneva

unread,
Dec 19, 2012, 4:03:10 PM12/19/12
to adwords...@googlegroups.com
You get it :) I want something like the example without the stats for avg. The problem is for the script Pos vs CTR, the script that I have doesn't have any initialize, I use math.round and some if condition :(

Can you give me an example ? Even if it's the some of clicks by criterion type or other thing.

Ty

Tomas Kapler

unread,
Dec 19, 2012, 4:18:45 PM12/19/12
to adwords...@googlegroups.com
I can't help you, mainly because i still do not understand, what is exactly your need. But IMO the sample script should answer all your questions, especialy if you need something, what is there, so just throw your script away and use what has been done by more skilled programmers ;)

The all magic is in this part:

 while (keywordIterator.hasNext()) {
   
var keyword = keywordIterator.next();
   
var stats = keyword.getStatsFor('LAST_WEEK');
   
var data = qualityScoreMap[keyword.getQualityScore()];
   
if (data) {
      data
.numKeywords++;
      data
.totalImpressions += stats.getImpressions();
      data
.totalClicks += stats.getClicks();
      data
.totalCost += stats.getCost();
   
}
 
}
So explaining with human language - the script iterate every keyword one by one and fill the Array where on "x-axis" you got quality score and on "y-axis" you got impressions, clicks and costs. If you want to add e.g. number of conversions there, just do it, you will probably understand how. When the iterator finishes, you can then simply create averages or other calculations you want. But again - i do not understand what you want, what should be the result.

Ramarosoa Faneva

unread,
Dec 27, 2012, 3:35:42 PM12/27/12
to adwords...@googlegroups.com
Hi,

Sorry if I answer 1 week later but the christmas day was a bit hard :) btw merry christmas to you (and all).

I read the example of script but i din't know that you can use Scoremap in Google Adwords.

Let's make it short because it seems that I couldn't explain my problem.

My problem is i want the total of clics by quality score 1 to 10. 
Exemple : 
QS Clics Costs
1     10    6$
2     15    10$
3     20    10$
4     4      1$
5     6      8$
6
7
8
9
10

The result will give me the sum of clicks of every quality score that have 1
The result will give me the sum of clicks of every quality score that have 2
The result will give me the sum of clicks of every quality score that have 3
.....

I think that the script who can give me that is really important.

Do you undertand my problem ?

Ramarosoa Faneva

unread,
Dec 27, 2012, 3:36:28 PM12/27/12
to adwords...@googlegroups.com
Sorry i send my answer automaticly.

Thank you for your help.

Martin Roettgerding

unread,
Dec 27, 2012, 4:12:23 PM12/27/12
to adwords...@googlegroups.com
Merry Christmas to you too! Good to see I'm not the only one back at work ;)

As far as I can see this is exactly what the script tutorial that Tomas linked to (https://developers.google.com/adwords/scripts/docs/tutorials/keyword-performance-report) does. There's a table on the page that looks just like your example...

Ramarosoa Faneva

unread,
Dec 27, 2012, 4:23:32 PM12/27/12
to adwords...@googlegroups.com
Haha ! It's not really that i came back to work :p you will take it funny but i love Google and his complexity :D

Yup, i read and read and read and try develop the script by using a part of this example of script but i can't have the result that i want. Actually i don't want to use spreadsheet, because this my next step ;)  At the moment, I m try to use the script on Adwords plateform then when i have all the script that i need, i will make a scpreadsheet.

I m not JSman, i try to script but I have a limit of code knowledge. If you can, could you help me ? i just want the code, if you can, the first step of how i can fixe the result of qualityscore only between 1 to 10.

Thank you ! 

Tomas Kapler

unread,
Jan 4, 2013, 11:39:05 AM1/4/13
to adwords...@googlegroups.com
Sorry Ramarosoa, i can't help you any more. It is not that i would not know how, it is that i have allready shown you not only first, but all steps. The script do everything you need and it is clearly explained by me and on the script page, how it works, so everyone with basic script knowledge do understand it and can change it to his needs. If you do not know scripting, than you should simply do not try to write scripts, or first learn it, or pay someone, who knows it.

But i will try to explain it again - It works so that it takes every keyword one by one in the keywordIterator, look at the keyword QS and then add the other values to the results array (data) with QS as a key. After it finishes, it write the results to the spreadsheet, so it looks like this (results from my of my clients)

Quality Score Num Keywords Impressions Clicks CTR (%) Cost
1 0 0 0 0 0
2 0 0 0 0 0
3 41 10758 66 0.61 698.66
4 352 48497 491 1.01 5368.77
5 310 31137 410 1.32 3053.95
6 259 11908 309 2.59 1801.4
7 354 16428 567 3.45 3727.14
8 37 2259 62 2.74 293.37
9 28 514 31 6.03 146.33
10 1139 71350 5419 7.59 18285.79

So exactly what you need.

If you need to do something else with the result, you can.

Tom

Ramarosoa Faneva

unread,
Jan 4, 2013, 5:21:56 PM1/4/13
to adwords...@googlegroups.com
Hi Tomas,

I understand the example code that you give, and i know how to use it with an Google Adwords account. In other way, in this step, I didn't want to use a spreadsheet, I wanted only the result by using the script on Google Adwords plateform. I was closed to create this script but it seems that I can't fixed something because the iterator method extract every QS. I read and understand your example, because if it's not the case, i willn't have the script of how to extract pos vs ctr for all or the perfomance detail focus on KW's semantic.

Sincerely, I didn't do any course of script, I learn it by using public site like "site du zero" or "w3schools"....doing exercise and repeat....check the blurn part on google and repeat.....secondly i willn't pay to do it because first I don't have the money and secondly it willn't give me any pleasure to do it...(may be a teacher !?)...and finaly yes I do not know how to script but for me it's not a reason to stop trying to do it....as we know
"If at first you don't succeed, try, try, try again" :-))

At least, I m here to exchange some knowledge If you want any information, I m here and I will be pleased to answer you. However if it's not the place to exchange information please let me know where is it ?

Faneva

Ramarosoa Faneva

unread,
Jan 4, 2013, 5:29:20 PM1/4/13
to adwords...@googlegroups.com
btw Tomas, you say below "But again - i do not understand what you want, what should be the result."  that the reason why I explain to you again my problem. I wrote again the problem but that's not mean that your help didn't give me the solution of it :-)

Faneva

Tomas Kapler

unread,
Jan 4, 2013, 5:46:43 PM1/4/13
to adwords...@googlegroups.com
Dont take me wrong, this is the proper place for asking, but i have 3 times tried to explained to you, that the answer lies here

it is ok, that you do not want to export it to spreadsheet. If you look in the script, you see, that the exporting to shpreasheet is just the very last phase starting with
// Output data to spreadsheet
You wrote, that you just want the the result by the google adwords platform, not spreadsheet. OK, so how you want it? You want to send it to you by e-mail or you want to just write it to the log?

Just before the outputing data to spreadsheet you have the qualityScoreMap variable full of the data you want, so if i would do some print of it, it would look something like
qualityScoreMap => {
  0 => {
      numKeywords = 123,
      totalImpressions = 456,
     ...
  },
  1 => {
      numKeywords = 423,
      totalImpressions = 756,
     ...
  }, ...
  9 => {
      numKeywords = 823,
      totalImpressions = 956,
     ...
  },

}

the script then shows you, how to iterate this variable and export it to spreadsheet. If you do not want to export it to spreadsheet, you can just put it into Loggger.log, like e.g. in this example https://developers.google.com/adwords/scripts/docs/examples/complete-scripts#cost-clicks
or e.g. send it to mail like e.g. in this example

So you have a script for doing what you want and you have a script for showing the result to you in whatever way you want.

If you want to mix it up and found some problems, i recommend you to start with the first sript, delete parts you do not need, add the parts from the other scripts, then if it does not work, place it here and we can help you to find the problem. But if you try to create something from scratch with almost no knowledge of script and have errors on every line, it is hard to help you - we would have to write it all from scratch and why we would do it when there is allready a sollution?

All codes are imo self explanatory and again, if you do not understand something of it, we can help you.

You shown the result you want and i have shown you the result of the existing script and it is the same. So probably the only problem as i understand it that you want it some other way. If i am right and you want just the qualityScoreMap in Logger.log and you have a difficulties with it, then let us know. If you need something else, then try to explain a bit more ;)

Tom

Ramarosoa Faneva

unread,
Jan 4, 2013, 6:24:20 PM1/4/13
to adwords...@googlegroups.com
If you need something else, then try to explain a bit more ;)
Lovely answer :-)

I was kidding you but for me it was a bit frustrating that i have been blocked by 1 thing. After reading your answer, I really think that the problem came from the way i wrote the script and the way of how extract the part of code that i needed. (for more, i did'nt had any error, I had the result but I didn't had the format that I wanted...)

I get your idea and i will do it in that way. Let me few day (not a year :D) to check it again².

Thank you + so much + for your help Tomas (Martin too ofc). 

Faneva

pedro_...@hotmail.com

unread,
Jan 26, 2013, 9:17:16 AM1/26/13
to adwords...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages