Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Universal Analytics custom dimensions and trackpageview

I'm trying to migrate from Google old Analytics to Universal Analytics. I have code below and from universal - developers guide I couldn't find solution.

Inside my analytics code I had these lines. Part 1:

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

     _gaq.push(['_trackPageview','/tools/one');
     _gaq.push(['_setCustomVar', 1, 'name', 'michael', 1]);

(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);
    })();

Also inside my some other script I have this code snippet. Part 2:

if (typeof _gaq !== "undefined" && _gaq !== null) {
    _gaq.push(['_trackPageview', '/dosomework']);
}

How can I convert "gaq.push"s inside 2 parts and add to my universal analytics code ?

like image 928
trante Avatar asked Nov 29 '22 02:11

trante


1 Answers

Unfortunately, migrating to analytics.js isn't as simple as just changing the code syntax.

Firstly you should know that Universal Analytics is currently in open beta phase. Currently google does not offer a way to "upgrade" or "convert" an existing web property to take advantage of universal analytics (analytics.js) tracking. You will need to setup a new web property (or new account) and check the "universal analytics" radio button.

Google currently recommends setting up analytics.js code in addition to your current ga.js code. Once you are happy that the base data is lining up between the two, you can either keep both versions on your page or decide on a date to remove the old ga.js code. The historical data in your old profile will still be there but it won't be tied to the new web property. I don't know if Google will eventually offer an "upgrade" or "convert" feature for existing ga.js based web properties; so far I have not seen any news on if/when they will offer this.

Moving on to Universal Analytics (analytics.js) code ...

Universal Analytics does not use the .push syntax. Instead, it has a function ga() that requires arguments to be passed to it. The first argument is the "command" argument, and the additional arguments are for passing additional settings, values, etc. based on the command.

  • setting the GA account is now done with the 'create' command
  • tracking a page view is now done with with the 'send' command
  • setting a custom variable* is now done as either an argument in the 'send' command (for only popping it on that 'send' command), or with the 'set' command (for setting it to be popped with all 'send' commands executed on the page)..but about this...

Custom Variables no longer exist

Well they do, but how they are implemented is different. Universal Analytics offers custom dimensions and metrics. Custom Variables are mostly what Custom Dimensions now are. The main difference is that setting things like the name and scope of the variable is now done within the GA interface instead of as an argument to the function. Also, you get more than 5 to work with now. To set this up, click on the web property you created, and you should see tabs

Profiles Tracking ..Custom Definitions

Click on the Custom Definitions tab to setup your custom dimensions and metrics there.

Now on to the page code

This is what the "equivalent" of the code you posted would look like:

First snippet:

<!-- Google Analytics -->
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');

ga('create', 'UA-123456-1');
ga('send', 'pageview', '/tools/one');
ga('set', 'dimension1', 'michael');

</script>
<!-- End Google Analytics -->

note: as mentioned above, you would set the name and scope of the dimension within the interface. 'dimension1' should be changed to whatever dimension# you created.

Second snippet:

if (typeof ga == 'function') {
    ga('send', 'pageview', '/dosomework');
}

sidenote: Not really related to your question, but in your code you first send a page view and then set the custom variable. In case you didn't know, if you set the custom variable (_setCustomVar) after the page view (_trackPageview), your custom variable will not be sent with that page view (the '/tools/one' hit). It will (assuming your 2nd snippet gets popped later on) be sent along with that 2nd page view (the '/dosomework' one). Not sure why you would have two separate pageviews or if you knew about that order of operations thing but if you're happy with how things currently look in the reports..well the analytics.js version will behave the same way.

like image 166
Crayon Violent Avatar answered Dec 04 '22 16:12

Crayon Violent