I'm trying to use this code to track an event in Google Analytics
_trackEvent(category, action, opt_label, opt_value, opt_noninteraction)
This seems to be meant to be use with the GA.js Analytics "package", however I'm using the Analytics.js
Like this
(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');
Is there a way I can do even tracking with this code only ?
Or do I have to use
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);
})();
Do I need both of these codes?
Or is there some different way using only Analytics.js
Thanks in advance
**EDIT:
Indeed I probably didn't explain what I want, it might not even possible.
I want to work with Funnels on Events, and I want to use _trackEvent (that's what I thought it would do) to mark a user as having Entered the Funnel, if the event is send then we will have the normal funnel.
So if 10 users enter the page and 1 clicks on the button, I would have 10 events on the Funnel with 1 success
analytics.js will report to GA same as the old code, but it has different syntax, so you can't use _trackEvent.
Here is a link to the basic on-page syntax with analytics.js, and here is a link for event tracking with analytics.js
<!-- 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-XXXX-Y'); // GA account ID goes here
ga('send', {
'hitType': 'event', // Required.
'eventCategory': 'category', // Required.
'eventAction': 'action', // Required.
'eventLabel': 'opt_label',
'eventValue': opt_value,
'nonInteraction': opt_noninteraction
});
</script>
<!-- End Google Analytics -->
Related
Looking at the GA developers page at https://developers.google.com/analytics/devguides/collection/gajs/eventTrackerGuide it says the tracking code is:
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);
})();
But when I start an account for my site GA gives me the tracking code:
(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-XXXXX-1', 'auto');
ga('send', 'pageview');
Why are these completely different?
Thanks
The first code snippet is for the old/traditional Google Analytics tracking script.
The second one uses the newer universal Google Analytics tracking script.
The reason why they are different is probably because Google is trying to push the newer tracking script, but haven't updated all of their documentation.
The analytics.js JavaScript library is built on Universal Analytics technology. If you’re using a tracking code snippet on your website that references analytics.js, you’re using Universal Analytics.
If you’re using any other library, like ga.js, you’re using a Classic Analytics tracking technology, and should upgrade your tracking code (if you also have a Universal Analytics property).
Reference
I got a website builder app where users can create their own sites.
Each customer points his domain to the app's ip which has its own UA code to collect data and show page views statistics in the back office. In addition, if customer got his own Google Analytics account he may indicate it and start tracking data.
The current frontend ga.js code looks like this:
<script type="text/javascript">
var _gaq = _gaq || [];
/* app UA code */
_gaq.push(['x._setAccount', 'UA-XXXXXXX']);
_gaq.push(['x._setDomainName', 'customersdomain.com']);
_gaq.push(['x._setAllowLinker', true]);
_gaq.push(['x._trackPageview']);
/* customer's UA code */
_gaq.push(['_setAccount', 'UA-YYYYYY']);
_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>
Now I need to migrate to Universal Analytics but I am not sure what code should I use. I've done plenty of searches but I am still doubting. Would the following code work?
<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-XXXXXXX', 'customersdomain.com');
ga('create', 'UA-YYYYYYY', 'customersdomain.com');
ga('send', 'pageview');
</script>
You need a named tracker. You can set this up in the configuration object that can be passed as the third parameter instead of a cookie domain (in that case the cookieDomain setting goes into the configuration object). Plus you need two send pageview calls, one for each tracker.
<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-XXXXXXX', {
'name' : 'mycustomtracker',
'cookieDomain' : 'customersdomain.com'
});
ga('create', 'UA-YYYYYYY', 'customersdomain.com');
ga('mycustomtracker.send', 'pageview');
ga('send', 'pageview');
</script>
I would like to migrate to universal analytics.js, however when I try to implement multiple accounts in the analytics.js snippet, the default account works great and shows up in Real Time, however the subsequent account never shows up on Real Time and traffic drops off to 0.
Currently I'm using ga.js to track multiple accounts in the single snippet. Here is the format I am using for that:
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(
['_setAccount', 'UA-XXXXXXXX-2X'],
['_setDomainName', 'example.com'],
['_addIgnoredRef', 'example.com'],
['_trackPageview'],
['b._setAccount', 'UA-YYYYYYYY-2Y'],
['b._setDomainName', 'example.com'],
['b._addIgnoredRef', 'example.com'],
['b._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>
Below is the snippet I have been trying to make work with analytics.js
<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-XXXXXXXX-2X', 'example.com');
ga('send', 'pageview');
ga('create', 'UA-YYYYYYYY-2Y', {'name': 'rollup'});
ga('rollup.send', 'pageview');
</script>
The "rollup" name space never registers on the rollup account in real time. The UA-XXXXXXXX-2X code registers just fine in Real Time.
I have run Google Analytics Debugger and it appears to be creating both accounts and sending just fine with no errors only info that looks appropriate:
Running command: ga(create, UA-YYYYYYYY-2Y, [object Object])
Creating new tracker: rollup
Running command: ga(rollup.send, pageview)
Could anyone offer some guidance on what might be the problem with my analytics.js snippet?
Here is the Google Universal Analytics Code that merges two Profiles and even sends demographic data.
Just replace - UA-XXXXXX-Y with UA-XXXXXX-X with your GA tracking IDs.
and replace EXAMPLE.com with the website domain and all set to go.
FYI - OldSite - you can replace it with whatever you like - its just used separate profile for the GA to send captured data. so you can use SecondProfile, IloveCodes or whatever.
This script additionally sends demographics and other data - how cool is that?
Enjoy!
And Yes thanks to all the guys out there who helped me reaching out to this answer.
<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-XXXXXX-Y', 'EXAMPLE.com');
ga('create', 'UA-XXXXX-X', {'name':'Oldsite'});
ga('send', 'displayfeatures');
ga('Oldsite.send', 'displayfeatures');
ga('send', 'pageview');
ga('Oldsite.send', 'pageview');
</script>
If you are also experiencing this, ensure that all the accounts you are attempting to track have already been upgraded to Universal Analytics via the GA Admin menu. A face palm moment for me, but perhaps this reminder will help anyone else who may be experiencing the same issue.
In my property's Tracking Code section I see this message:
Status: Tracking Not Installed Last checked: Oct ...
The Google Analytics tracking code has not been detected on your website's home
page. For Analytics to function, you or your web administrator must
add the code to each page of your website.
The tracking code shown to be copy/pasted is this:
<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-XXXXXXx', 'example.com');
ga('send', 'pageview');
</script>
There's a learn more button and when I click it it takes me to here which shows the asynchronous code, which is something like this:
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXX-Y']);
_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>
Is this the current/latest version of the GA code? Why is the dashboard (i.e. the Tracking Code section) is not showing this by default? Is there a way to customize the Google Analytics to show this code in the Tracking Code section, prepared for my property (website)?
What you are seeing first is the latest version of Google Analytics known as Universal Analytics. As per Google recommendation , use that for all your new analytics properties. This is identified by the analytics.js library in the tracking code
The second one is an earlier version of Google Analytics based on ga.js library.
Use the first version of tracking code given by GA and follow this tutorial based on analytics.js to debug your code.
How do i use both for an existing google acc that has been using ga.js...i cant seem to find the right documentation. anyone with experience in this?
To expound what Pete says.
Create a new web property for analytics.js
- In your google analytics account, go to Admin section and Create a new web property
- Creating a new property will provide you with a new Tracking ID
Dual Tag
- Add both your ga.js and analytics.js tracking code. Note that they must not share the same UA code. Your codes may look something like below:
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-xxxxx-y']);
_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>
<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-xxxxx-z', 'yoursite.com');
ga('send', 'pageview');
</script>
Existing ga.js users should create a new web property for analytics.js and dual tag their site. It is perfectly safe to include both ga.js and analytics.js snippets on the same page.
see
https://developers.google.com/analytics/devguides/collection/analyticsjs/
The ga.js code will be obsolete in a couple of years, i suggest you migrate to Universal analytics. The initial phase has probably started by non.