views:

161

answers:

3

I have been trying to setup a custom variable for the past few days and it hasn't been working.

My Google tracking code is part of a master page (asp.net concept) so I can't set the custom variable inside the second script block labeled "Async Google Code" because it is shared by many other sections.

Below is my code and the order it appears in my page. Is there any way I can set it outside the "Async Google Code" script?

<head>

<!-- Setting Custom Var -->
<script type="text/javascript">
     $(function () {
        _gaq.push(['_setCustomVar', 1, 'Account', 'UserType', 2]);
     }
</script>

<!-- Async Google Code -->
<script type="text/javascript">
     var _gaq = _gaq || [];
     _gaq.push(['_setAccount', 'UA-XXXXXXX-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>

</head>
+3  A: 

wrap your custom variable stuff in a function to be called and insert a call to that function between the var _gaq... and _trackPageview lines in the bottom piece.

Crayon Violent
The important detail here is that the custom variable has to be set AFTER the _gaq is set but BEFORE _trackPageView, since the custom variable does not make its own utm.gif call, but instead rides on _trackPageView calls made after its initiation.
yc
Ah, that makes sense. What would happen if I have a function call between the _gaq and _trackPageView and the function doesn't exist on the page? Would this cause an error? I'm a complete js newb, sorry about the basic questions.
chobo
it would give you a function doesn't exist error, but you could wrap it in a try...catch like try { someFunction();} catch (err) { }to avoid the js error
Crayon Violent
This is really strange... My variables are now showing up and I haven't changed anything. It took 3 or 4 days for them to show up,so my original code must be working. I had no idea it took that long
chobo
A: 

I have the same problem... this is my code:

<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-17790624-1']);

  <?php if($sf_user->hasAttribute('id')): ?>
    _gaq.push(['_setCustomVar', 1, "Registered User", "yes", 1]);

  <?php else: ?>
    _gaq.push(['_setCustomVar', 1, "Registered User", "no", 1]);

  <?php endif; ?>

  _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>

I configured today my GA account... is it really going to take 3 or 4 days to see the variables?

I am new in GA, do you know where could I find some good book? I noticed that it has change a lot recently (async), so an old book is not going to work, right?

Thanks!

The Luctus
There is a 24 hour delay on data showing up in reports. Which is still quite a pain in the ass to QA with that kind of delay.
Crayon Violent
You can inspect the _utm.gif hit that is requested, via a tool like Firebug. In code like this, there should be just one. If you see the text 'Registered User' in the parameters, chances are its logging correctly.
yc
Great advice! I'm going to give that a try.
chobo
Thank you @yc, I can see the text "Registered User" in the parameters, so I hope the custom variables start showing up soon...Thanks!
The Luctus
Today I can see the custom variables in my report. By the way, it shows a "Registered%20User" variable. I suggest you to use no spaces in the names... It looks better "RegisteredUser" or "Registered_User" than "Registered%20User"
The Luctus
A: 

Your "setting custom var" code is missing an end bracket.

bozdoz