David
David

Reputation: 16726

Google Analytics : _setvar to new tracking code

I have been using the older version of analytics code and used the following to track different types of users

<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-xxxxxxx");
pageTracker._setVar('memberlevel-2');    pageTracker._trackPageview();
} catch(err) {}</script>

How do I use this with the new asynchronous code? Google Analytics forums is dead and i got no response :(

Upvotes: 5

Views: 1997

Answers (2)

ysrb
ysrb

Reputation: 6740

Try reading this for setting custom variable:

http://code.google.com/apis/analytics/docs/tracking/gaTrackingCustomVariables.html

Sample code for to track page view:

<script type="text/javascript">
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-XXXXX-X']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();
</script>

Upvotes: 3

Yahel
Yahel

Reputation: 37305

_setVar still works, but you should use _setCustomVar instead, as its more powerful.

Here's what your old code looks like using the async code:

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-XXXXX-X']);
  _gaq.push(['_setVar', 'memberlevel-2']); 
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

To use setCustomVar instead, you could do this:

  _gaq.push(['_setAccount', 'UA-XXXXX-X']);
  _gaq.push(['_setCustomVar', 1, 'memberlevel', '2', 3]);// page-level scope (3), in slot #1
  _gaq.push(['_trackPageview']);

Upvotes: 3

Related Questions