Scroll to top

Google Ads Script to automatically pause/activate campaigns when advertiser is temporary disabled for 2performant

This script will help you prevent wasted budget with running campaigns for advertisers that are temporary disabled, it will also enable the campaigns automatically when the advertiser is active again in the platform.

To add the script, in your Google Ads Account, go to “Tools&Settings” -> “Scripts” -> press the “+” button -> insert a descriptive script name, and copy/paste the following code in the code editor -> Authorize the script -> Save the script -> Go to the script list (“Tools&Settings” -> “Scripts”) -> in the column “Frequency” press the “-” sign and in the drop down set it to run every hour.

Important: in order for the script to run on the campaigns, the campaign name must contain “-auto”, if your campaign is called “libris.ro dsa+maxclicks”, in order for the script to run on that specific campaign, it must be renamed to “libris.ro dsa+maxclicks -auto”.

If this tool disables a specific campaign it will add “-missingtool” to the campaign name, it will automatically remove the “-missingtool” tag from the campaign name when the advertiser is back and it activates the campaign.

function is_campaign_expired(e) {
    var a = e.urls().getTrackingTemplate();
    if (null == a) return !1;
    if (-1 != (a = a.toString()).indexOf("mergi.la") && (a.indexOf("?") ? a += "&get_hidden_url=1" : a += "?get_hidden_url=1", a = a.replace(/{/g, "%7B").replace(/}/g, "%7D")), -1 === a.indexOf("2performant.com") && -1 === a.indexOf("get_hidden_url=")) return Logger.log("Other network:" + a), "other platform";
    a = a.replace("_{gclid}", "").replace("{escapedlpurl}", "").replace("{lpurl}", "").replace("{gclid}", ""), Logger.log(a);
    var i = UrlFetchApp.fetch(a),
        n = Math.round(i.getResponseCode());
    return 500 == n || 404 == n || 503 == n || -1 !== (i = i.getContentText()).indexOf("Expired") && -1 !== i.indexOf("If you have an online store...")
}

function run_classic() {
    for (var e = AdsApp.campaigns().withCondition('CampaignName CONTAINS "-auto"').get(); e.hasNext();) {
        var a = e.next(),
            i = a.getName().toLowerCase();
        if (-1 !== i.indexOf("-auto") && -1 === i.indexOf("-nodisable")) {
            var n = is_campaign_expired(a);
            !0 === n ? a.isEnabled() && (Logger.log(i), Logger.log(n), a.pause(), name = a.getName().replace(" -missingtool", ""), a.setName(name + " -missingtool")) : -1 !== i.indexOf("-missingtool") && !1 === n && a.isPaused() && (a.enable(), a.setName(a.getName().replace(" -missingtool", "")))
        }
    }
}

function main() {
  run_classic()
}
Author avatar
razvypp
https://marketplus.io/blog

Post a comment

Your email address will not be published. Required fields are marked *

17 − 8 =