- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Subscribe
- Printer Friendly Page
Cannot save the script: main function is not defined
September 2015
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Email to a Friend
- Report Abuse
Hello everybody!
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"')
.get();
if (campaignIterator.hasNext()) {
var campaign = campaignIterator.next();
// Retrieve the campaign's sitelinks. Retrieving an ad group's
// sitelinks is similar.
var sitelinksIterator = campaign.extensions().sitelinks().get();
while (sitelinksIterator.hasNext()) {
var sitelink = sitelinksIterator.next();
// You can also request reports for pre-defined date ranges. See
// https://developers.google.com/adwords/api/docs/guides/awql,
// DateRangeLiteral section for possible values.
var stats = sitelink.getStatsFor('LAST_MONTH');
Logger.log(sitelink.getLinkText() + ', ' + stats.getClicks() + ', ' +
stats.getImpressions());
}
}
}
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!
Re: Cannot save the script: main function is not defined
[ Edited ]September 2015 - last edited September 2015
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Email to a Friend
- Report Abuse
simply add the following code to the script -- typically, at the very end:
function main() {
getSitelinkStats();
}
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
https://developers.google.com/adwords/scripts/docs/your-first-script
https://developers.google.com/adwords/scripts/community/
https://en.wikipedia.org/wiki/Dennis_Ritchie
Re: Cannot save the script: main function is not defined
September 2015
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Email to a Friend
- Report Abuse
Thanks a lot for your clear explanation

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() + ', ' +
stats.getImpressions());
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!
Re: Cannot save the script: main function is not defined
[ Edited ]September 2015 - last edited March 2017
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Email to a Friend
- Report Abuse
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.
https://developers.google.com/adwords/scripts/
then, ask any follow-up questions within the scripts-api support forum:
https://groups.google.com/forum/#!forum/adwords-scripts
the scripts blog has updates to the api:
http://googleadsdeveloper.blogspot.com/search/label/adwords_scripts
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.
AdWordsApp.campaigns()
extensions().sitelinks()
- however, getCost() and getCtr() are not part of
the sitelinks() methods (functions) under the api:
https://developers.google.com/adwords/scripts/docs/reference/adwordsapp/adwordsapp_stats
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.
https://support.google.com/adwords/answer/2375416
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:
https://developer.mozilla.org/en-US/docs/Web/JavaScript
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Introduction_to_Object-Oriented_JavaScript
see also
https://developers.google.com/adwords/scripts/docs/best-practices