Script To Pause A Campaign At A specific click threshold Within Specific Time Range

105 views
Skip to first unread message

Cyber Ninja

unread,
Sep 14, 2018, 4:48:07 PM9/14/18
to AdWords Scripts Forum
Hi Everyone,

Is there a script that allows me to pause a specific search campaign once It reaches a certain threshold, let's say 100 clicks and Time let's say between 9:00 Am to 3:00 Pm Everyday ?

Any help would be highly appreciated :)

Regards,
Vipin

Message has been deleted

Hiroyuki Miyauchi (AdWords Scripts Team)

unread,
Sep 17, 2018, 3:56:58 AM9/17/18
to AdWords Scripts Forum
Hello Vipin,

There is no readily available script that performs the behavior you described but you may refer to the sample code snippet below. The script will pause campaigns which exceed 100 clicks threshold between 9:00 AM to 3:00 PM. It will also enable any campaigns that are below the threshold. You may remove the enabling campaign lines of code if this is not needed from your end. You may schedule this script to run hourly.

function main() {

    
var timeZone = AdWordsApp.currentAccount().getTimeZone();
    
var nowString = Utilities.formatDate(new Date(), timeZone, 'MMMM dd, yyyy HH:mm:ss');
    
var nowDate = new Date(nowString);
    
var time = nowDate.getHours();

    
// Between 9:00 AM to 3:00 PM
    
if (time >= 9 && time <= 15) {

        
var clicksLimit = 100; //set your clicks limit
        
var campaigns = AdWordsApp.campaigns().get();

        
while (campaigns.hasNext()) {

            
var campaign = campaigns.next();
            
var campaignStats = campaign.getStatsFor("TODAY");

            
if (campaignStats.getClicks() > clicksLimit) {
                campaign
.pause();
            // You may remove this part of the code based on your requirements
            
} else {
                campaign
.enable();
            
}
        
}

    
}

}

Feel free to test the sample script above using the Preview button and let me know how it goes.

Regards,

Hiroyuki

AdWords Scripts Team

CyberNinja

unread,
Sep 17, 2018, 1:23:59 PM9/17/18
to AdWords Scripts Forum
Hi Hiroyuki,


Thanks a lot for sharing the code .

I tried it but when I run it as preview result shows "no changes".

Also I placed it and kept it running with parameters as Time: 18 to 4 and clicks =3 but when clicks surpassed the no. of clicks i.e 3 within the time I mentioned campaign did not pause.

Am I doing something wrong while placing it ?

Regards,
Vipin

Hiroyuki Miyauchi (AdWords Scripts Team)

unread,
Sep 17, 2018, 11:39:10 PM9/17/18
to AdWords Scripts Forum
Hello Vipin,

I would suggest you to add Logger output into your script to check the values of each variable and values returned by methods for debugging purposes. Also, if you want to run the script between 6:00 PM to 4:00 AM, you would need to modify the if statement line below as seen in the code below.

// Between 6:00 PM to 4:00 AM
if ((time >= 18 && time <= 24) || (time >= 0 && time <= 4)) {

After trying this, if you still cannot find the cause of the issue, could you please provide your CID and script name via Reply privately to author so I can further investigate?

Regards,
Hiroyuki
AdWords Scripts Team

CyberNinja

unread,
Sep 18, 2018, 4:49:00 PM9/18/18
to AdWords Scripts Forum
Hi Hiroyuki,

Kudos!!

You nailed it :) Thanks for helping out.

Only one thing left I want it to run automatically let say in every 15 minutes ?

Looking forward.

Regards,
Vipin

Hiroyuki Miyauchi (AdWords Scripts Team)

unread,
Sep 18, 2018, 10:39:46 PM9/18/18
to AdWords Scripts Forum
Hi Vipin,

Glad to hear that it is working fine now.

With regards to scheduling your script, unfortunately, it is not possible to schedule it every 15 minutes. Currently, the available options are "run once, daily, weekly or monthly at a certain hour". You may refer to "Scheduling a script" section in this guide to know more about scheduling a script.

Please let me know if you have further clarifications.

Regards,
Hiroyuki
AdWords Scripts Team

Reply all
Reply to author
Forward
0 new messages