Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting a custom variable in Google Analytics after _trackPageview is called

Using Goggle Analytics I'd like to use a custom variable. This is built in functionality, but unfortunately I don't have control over the code that loads GA and calls _trackPageview;

This means I can't call _setCustomVar before _trackPageview

If I call _trackPageview a second time will it log two page views?

For example

// I'm not able to change this order
_gaq.push(['_setAccount', 'UA-XXXXXXXX']);
_gaq.push(['_trackPageview']);

// I'm forced to run this after the first _trackPageview
_gaq.push(['_setCustomVar',1,'name','value']);
_gaq.push(['_trackPageview']);

Is there any other way to get the custom variable set

like image 842
slifty Avatar asked Apr 06 '12 02:04

slifty


2 Answers

Yes, each _trackPageview will log a page view.

You could pass a pageURL to the second _trackPageview, and set a filter in your analytics profile to ignore those page views.

_gaq.push(['_trackPageview', '/dummyPageName']);

Alternatively, instead of a second _trackPageview, you could use _trackEvent to cause a tracking GIF request and deliver the custom variable.

_gaq.push(['_setCustomVar', 1, 'name', 'value']);
_gaq.push(['_trackEvent', 'dummy category', 'dummy action']);
like image 112
mike Avatar answered Sep 20 '22 15:09

mike


In order to prevent affecting the statistics on both your page view count and your bouncerate you should probably use the _trackEvent method and remember to set the opt_noninteraction variable to false. This should neither track a page view nor affect the bouncerate

_gaq.push(['_setCustomVar', 1, 'name', 'value']);
_gaq.push(['_trackEvent', 'category', 'action', 'label', 1, true]);
like image 44
Søren Dam Avatar answered Sep 22 '22 15:09

Søren Dam