URL Tracking Checker: Google Ads 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: 'julian@acuto.io', // 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);
  }
}
10 views0 comments

Recent Posts

See All