will there be a problem when importing a javascript code for Google Analytic tracking code.
if needed,heres my .js code
<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','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-xxxxxx-1', 'auto');
ga('send', 'pageview');
</script>
I have trouble in creating a new .js file for this. Please explain about this.
Related
I manage google analytics inside google tag manager and I don't have any separated script for google analytics in the head of my website.
I need to use ga() function like this:
<!-- 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','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-XXXXX-Y', 'auto');
</script>
<!-- End Google Analytics -->
See? It has created ga right after google analytics script. But as I said, I don't have any script for google analytics. I have just a script (which is located in the head of my website) from google tag manager:
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-{$google_tag_manager_token}');
ga('create', 'UA-{$google_analytics_token}', 'auto');
</script>
The problem is, it throws:
Uncaught ReferenceError: ga is not defined
Any idea where should I place ga('create', 'UA-{$google_analytics_token}', 'auto'); exactly?
It should be noted, when I run ga('create', 'UA-{$google_analytics_token}', 'auto'); in the console of the browser, it will be created as well. Do I need a settimeout() ?
By default, there is no analytics tag set up into the Google Tag Manager script.
You must configure into GTM a new tag for which you set up the type of the tag with "Google Analytics - Universal Analytics" and set up an event to launch this tag. "ga" function will then be available when GTM actually runs the tag.
One more information here, GTM send a hit with a prefix (generated tracker name). Sample:
ga('gtm1.send', 'pageview', '/page-name');
I want to implement Google analytics for an Angular SPA which is currently running in my localhost. I tried to copy the google tracking Js code to each of my view in the Angular SPA. But i don't think it actually worked.
Is this the correct way to implement Google Analytics or whats the correct way?
<div class="background">
<div class="container">
<h2 class="page-title">{{'contact-name' | translate}}</h2>
</div>
</div>
<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','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-XXXXXXXX-Y', 'auto');
ga('send', 'pageview');
</script>
Don't insert the script in each of your views, instead paste it within your main HTML page.
In addition to Google Analytics you might want to consider using Autotrack and the urlChangeTracker in your SPA
I am using the Google Analytics snippet below
<!-- 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','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-XXXXXXXX-X', 'icantreveal.com');
</script>
<!-- End Google Analytics -->
I am sending my events on say a button click as follows
//google analytics click event hit record
if(typeof(ga) == "function")
ga('send', 'event','int_lp_btn','int_lp_findPeople_btn');
I am using Google Analytics debug Chrome addon for testing purpose. However when I test the same in my site I can see in console
Executing Google Analytics commands. Running command: ga("send", "event", "int_lp_btn", "int_lp_findPeople_btn") Sent beacon:some ga stuff
However the event report in dashborad does not have anything to show!. I have been unable to figure out what the problem is. Any help will be appreciated. thanks.
Replace your google tracking ID and set page view as ga('send', 'pageview');
So I have a scenario where I am constantly getting a good number of self-referral traffic to my website. I investigated it further and believe it to be a case of an issue with my cross domain tracking (as I use a 3rd party shopping cart).
What I can't seem to figure out is what I have wrong and why it isn't working. My specific scenario looks like the following
MainSite.com Analytics Code
<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-XXXXX210-1', 'auto', {'allowLinker': true});
ga('require', 'linker');
ga('linker:autoLink', ['mainsite.com','checkoutsite.com'], true, true);
ga('require', 'displayfeatures');
ga('send', 'pageview');
</script>
CheckoutSite.com Analytics Code
<script type="text/javascript">
(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-XXXXX210-1', 'auto', {
'allowLinker': true
});
ga('require', 'ec');
</script>
As for the setup options:
The following domains are added to the exclusion referral list in GA
mainsite.com
checkoutsite.com (the actual checkout is on a subdomain of kiosk.checkoutsite.com but this shouldn't make a difference AFAIK)
mainsite.com 301 redirects to www.mainsite.com
As far as I can tell the 3rd party checkout JS script is adding link decorators as seems to be required Edit: Have confirmed as per the comments below
The links to the checkout cart are Javascript based which result in a iframe popover. They look like the follwing:
<a class="eztixKioskLink tourTemplateId_XXXX" href="javascript:void(0);">
<img alt="" src="http://www.mainsite.com/buy.png" />
</a>
However, I still appear to be getting self-referrals and am completely unable to figure out why. Is there something wrong with my code or my setup? Is there something else I should be looking for?
Any help would be hugely appreciated!
You should be good code wise. You can also exclude those domains as referrers:
https://developers.google.com/analytics/devguides/collection/analyticsjs/cross-domain#selfreferrals
As you go through the flow yourself do you see #_ga hash's being appended? If not you'll need to reverse engineer the javascript link clicks as the outgoing clicks aren't getting decorated:
https://developers.google.com/analytics/devguides/collection/analyticsjs/cross-domain#linkerparam
It is the first time i am using the new version of Google Analytics (Universal Analytics), so when i put the below code imediatally before </head> i get blank pages for all my URLs.
Here is the code given by Google:
<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-1234567-89', 'mysite.com');
ga('send', 'pageview');
</script>
Someone knows why it is happening? Seems to be a bug, but i don't know how to solve this.
Based on #Brad comment i detected that PHP Smarty was crashing with Universal Analytics code tracking, so i found this post:
Use {Literal} Smarty Tag to Add Google Analytics Tracking Code to BlogSome Hosted Blogs
And then i did like below and the tracking code works perfectly!
<script>
{literal}
(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-1234567-89', 'mysite.com');
ga('send', 'pageview');
{/literal}
</script>