Implement Google Analytics for Angular1.5 App - javascript

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

Related

Google Analytics simple client

I am trying to integrate GA into a QT5 / QML project. For this I created a dummy GA web page project to test connectivity. First step is to test the setup with a simple web page (code below).
I had to specify a URL for the GA project. The page, however, is on my desktop, not online. I see no traffic in the real time GA view. Not sure why. Is it that my page is not on www.my-domain.com but literally on my desktop?
SETUP
On the GA settings page, this is my setup
Basic Settings
Tracking Id
UA-1234567890-1
Property Name
Default URL
http://www.my-domain.com
LOCAL TEST PAGE CODE
<html>
<head>
<!-- Global site tag (gtag.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','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-1234567890-1', 'auto');
ga('send', 'TEST');
console.log(ga.q);
</script>
</head>
<body>
<button onclick="ga('send', 'BUTTON PUSH')">
PUSH ME
</button>
</body>
</html>
I have used GA before and do not remember the integration was this bumpy. Right now, I am also confused by the difference of GA view and Firebase view on the live report. However, I first need to establish proper tracking.

Google Analytics not working with Barbajs

Trying to use the latest google analytics tracking code in a site that uses Barba JS but it will not track past the first page, ie the path of the user, any ideas anybody?
The reason it is only working on the initial page is that the default GA snippet implemented using gtag.js only runs once after load. Since you're using it in a SPA setting, you need to manually re-send the page data when you route. Looking at the barbajs documentation, I think it'll be something like this:
// define a global hook, using afterEnter, but you can adjust to enter, beforeEnter, etc as you see fit.
barba.hooks.afterEnter((data) => {
// this hook will be called for each transitions
//data.current.url is the page path.
gtag('config', 'UA-#######-1', {'page_path': data.current.url});
});
use full code provide with google annalitics
https://developers.google.com/analytics/devguides/collection/analyticsjs/?hl=en
in you JS add
(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');
ga('send', 'pageview');

Google Analytic's events sent not showing up in dashboard

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');

Cross Domain Tracking in Google Universal Analytics with Javascript Links

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

Google Universal Analytics tracking code generating an empty page

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>

Categories