Set AdSense Channels by Browser, Referrer, etc.

Written October 2nd, 2010
Categories: Articles, Scripting / Programming
No Comments »

Ever wondered how you can set channels in your Google AdSense ad units based on on-the-spot data like user browser or origin? In this quick tutorial I’ll be showing you how to set AdSense channels in order to track statistics based on user browser types, how they arrived at your site, and pretty much anything else.  It all pivots on the google_ad_channel parameter.  For those of you who are extra savvy, here’s a summary:

  1. Build an AdSense ad unit using native code,
  2. Create AdSense channels for your parameters,
  3. Write JavaScript code to detect the value you need to use,
  4. Use the google_ad_channel parameter to track stats on the right channel.

Build an AdSense Ad Unit With Native Code

Creating an AdSense Unit

Creating an AdSense Unit is Easy. Channel setup can be tricky.

Consider the following ad code which would form a regular 300×250.

<script type="text/javascript"><!--
google_ad_client = "ca-pub-xxxxxxxxxxxx1083";
/* Testing 300x250 - Do not target */
google_ad_slot = "1168546099";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>

When you create an ad unit, you’re given a chance to create and assign channels to it. Normally, these channels are going to be set values that are true no matter what. For example, “Medium-Rectangle”, “Above-The-Fold”, and “Articles-Section-Sidebar”. What you can’t seem to set are conditional parameters like “User-Came-From-Google” or “User-On-Firefox”.

Create a Channel for your Parameter

Create an AdSense channel to track your parameter.

Create a new channel to track what you’re looking to gather stats for. This could be just about anything, but for this example I’m going to create 5 channels

  • “Users from Google”,
  • “Users from Bing”,
  • “Users from Yahoo”,
  • “Users from 3rd Party Sites”,
  • and “Internal Link – No Referrer”.

When you create these channels, you’ll see that each one has a 10-digit ID number in the “ID” column.  Take note of that- we’ll need it later!

Write JavaScript to Detect Parameter Values

At this point, you’ll need to write JavaScript code to detect the parameter you want to track.  In our case, it’s going to be bucketing where the user is coming from when they load a page.  I’ll also include code for tracking users by browser, but the sky is the limit on this technique!

Tracking AdSense Metrics by Referrer

In order to attach the appropriate channel ID based on referrer, we need to build code that determines the user’s origin.  Consider the following:

<script type="text/javascript">
var adsense_referrer = document.referrer;
var adsense_referrer_channel = "0123456789";  // Set a default value, like "Users from 3rd Party Sites" channel.

if (adsense_referrer.search(/google\.*/i) != -1)
    adsense_referrer_channel = "1234567890"; // Set to "Users from Google" channel.

else if (adsense_referrer.search(/bing\.*/i) != -1)
    adsense_referrer_channel = "2345678901"; // Set to "Users from Bing" channel.

else if (adsense_referrer.search(/yahoo\.*/i) != -1)
    adsense_referrer_channel = "3456789012"; // Set to "Users from Yahoo" channel.

else if (adsense_referrer.search(/mrbluesummers\.*/i) != -1 || !adsense_referrer)
    adsense_referrer_channel = "4567890123"; // Set to "Internal Link - No Referrer" channel.
</script>

Tracking AdSense Metrics by Browser

You can follow the above system for assigning a channel value, but the difference here is that you’ll pivot on detecting the name of the user’s browser. There’s an excellent (long) article on how to use JavaScript to detect the user’s browser. It’s a little trickier than working with referrer because there’s so many browsers, so I’ll leave it to you.

Use the google_ad_channel Parameter to Track Metrics

At this point, you can just insert your new channel into the ad code using the google_ad_channel parameter.  This will append the value to your channels, not overwrite them! So, for example, if you have two channels in your ad unit- “Sports” and “Leaderboard”- you’ll now have 3 channels- “Sports, “Leaderboard”, and “Users from Google”.  Note that if you want to use more than one channel, the google_ad_channel parameter is an array of strings, so just stack up your strings as an array and you can get even more powerful reporting!

<script type="text/javascript"><!--
google_ad_client = "ca-pub-8741444217221083";
/* Testing 300x250 - Do not target */
google_ad_slot = "1168546099";
google_ad_width = 300;
google_ad_height = 250;
google_ad_channel = adsense_referrer_channel;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>

Leave a reply


©2023 MrBluesummers.com