Track Website Optimizer Tests with Yoast’s Google Analytics Plugin
Update 06 August 2012: Will not work with Google Analytics Content Experiments.
Update 30 June 2011: Just updated the code to detect if it’s on the page, in case you’re not running a sitewide test.
I’ve been setting up some crazy-complex experiment on my blog recently and I figured I would simplify things by using Yoast’s Google Analytics for WordPress plugin. This is such a well built plugin but my only concern was that there was no easy way to pull Google Website Optimizer experiments information into it - so I wrote a little work-around.
On top of that,it’s incredibly easy to do, so let me show you…
How it works
- GWO control script runs
- Yoast’s GA script runs & calls the GWO-GA integration script
- GWO-GA integration script passes experiment information through a custom variable
- GA tracks it
- Measure your Google Website Optimizer tests in GA
So here’s how you set it up…
1. Adding the code to your site
The order of the scripts is extremely important. First comes Google Website Optimizer’s control script, followed by my GWO-GA integration function and followed by Yoast’s GA script or the wp_head() WordPress function - see below:
GWO-GA integration script
As shown above, place this script between the GWO Control script and GA script on the page (within the
<script> function GWOGAintegration(s,e) { try { var u = utmx('combination'); if (u != undefined) { _gaq.push(['_setCustomVar',s,e,'Variation-'+u,1]); } } catch(err){} } </script>
2. Choosing a custom variable slot
If you’re not using custom variables on your site, skip to step 3.
If you are, you’ll want to first determine which variables are in use, otherwise it will be overwritten. I’m not 100% sure how to do this, but you can check by going to a page with custom variables on it (i.e. a post page) and viewing the source of the page. Find the first number after ‘_setCustomVar’ in the line below:
_gaq.push(['_setCustomVar',1,'category','analytics',3],['_trackPageview'],['_trackPageLoadTime']);
In this case it’s ’1′. If you have multiple ‘_setCustomVar’ pushes, you’ll want to look for the highest number and add one. Because we don’t want that overwritten,we need to put it into an unused slot - in the case above, let’s choose slot ’2′. If you need help identifying this, please let me know in the comments.
3. Updating Yoast’s GA plugin settings
Go into Yoast’s Google Analytics plugin and under ‘Advanced Settings’. You should see a field for ‘Custom Code’. In here you need to first modify, then paste in the following code:
if(typeof(GWOGAintegration) == "function") {GWOGAintegration({Slot Number},'{Experiment Name}');}
Where ‘{Slot Number}‘ is the slot number you chose in the previous step. If you do not have custom variables enabled, you would set this number to ’1′.
Meanwhile ‘{Experiment Name}‘ is the name of the experiment (For every test you run, I recommend changing this to describe the experiment). Generally, keep this short so that it fits, and avoid using any quote marks or other symbols in here.
You would typically add the following for a test:
GWOGAintegration(1,'Test-1');
It should resemble the following once you’re finished with it:
4. Start your experiment and watch the data roll in
I’ve configured the code to work from the point you start your experiment to the point you stop it. So, once you turn it on, data will start to track in GA.
You’ll find it under ‘Visitors’ and ‘Custom Variables’ in the old interface:
Don’t be alarmed if you don’t see the data show up in your account for a few days - custom variables normally take 2 or three days to show up (even if your reports update many times per day).
Anyway, I hope you like it and found it useful. This is a sort of prelude to a better post I’m writing (I just had to get this out of the way first as a reference point for my next post for you)…
This post is exactly what I’ve been looking for. But I have one question…what does “GWOGAintegration(1,’Test-1′);
” do? Is that needed for Google, or this Yoast thingy? Since I’m not using WordPress, I’m only really interested in the GWO aspects of this type of testing.
@JP glad you like it. If you’re looking for one that is not for WordPress, check out my post here (though I’d like to update it based on Andre’s suggestion):
http://www.optimisationbeacon.com/testing/how-to-integrate-google-website-optimizer-with-google-analytics/
In stead of the try-catch construction you could do this:
if (typeof(utmx) == ‘function’)
{
_gaq.push(['_setCustomVar, 1, 'GWO', utmx('combination').toString(), 2]);
}
And you chose 1 and 3 in your customvar’s scope. I prefer 2 (visit scope) for the best results. As soon as you stop a test the visits for that customvar will stop also. With 1 you will keep visitors for the customvar, and 3 is just not usefull here.
@Andre, thanks for the tip.
Is there a way to detect if utmx(‘combination’) is not undefined in the way you showed above? Reason being, It’s possible for utmx(‘combination’) to return undefined if utmx() is a function on the page.
There’s a reason why I specifically chose a Visitor level scope (as opposed to session or page level). It’s because GWO works on unique visitors, so if you’re tracking how a visitor reacts to a different landing page across multiple visits, but they don’t return through that same page you’re testing, you’ll be able to attribute their conversion to the variation across multiple visits. It also helps if you want to track how visitors perform if you’re conducting a longitudinal study (but that’s very rare and difficult to achieve online).
As for page level scope, are you referring to my WordPress category tracking? Using a visit level scope here would mean it would be overwritten each time someone viewed another category.
You can ignore my comment on the page scope (3), didn’t read that part well
You mean that utmx could be a function that already exists? I’ve never seen that, but yes, it could. Then you would have to check what it returns.
utmx can be a function on the page even when the experiment is not running, so for a period, utmx(‘combination’) will return undefined until the test starts.
Experiment off:
- utmx is a function
- utmx(‘combination’) is undefined
Experiment on:
- utmx is a function
- utmx(‘combination’) is an integer
My JS isn’t the greatest, so there’s probably a better way to do things. This is a solution that has worked for us for the past year or so.
By the way André, I’m putting together a little bit of a complex test using JS, mind if I email you a bit later to get your opinion of the code?
Hey Robert,
Have looked for years to find someone as into GA and GA testing. Wow you’re an true expert! Where are your consulting / contact services (do what Yoast does - you could make a great living
So I took the first steps and installed Yoast’s GA plugin. But for 4 days now I cannot get any information to pass over. Now my GA is just reading zero hits per day (while my WP Stats are still in hundreds). Do you see anything on why GA might not be seeing info. Here is GA Chrome’s Debug error report:
Resource interpreted as Image but transferred with MIME type text/html.
ga_debug.js:11_gaq.push processing : “[_setAccount,UA-2109736-2]”
ga_debug.js:11_gaq.push processing : “[_setDomainName,.auto]”
ga_debug.js:11_gaq.push processing : “[_setCustomVar,1,post_type,page,3]”
ga_debug.js:11_gaq.push processing : “[_setCustomVar,2,author,meninkilts,3]”
ga_debug.js:11_gaq.push processing : “[_setCustomVar,4,year,2011,3]”
ga_debug.js:11_gaq.push processing : “[_trackPageview]”
ga_debug.js:11_gaq.push processing : “[_trackPageLoadTime]”
carouFredSel: No element selected.
Cheers Brent
Hey Brent,
Thanks for the compliment - I’m flattered :). I also wonder why there aren’t more analytics aficionados out there (I think most of them are in hiding). Unfortunately I don’t offer consulting services because my work contract doesn’t allow it, but I’ve definitely thought about it.
You may not be seeing the GWO info because the experiment must be switched on (not just being previewed). Also, you’ll need to check and make sure you have defined the GWOGAintegration() function ABOVE the GA script.
If Yoast’s plugin is automatically inserting the script into your page, you’ll need to place the GWOGAintegration() function right above the wp_head() PHP function in your WordPress header.
Let me know how it goes. If you’re still having troubles, can you email the HTML markup for your header on to my Gmail account (username kingo55)?
I see your update on that it doesn’t work with the Yoast Analytics Plugin. Have you found a way to make it work, since posting the update? Should I switch to a different g-analytics plugin?
Hey Chris,
Unfortunately when Google Website Optimizer moved to Analytics’ Content Experiments, they removed the features that make this integration possible. Long story short, this approach won’t work any more - not with Joost’s GA plugin for WordPress or any other site (though I have been looking into alternatives - will post an alternative when I’ve found it).
In the meantime, since Gooogle Analytics Content Experiemnts (can we coin this GACE?) only supports A/B/n tests, you may be able to get away with the following options:
1. Setup two advanced segments - one which includes visits to the Original page, another which includes visits to the Variation page.
2. Customise and add the following code to each of the posts (it has to sit on the page after your Google Analytics script):
<script type="text/javascript">
_gaq.push(['_setCustomVar',1,'Split test name','Page version',1],['_trackEvent','Split test name','Page version','',0,true]);
</script>
Be sure customise this to fit each of your own test pages. I.e. Custom variable slot number (if the slot is already in use, you can’t use it), name of the split test and the page version.
Keep in mind this is not without its limitations. You will have to remove the code from the site when you stop the test.
Does that help, Chris?