Sunday, November 17, 2013

Track Adwords Script Runs with Google Analytics

The other day, I was looking into how I could report on how many times my AdWords scripts were running. I figured that since I use Google Analytics reporting for everything else, maybe I should try to use it for reporting on my scripts as well.

So using some information gathered around the web, I put together this simple script to push data into Google Analytics. This will register each script run as a pageview. If you want to keep track of account ids where the script is running, you will need to add AdWordsApp.currentAccount().getCustomerId() somewhere in the CAMPAIGN or PAGE values. Once you have this code in your script, just make a call to beacon(); at the beginning of your code. And since many tracking systems mimic the Google Analytics tracking format, I'm sure this can be easily adapted to other systems.

Thanks,
Russ

/********************************
* Track Script Runs in Google Analytics
* Created By: Russ Savage
* FreeAdWordsScripts.com
********************************/
function beacon() {
  var TAG_ID = 'UA-XXXXXXXX-X';
  var CAMPAIGN_SOURCE = 'adwords';
  var CAMPAIGN_MEDIUM = 'scripts';
  var CAMPAIGN_NAME = 'Your Script Name And Version';
  var HOSTNAME = 'www.freeadwordsscripts.com';
  var PAGE = '/Some/Virtual/Page/Similar/To/Campaign/Name/Probably';
  var DOMAIN_LINK = 'http://'+HOSTNAME+PAGE;

  //Pulled from: http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript
  var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, 
    function(c) {var r = Math.random()*16|0,v=c=='x'?r:r&0x3|0x8;return v.toString(16);});
  
  var url = 'http://www.google-analytics.com/collect?';
  var payload = {
    'v':1,'tid':TAG_ID,'cid':uuid,    
    't':'pageview','cs':CAMPAIGN_SOURCE,'cm':CAMPAIGN_MEDIUM,'cn':CAMPAIGN_NAME,
    'dl':DOMAIN_LINK
  };
  var qs = '';
  for(var key in payload) {
    qs += key + '=' + encodeURIComponent(payload[key]) + '&';
  }
  url += qs.substring(0,qs.length-1);
  UrlFetchApp.fetch(url);
}

12 comments:

  1. Looking for latest update on TNPSC exams? Kalviseithi - #1 educational portal offer latest news about TN state government jobs, educational news and much more information.

    ReplyDelete
  2. Positive site, where did u come up with the information on this posting?I have read a few of the articles on your website now, and I really like your style. Thanks a million and please keep up the effective work. ( Locateanumber.com | www.locateanumber.com | http://locateanumber.com | https://locateanumber.com )

    ReplyDelete
  3. Positive site, where did u come up with the information on this posting?I have read a few of the articles on your website now, and I really like your style. Thanks a million and please keep up the effective work. Locateanumber.com

    ReplyDelete
  4. Do you want to know how to use Website Blockers for Safety? On https://celltrackingapps.com/website-blocker/ you can read more about it

    ReplyDelete
  5. You are so thrilling! I do no longer take into account I've certainly study through some thing like that before. So splendid to discover any other character with some precise mind in this difficulty click. Seriously.. Many thanks for beginning this up. This internet site is some component this is required on the net, a person with a piece of originality!

    ReplyDelete
  6. With a parental control like mSpy, I can track even Skype on my kid's phone. As a parent, I find it extremely important not to let my son to be involved in any kind of online scam. So, I do my best!

    ReplyDelete
  7. This comment has been removed by the author.

    ReplyDelete
  8. these techniques derive results that can perform much better without the need of programming rules. data science course in hyderabad

    ReplyDelete
  9. The way people find information now is quite a bit different than 10 years ago. This may be obvious to most people but many businesses have not responded to this challenge. Your website is your main marketing tool now and your web design is critical to successful marketing. Jake Hackett

    ReplyDelete