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);
}

Monday, November 11, 2013

Building Entity Deep Links with AdWords Scripts

I was trying to build a report on problems in an account and I was thinking to myself, "Man, it sure would be nice to deep link directly to the entity that was having issues." Then I realized that is the script change logs could do it, I probably could too.

It turns out, there are two magic numbers that you need in order to get this to work. When you login to your account, in the url, you will see __u= and __c=. According to this blog post, these values are the 'effectiveUserId' and 'customerId' respectively. Unfortunately, there isn't a way to access these values when using scripts, so you will have to manually copy them into the script below.

After that, you can include the function in all your scripts and deep link to your heart's content. It isn't the prettiest thing in the world but it is self contained so it should be easy to copy into the bottom of your scripts.

Thanks,
Russ

  // Link to the Keyword Tab of the AdGroup
  Logger.log(getUrl(someAdGroupEntity,'Keywords'));
  // Link to the Ads Tab of the AdGroup
  Logger.log(getUrl(someAdGroupEntity,'Ads'));      
  // Link to Location Settings Tab of the Campaign
  Logger.log(getUrl(comeCampaignEntity,'Settings:Locations')); 


/***********************************
* Build Deep Link Urls for Entities
* Version 1.0 
* Created By: Russ Savage
* FreeAdWordsScripts.com
***********************************/
function getUrl(entity,tab) {
  var customerId = '__c from the url';
  var effectiveUserId = '__u from the url';
  var decodedTab = getTab(tab);  
  
  var base = 'https://adwords.google.com/cm/CampaignMgmt?';
  var url = base+'__c='+customerId+'&__u='+effectiveUserId+'#';
  
  if(typeof entity['getBudget'] !== 'undefined') {
    //A Campaign
    return url+'c.'+entity.getId()+'.'+decodedTab+'&app=cm';
  }
  if(typeof entity['createKeyword'] !== 'undefined') {
    //An AdGroup
    return url+'a.'+entity.getId()+'_'+entity.getCampaign().getId()+'.'+decodedTab+'&app=cm';
  }
  if(typeof entity['getMatchType'] !== 'undefined') {
    //A Keyword
    return url+'a.'+entity.getAdGroup().getId()+'_'+entity.getCampaign().getId()+'.key&app=cm';
  }
  if(typeof entity['getHeadline'] !== 'undefined') {
    //An Ad
    return url+'a.'+entity.getAdGroup().getId()+'_'+entity.getCampaign().getId()+'.create&app=cm';
  }
  return url+'r.ONLINE.di&app=cm';
  
  function getTab(tab) {
    var mapping = {
      'Ad groups':'ag','Settings:All settings':'st_sum',
      'Settings:Locations':'st_loc','Settings:Ad schedule':'st_as',
      'Settings:Devices':'st_p','Ads':'create',
      'Keywords':'key','Audiences':'au','Ad extensions':'ae',
      'Auto targets':'at','Dimensions' : 'di'
    };
    if(mapping[tab]) { return mapping[tab]; }
    return 'key'; //default to keyword tab
  }
}

Wednesday, October 30, 2013

Disable Ads and Keywords For Out of Stock Items

UPDATE 2016-01-26: In response to some common issues, this script has been updated to include some enhancements. Please see the change log notes for v1.2.

As a follow up to my questions about cool scripts for Black Friday / Cyber Monday, today I put together a script to run through your urls and check if the item is out of stock on the website. If it is, we will pause the AdGroup.

This script has some of the same elements of my script on checking for broken links in your account, but it actually pulls the html source of each page and searches for a configurable string that lets it know when it is out of stock.

Let's walk through an example. I love some of the quirky gifts I find on ModCloth.com. But like any online store, some items go out of stock. Here is one I found while testing this script.
In order to get this script to work, I need to find out what is different about the page when it goes out of stock. If I right click and view the page source, and search for the work "stock", I can see a few different places where it is used. One of them is the following that says "in_stock":false.
That looks promising. I check on an in stock item and sure enough, "in_stock":true is on that page.

Alright, so now I know what text I need to use to fill in the OUT_OF_STOCK_TEXT variable in my code. Now each site is going to be a little different, so I have a simple script that uses the same url logic as the complete script that you can use for testing.

Once you find some HTML text in the source of the landing page that identifies if an item is out of stock, you should be good to go on the full script. There are a few other options in the script that allow you to enable or disable various url manipulations in the script. And remember, this will pause only the Ads or Keywords that link to the page with the out of stock item.

Thanks,
Russ

Monday, October 21, 2013

Use AdWords Scripts To Automate Black Friday

Black Friday is quickly approaching for retailers in the United States. I have some ideas for ways that you can automate your campaigns using AdWords Scripts, but I'd love to get your thoughts on some of them.  Here's what I can think of.  Please comment with the ones that you'd like to see most.

  • Countdown Script - Similar to this script from Google but read the information from a Google doc and add support for timezones.
  • Add Pricing, Inventory, and percent discount to Creatives - Use a Google Spreadsheet to update prices and inventory for your items. That info will be read by the script get updated in the Ads hourly.
  • Automatically Pause Ads as Items Go Out of Stock - This script will monitor your landing pages and pause the ads once it sees that the item is no longer in stock.
  • Update Mobile Modifiers and Location Modifiers for Retail Locations Throughout the Day - This script would start with a boost to specific location and mobile modifiers for your campaigns and then slowly ramp them back down throughout the day. Set the end time and it will automatically calculate the ramp down.
Those are just a few ideas I had.  Do you have a better idea? Would you like to see one of the scripts above?  Let me know by leaving a comment.  I'll implement the winner by the end of October.


Thanks,
Russ