Unfortunately, switching to analytics.js is not as simple as changing the syntax of the code.
First, you should be aware that Universal Analytics is currently in open beta. Currently, google does not offer a way to “upgrade” or “transform” an existing web property to use universal analytics tracking (analytics.js). You will need to configure a new web resource (or a new account) and set the "universal analytics" switch.
Google currently recommends setting up the analytics.js code in addition to your current ga.js code. Once you are happy that the underlying data is built between them, you can either save both versions on your page, or decide to delete the old ga.js. code. The historical data in your old profile will still be present, but it will not be tied to the new web resource. I don’t know if Google will ultimately be able to offer an “upgrade” or “convert” function for existing web properties based on ga.js; so far I have not seen any news about when / when they will offer it.
Switching to Universal Analytics code (analytics.js) ...
Universal Analytics does not use .push syntax. Instead, it has a ga() function that requires passing arguments. The first argument is the "command" argument, and additional arguments are for passing additional parameters, values, etc. Based on the team.
- GA account setup is now done with the "create" command
- pageview tracking is now done using the submit command
- custom variable * is now configured either as an argument in the send command (only to add it to the send command), or using the set command (in order to set it for all send commands, executed on the page), but more about that ...
User variables no longer exist
Well, they do, but how they are implemented is different. Universal Analytics offers customizable dimensions and metrics. User variables are basically user sizes. The main difference is that setting up objects such as the name and scope of a variable is now done in the GA interface, rather than as a function argument. In addition, more than 5 people are now working with you. To configure this, click on the created web resource and you will see tabs
Profiles Tracking ..Custom Definitions
Click the Custom Definitions tab to configure your own dimensions and metrics.
Now on the page
This is what the “equivalent” code you posted looks like:
The first fragment:
<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>
Note: as indicated above, you must specify the name and scope in the interface. 'dimension1' should be changed to any dimension # you created.
Second snippet:
if (typeof ga == 'function') { ga('send', 'pageview', '/dosomework'); }
sidenote: not relevant to your question, but in your code, you first submit the page view and then set the user variable. If you do not know, if after viewing the page ( _trackPageview ) you set a user variable ( _setCustomVar ), your custom variable will not be sent with this page view ("/ tools / one"), It will be (provided that your second snippet appears later) will be sent along with this view of the 2nd page ("/ dosomework"). You don’t know why you would have two separate page views, or if you knew about this order of operations, but if you are happy with how things look in reports now, the version of analytics.js will behave the same.