Google Universal Analytics custom dimensions and trackpageview - google-analytics

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 ?

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.

Related

Pageview-level Custom Dimensions in Google Analytics?

The case: We are running a test on 5% of page views (NOT USERS) with a different ad layout. We are looking to tag each page view that has the different ad layout with a custom dimension.
The custom dimension scope is "Hit".
The problem we're running into: Google Analytics reporting is showing more than 5% of page views with the custom dimension. We know (via other reporting) that the different ad layout is only being shown to 5% of traffic (so Google Analytics is overreporting).
I'm assuming this is happening because the dimension we're setting is staying for subsequent page views (i.e. once a user has been tagged with that dimension, all page views after that are tagged). This is almost certainly due to a misunderstanding of how dimensions work and/or the code.
The code:
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'GA_TRACKING_ID', {
'custom_map': {'dimension1': 'adlayout'}
});
Later on in code:
gtag('event', 'adlayout_dimension', {'adlayout': 'true'});
Questions:
Are custom dimensions the correct way to do this? Is there a better feature for tagging a page view in Google Analytics?
If so, where am I going wrong with my code/setup that's causing it to overreport?
The event is firing after the pageview and the custom dimension is associated with the event.. not the pageview.
To fix it going forward, pass the custom dimension with the pageview itself rather than an event. To do so, edit the tracking code for the relevant pages similar to the following
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=GA_TRACKING_ID"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'GA_TRACKING_ID',{
"dimension1": "true"
});
</script>
You can then create a custom report using a flat table, with Page and the Custom Dimension as dimensions together with the desired metrics.
I think that in this case the most suitable scope is at session level. In this way you can see all the sessions that have seen a particular page tagged (obviously if in a session you can see more than one page tagged with your system only the last value will be saved).
Otherwise at hit level but only for specific pages and looking at unique pageviews.

Google Tag Manager - How to avoid data loss

I am using Google Tag Manager to register events with Google Analytic. At one plance I am changing url on the changeof a dropdown. I want to track the same event on Google Analytics. I am worried what would happen if page is changed before event is registered with GA. Could you please let me know if there is a feature in GTM that can ensure that page is not changed before event is registered with GA.
Here is the code that will execute on the change of the dropdown
var targetCityChangedEventName = "TargetCityChanged";
$("#location", topHeader).bind({
"change": function(ev, obj) {
dataLayer.push({event : targetCityChangedEventName });
var url = "http://" + window.location.host + "/" + $(this).val();
window.location = url;
}
});
If you are using ga.js (asynchronous Analytics) you can set an hit callback (a macro that returns a function) in the tag template under "advanced configuration and do the redirect there (possibly you'd need a separate analytics tag just the change event).
If you use Universal Analytics there was a discussion at the Tag Manager Google Group a while ago where Googles Brian Kuhn suggested the following way ( I have not tested this):
In the meantime, have you tried this?
dataLayer.push({callback:
function() {
alert(123);
});
Then, create a dataLayer macro that reads the "callback" key. Then,
use that macro as the value of a "fields to set" pair on your UA tag,
under the field name "hitCallback".
Instead of the alert you'd do the redirect.
In case that's not clear, a hit callback is a function that can be passed to the tracking calls and is executed after the tracking call has executed.
I was able to resolve the same issue by simply inserting a delay. The dataLayer.push doesn't need to return anything, so a 100 millisecond delay is sufficient in 99% of the cases for the dataLayer.push to be executed.
dataLayer.push({ ... });
setTimeout( function(){ window.location = ...; }, 100 );
Note that the GTM preview/debug mode gives false positives - you must make sure that your tags are actually being fired, in my case my event was for a virtual pageview and I could see the results in RealTime Analytics. Without the delay, I could see that the tag was not being fired.

Google Anlytics event not firing

I am not getting and event data in GA. I installed Google Analytics Debugger Chrome extension and I see nothing happening (same goes when looking at Network panel in developer tools). I Googled it and read many (many) other answers and it looks like I'm doing things right. Page views, etc. are registering correctly...
I have this code as the last thing before my closing tag:
<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-MYREALCODE', 'mybna.net');
ga('send', 'pageview');
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-MYREALCODE']);
_gaq.push(['_trackPageview']);
</script>
My event handlers are done using jQuery, all inside an external js file, loaded before the closing tag. For example:
$(function () {
$('#show-less').click(function (e) {
pbr.showHideMore(e);
_gaq.push(['_trackEvent', 'ShowMore', 'Hide', 'top button']);
});
});
Any ideas anyone?
Edit: I keep deleting and undeleting this anwswer because I'm not sure if your code is redundant or if you are actually trying to send data to two different properties.
If it's the first, see original answer below. For the latter make sure that you account/property-Ids are pointing to correctly configured properties (i.e. the first UA, the latter asynchronous), plus I'm pretty sure you need to include the ga.js liberary two if you want the methods pushed in the array to be executed.
You are mixing Universal Analytics and asynchronous code, that's not going to work (i.e. if you have ga send and _gaq.push in the same piece of code you are most probably doing it wrong). Adapt your event tracking code for UA and you should be alright.
Try wrapping the click listener in a document ready function:
$(document).ready(function(){
$('#show-less').click(function (e) {
pbr.showHideMore(e);
_gaq.push(['_trackEvent', 'ShowMore', 'Hide', 'top button']);
});
});
I often find that the listener won't link up until the DOM is fully loaded. Sometimes it does, though, but that might have to do with the code being below the object in question in the code.
It always helps me to put an "alert" into the click function while testing to be sure it's actually firing off (and catching) that event.

Custom reports showing download events in Google Analytics

Has anyone been able to include downloads within a custom report in Google analytics? I know I can get general/all events, but I just want downloads.
Thanks
If you are asking to track the downloads then you can add _trackEvent method in to the link that you want to track, like
Click <a onclick="_gaq.push(['_trackEvent', 'Downloads', 'Demo', this.href]);" href="downloads/google_dynamic_map.zip">here</a> to download with demo.
Above code is from my working site and I'm able to track download of that file.
To view the report you can select the Contents->Events menu from the left nav bar of your google analytics page.
In my link Downloads is the Event Category and Demo is the Event Action and this.href is my Event Label which gives me the url of the file.
I've also created a custom report only to track Downloads event category from Google's custom report option and can track it from that custom report, filtered by Downloads.
Screenshot of Custom Report settings
Screenshot of standard report and viewing from content menu
Useful links here: Link One and Link Two and Event Tracking Guide.
Update: Also remember it'll take some time after you setup all of those things to track and see the result.
Google Analytics on Steroids offers a great _gasTrackDownloads hook in their API if you don't already have the tracking code in place per #Sheikh's more accurate Custom Report answer above.
Code Example:
<script type="text/javascript">
var _gas = _gas || []; // _gas instead of _gaq
_gas.push(['_setAccount', 'UA-YYYYYY-Y']); // REPLACE WITH YOUR GA NUMBER
_gas.push(['_setDomainName', '.mydomain.com']); // REPLACE WITH YOUR DOMAIN
_gas.push(['_trackPageview']);
_gas.push(['_gasTrackForms']);
_gas.push(['_gasTrackOutboundLinks']);
_gas.push(['_gasTrackMaxScroll']);
_gas.push(['_gasTrackDownloads']); // track downloads
_gas.push(['_gasTrackYoutube', {force: true}]);
_gas.push(['_gasTrackVimeo', {force: true}]);
_gas.push(['_gasTrackMailto']);
(function() {
var ga = document.createElement('script');
ga.type = 'text/javascript';
ga.async = true;
ga.src = '//cdnjs.cloudflare.com/ajax/libs/gas/1.10.1/gas.min.js'; // new url
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(ga, s);
})();
</script>
Link: Google Analytics on Steroids

Concrete5 google analytics

How do i get google analytics to work
I've put it in site/settings - tracking code.
I made a google analytics account but it says 0 visited
Hope you guys can help
Best Regards
It takes one day before you will see the result in Google Analytics. It is not real time... If you placed the tracking code on your website you should be good to go.
Place the following script immediatly after your <body> tag.
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', '<<Your UA-000909 ID>>']);
_gaq.push(['_trackPageview']);
</script>
Place the following script immediately before your </body> tag.
<script type="text/javascript">
(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';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(ga);
})();
</script>
Between the scripts your content should be. This gives the best page load time.
You can test whether Google Analytics is configured correctly immediately after setup by utilizing the Google Analytics realtime view. In one browser tab visit your website, in another tab have Google Analytics open, with the real time tab selected (it can be found on the left hand side of the Google Analytics user interface under the reporting tab).
To install, grab your tracking code (it sounds like you've already done this) and paste before the closing tag.
You can also test whether you've configured Google Analytics correctly by selecting admin, then tracking info, then tracking code. If configured correctly, you should see the Status as "Status: Receiving Data".

Resources