Output Any Scripts Report to a Sheet: Google Ads Script

Ensure Your Google Ads URLs Have the Correct Tracking With this Free Script

Next up in our series of quick scripts we’re creating for free following requests is a simple script to ensure all of your ads have the correct tracking parameters applied.

The script can look for a specific custom parameter, a parameter from the URL suffix, or both.

It will go through all of your enabled ads and look for that parameter. It will then send you an email with any ads (and their campaigns and ad groups) that don’t have the parameter applied.

You can optionally also change the bothRequired parameter, which lets you look for both or either. Essentially, this means that if it’s set to false then your won’t get an email alert as long as the parameter is present either as a custom parameter or in the URL suffix.

You can set the script to run as often as you like, although daily is probably the most frequent it should run – to ensure you have time to action any changes before the script runs again and sends you another email.

/*

    _   ___ _   _ _____ ___   _     
   /_\ / __| | | |_   _/ _ \ (_)___ 
  / _ \ (__| |_| | | || (_) || / _ \
 /_/ \_\___|\___/  |_| \___(_)_\___/


*/

var config = {
  customParam: 'CID', // The custom parameter the script should look for - case insensitive (leave blank if not necessary)
  suffix: 'CID', // The final URL suffix the script should look for - case insensitive (leave blank if not necessary)
  bothRequired: false, // If this is true, you get an alert when either custom param or suffix is missing. If this is false, you only get alerted when both are missing.
  emailAddresses: '[email protected]', // Comma-separated list of email addresses that will be alerted if one of the two above is missing 
}

function main() {
  var ads = AdsApp.ads().withCondition('Status = ENABLED AND CampaignStatus = ENABLED AND AdGroupStatus = ENABLED').get();
  var missingTracking = [];
  while (ads.hasNext()) {
    var ad = ads.next();
    var adId = ad.getId();
    var adGroupName = ad.getAdGroup().getName();
    var campaignName = ad.getCampaign().getName();
    var urls = ad.urls();
    var customParams = urls.getCustomParameters();
    var suffix = urls.getFinalUrlSuffix();
    var missingParam = false;
    var missingSuffix = false;

    // Custom paramater needs to be checked
    if (config.customParam && config.customParam.length > 0) {
      if (customParams) {
        var params = Object.keys(customParams).map(function(param) {
          return param.toLowerCase();
        });
        if (params.indexOf(config.customParam.toLowerCase()) === -1) {
          missingParam = true;
        }
      }
      else {
        missingParam = true;
      }
    }

    // Suffix needs to be checked
    if (config.suffix && config.suffix.length > 0) {
      if (suffix) {
        var params = suffix.split('&');
        params = params.map(function(param) {
          return param.split('=')[0].toLowerCase();
        })
        if (params.indexOf(config.customParam.toLowerCase()) === -1) {
          missingSuffix = true;
        }
      } else {
        missingSuffix = true;
      }
    }

    if (missingParam || missingSuffix) {
      var thingMissing;
      if (missingParam && missingSuffix) thingMissing = 'Both';
      else if (missingParam) thingMissing = 'Custom Parameter';
      else if (missingSuffix) thingMissing = 'Final URL Suffix';
      missingTracking.push(['Campaign: ' + campaignName + '\n Ad Group: ' + adGroupName + '\n Ad ID: ' + adId + '\n Missing: ' + thingMissing]);      
    }
  }
  
  if (missingTracking.length > 0) {
    var missingMessage = missingTracking.join('\n\n');
    var message = 
        'Hi there, \n\n' +
        'Some of your ads are missing the necessary tracking - take a look below: \n\n' +
        missingMessage + '\n\n' +
        'Thanks,\nThe Script';
    MailApp.sendEmail(config.emailAddresses, 'Ads Missing Tracking!', message);
  }
}

Want to automate Google Ads with custom scripts?

If you liked this script or would like a personalized one, we’re here to help! Our team of developers with experience in PPC can create custom scripts to automate any aspect of your Google Ads accounts. Schedule a discovery call to discuss how we can assist you in implementing automation scripts effectively.

Recent Posts
google ads high CPC alert script

Google Ads High CPC Alert Script

Discover how Google Ads high CPC alert script can revolutionise the way marketing agencies optimise their campaigns.

pause google ads with low CTR script

Pause Google Ads with Low CTR Script

Use this free script to pause Google Ads with low CTR to help improve the efficiency of your campaign and avoid wasted ad spend.

Recent Posts