2.8K members online now
2.8K members online now
Use AdWords conversion tracking and reporting to measure your results. Have a question about Google Analytics? Post it here, on the Google Analytics Community!
Guide Me

Cannot save the script: main function is not defined

Visitor ✭ ✭ ✭
# 1
Visitor ✭ ✭ ✭

Hello everybody!Smiley Happy


I'm quite in new in Java Script and decided to use the Script example AdWords provides.

I wanted to generate a report about my site links performance and copy and paste from the Library this :



function getSitelinkStats() {
  var campaignIterator = AdWordsApp.campaigns()
        .withCondition('Name = "NAMEOFMYCAMPAIGN"')
  if (campaignIterator.hasNext()) {
    var campaign =;
    // Retrieve the campaign's sitelinks. Retrieving an ad group's
    // sitelinks is similar.
    var sitelinksIterator = campaign.extensions().sitelinks().get();

    while (sitelinksIterator.hasNext()) {
      var sitelink =;

      // You can also request reports for pre-defined date ranges. See
      // DateRangeLiteral section for possible values.
      var stats = sitelink.getStatsFor('LAST_MONTH');
      Logger.log(sitelink.getLinkText() + ', ' + stats.getClicks() + ', ' +




I get this error whenever I try to save or run the script:

"Cannot save the script: main function is not defined. Expected to find function main()."


Isn't weird that an example I got from the library gave me an error? I tried with others examples and I got the same error.


Thanks for your reply!




1 Expert replyverified_user
Marked as Best Answer.
Accepted by topic author Rossana D
September 2015

Re: Cannot save the script: main function is not defined

[ Edited ]
Top Contributor
# 2
Top Contributor

simply add the following code to the script -- typically, at the very end:

function main() {

most of the examples are snippets -- like rooms in a house without a door.

an entry-point is required -- the entry-point is a function known as main.

main() is the doorway to the rooms --
when the script is started, google
runs the function main() first.


a script cannot be saved or used without a main.


see also


Re: Cannot save the script: main function is not defined

Visitor ✭ ✭ ✭
# 3
Visitor ✭ ✭ ✭
Hello Celebird,

Thanks a lot for your clear explanation Smiley Happy

I would like to ask you, if you can suggest me some online course, source-blog where to learn more about Java script.
I can read the code and understanding it more or less, but whenever I want to make some change or adapting a script I found on the web to my needs, it's not easy at all.

I.e. the script about the site links performance from yesterday... at the moment the stats I get are clicks and impressions...

Logger.log(sitelink.getLinkText() + ', ' + stats.getClicks() + ', ' +

What If I want to retrieve more info about it? Like costs or CTR?
I would add this code:
+ stats.getCosts() + ', ' + stats.getCTR()
but apparently those variables are not supported by the function getSitelinkStats() because I get an error.
My question then is: how can I know what is supported for a specific function and what it's not? Where can I learn about them?
Of for example, what if I want my logs written in a spreadsheet?
Thanks a lot in advance for your reply!

Marked as Best Answer.
Accepted by topic author Rossana D
September 2015

Re: Cannot save the script: main function is not defined

[ Edited ]
Top Contributor
# 4
Top Contributor

first, you're welcome.

such questions can be answered by the scripts-api -- not javascript per se.

questions such as what functions support costs or ctr,
are exclusively related to the scripts-api -- not javascript.

or, how to write data to a spreadsheet; again, is related to
the scripts-api or one of the supporting advanced-api's --
not javascript.

even the cannot-save-the-script issue is exclusive to the scripts-api.

the best likely course is to simply study the scripts-api documentation;
including the videos, guides, and reference - before even trying to use
the samples or solutions.

then, ask any follow-up questions within the scripts-api support forum:!forum/adwords-scripts

the scripts blog has updates to the api:

just a few points of clarification:

- the getSitelinkStats() function is merely a sample and is
not part of the scripts-api -- the function could have been

named abc() and the script would still work as described.


generally, that is true for most all the documentation;

anything labeled as a function is usually only a sample,

skeleton, and not one of the scripts-api methods -- so,

can be renamed and reused as you wish.

- the sample does contain methods (functions) from the scripts-api: e.g.

- however, getCost() and getCtr() are not part of
the sitelinks() methods (functions) under the api:

studying the methods (functions) that are exclusive to the scripts-api is key.

of course, knowing the account-gui terminology and
structure is also likely a fundamental prerequisite; e.g.
the account-gui often follows the api rather closely --
the account-gui and scripts-api are built from the
same similar underlying api methods.

almost all external tutorials will be in the context of javascript and browsers --
and therefore will likely confuse not clarify most issues; if the browser concepts
can be ignored then, the general terminology, concepts, and syntax, may help:


see also