slifty
slifty

Reputation: 13811

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

Upvotes: 8

Views: 4834

Answers (3)

mike
mike

Reputation: 7177

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']);

Upvotes: 9

Bogdan
Bogdan

Reputation: 1083

I'm having the same issue, and it seems the better solution is to move _setCustomVar BEFORE _trackPageview is called. You're going to have to initiate the _gaq variable prior to the _setCustomVar like this:

var _gaq = _gaq || [];
_gaq.push(['_setCustomVar',1,'name','value',3]);

Upvotes: 1

Søren Dam
Søren Dam

Reputation: 1555

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

Upvotes: 4

Related Questions