Google Analytics custom user properties - javascript

I have created an app for SharePoint Online that adds Google Analytics to our site and all it's pages to bridge the gap that you cannot pull this data via audit reports like an on premises installation. I can see data flowing into analytics without an issue. The problem comes in when I'm looking for the user information in Analytics. I have set up a custom dimension and a custom metric, but I cannot see the data. I know it has to be something on the setup as the other generic data is working fine.
This is the setup:
Custom Dimension
Custom Metric
Analytics code
ga('create', 'UA-xxxxxxx-y', 'auto');
ga('set', {
'dimension1': _spPageContextInfo.userLoginName,
'metric1': 1
});
ga('send', 'pageview');
Call to analytics:
https://www.google-analytics.com/r/collect?v=1&_v=j40&a=673549611&t=pageview&_s=1&dl=https%3A%2F%2Ftenant.sharepoint.com%2Fsites%2Fsite%2FSitePages%2FHome.aspx&ul=en-us&de=UTF-8&dt=Home&sd=24-bit&sr=1920x1080&vp=1610x991&je=0&fl=19.0%20r0&_u=QACAAEABI~&jid=369651474&cid=2614006.1448315161&tid=UA-xxxxxxx-y&_r=1&cd1=email.address%40domain.com&cm1=1&z=61973351
So I can see it is sending over the data correctly, I can't understand how/why/where the configuration within Analytics is to see this data.

Ultimately I ended up adapting my code to log all the things I could think of.
ga('set', {'dimension1': _spPageContextInfo.userLoginName, 'metric1': 1, '&uid': _spPageContextInfo.userLoginName});
Multiple iterations over several days did not start logging data.

Related

How to send EVENTS in addition to pageviews using a Java Script GTM tag which is tracking Individual Logged in Users using Google Analytics?

I have used a JavaScript tag which is tracking Individual Logged in Users using Google Analytics. It has a statement "ga('send', 'pageview');", which results in showing the pages a particular user has viewed, in user explorer of Google Analytics. I want to send events (I have already created in GTM) along with pageviews with reference to User-ID i.e., shKey in my case.
Here is the code in my JS 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','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-xxxxxxxxx-x' , 'auto');
ga('set', 'userId', arguments.shKey);
ga('set', 'dimension1', arguments.fname);
ga('set', 'dimension2', arguments.type);
ga('send', 'pageview');
ga('send', 'event');
</script>
You have 2 issues: your code won't send an event (you're missing parameters), and you need to address PII (see further below)
This is how you send an event programmatically:
https://developers.google.com/analytics/devguides/collection/analyticsjs/events
ga('send', 'event', 'My Category', 'My Action', 'My Label');
However if you're using GTM, you might want to look into how to handle events with GTM:
https://developers.google.com/tag-manager/devguide
// You need to configure GTM UI to handle below event as desired
dataLayer.push({'event': 'event_name'});
Regarding PII, this is the authoritative answer from Google:
https://support.google.com/analytics/answer/6366371?hl=en
Google policies mandate that no data be passed to Google that Google
could use or recognize as personally identifiable information (PII).
PII includes, but is not limited to, information such as email
addresses, personal mobile numbers, and social security numbers
If you want to use PII, it must be encrypted before being sent, from same link as above:
You can send Google Analytics an encrypted identifier or custom
dimension that is based on PII, as long as you use the proper
encryption level. Google has a minimum hashing requirement of SHA256
On top of that there might be additional legal restrictions depending on the jurisdiction you and your customers are in (eg in Europe you would have GDPR).
Your ga('send', 'event') has a wrong signature. Events require category and action. So your event should look something like: ga('send', 'event', 'category', 'action', 'label');
You're doing it strategically wrong. You're solving through JS what is supposed to be solved via GTM UI. In short term, it may seem easier and more elegant to do it in JS. In long term, however, it makes GTM obsolete and increases your technical debt, so the implementation becomes really difficult to maintain.

Massive differences between Google Analytics and own data collection

The use of a web app is to be evaluated statistically. It has been publicly available since spring of this year.
The web app is linked to Google Analytics. The following is done for the own user data collection:
A Unique User ID is created when the web app is called for the first time. It is stored in the localStorage and is compared each time the page is called up again.
if (localStorage.getItem("uuid") === null) {
localStorage.setItem("uuid", get_uuid());
}
function get_uuid() {
return ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, c =>
(c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)
)
}
This data is written to a database together with other information (concrete page, time, device type, etc.). Users without Javascript or localStorage will not be included; however, they will probably not be able to use the web app correctly anyway.
If I now compare the data from Google Analytics with my own variant, the discrepancy is considerable.
Different users according to Google: about 900
Different users due to UUID: about 400
Additionally about 100 visits (or interactions) without UUID were registered.
Now my question is why these big differences exist. In my opinion, my data collection should be pretty accurate. But maybe I have a thinking error with the approach of the UUID? Or could it be that Google counts quite differently; for example, any robots that don't leave a UUID behind?
Thank you very much for your answers and considerations.
I'm quite sure you have encountered Google Analytics (GA) spam.
This is because GA is JavaScript and your ID is listed in the html source.
So anyone who wants to create spam on your data can use your ID.
Why you ask... When you notice it you see that there are webpages listed you don't know in your GA data, you (the admin) open them and get a virus or worse.
Don't open the webpages...
There are as far as I know two ways to fix it. Regex filter wich is a common way.
All webpages that has refferals from other domains you don't "know" you need to block.
This takes time and is not a good approach.
My method is to pass a dimension from the html to GA.
If that dimension is missing the data is not real.
Your JavaScript probably looks something like:
.....
ga('require', 'linkid', 'linkid.js');
ga('require', 'displayfeatures');
ga('send', 'pageview');
</script>
If we add a dimension which we pick up in GA admin tools
.....
ga('require', 'linkid', 'linkid.js');
ga('require', 'displayfeatures');
ga('send', 'pageview', {
'dimension1': 'FooBar'
});
</script>
Go to admin -> Property (the middle column) and at the bottom you have Dd Custom Definitions.
Open Custom Dimensions and add the dimension you added to the html.
Now you can set up a filter in the view tab of GA admin to only show data with your custom dimension "FooBar".
Any data that does not have this "FooBar" is spam that is not generated from your webpage.
Just remember you need to change all GA JavaScript codes and add the dimension.
You can see this spam (if I'm correct) in the Acquisition -> All Traffic -> Referrals report.
If you see Sources that you don't recognize and looks odd it's most likely the spam.
Before I used this method my Referrals looked something like this, there is about 50 of these fake referrals.

Listen for Google Analytics hit using JavaScript

I am using Google Tag Manager and want to fire a tag when a hardcoded pageview hit is sent to Google Analytics.
Is it possible to listen for hits being sent to Google Analytics using JavaScript?
If you want to make sure that some code is executed when GA sends it's data you can use tasks in GA - specifically the custom task, which does nothing by default and is there so you can add custom behaviour to GA.
A task is a javascript callback that performs certain actions during a tracking call (checking if there is a cookie, setting the page speed sample, checking if the payload data is correct etc.).Google introduced the custom task as a custom callback basically for scenarios like yours.
Very much from the top of my hat this would look something like this:
ga('create', 'UA-XXXXX-Y', 'auto');
ga('set', 'customTask', function(model) {
page = model.get('page');
if(page.indexOf('pageview/where/i/want/my/pixel') {
trackingUrl = "/path/to/pixel";
trackingPixel = new Image();
trackingPixel.src= trackingUrl;
}
});
(untested, might need tweaking).
But the idea is that a task is only executed when GA sends a tracking call, so if you want to tie your tag to the GA call that's the way to go.

Google Analytics multiple tracking on a single web page

I recently get a partnership with a website, in order to put my Universal Analytics tracking code on a single web page. The website's owner has already one.
I've searched on Google Developers and Stack to find how to use multiple tracking codes. I built this code but I'm not sure whether it's OK, I don't want to interfere with his data. I just want to get common analytics data in my own account.
<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');
//1- Create default tracker and mytracker
ga('create', 'UA-XXXXX-1', 'auto'); // His default tracker
ga('create', 'UA-XXXXX-50', 'auto', 'mytracker'); // My tracker
//2- Get the pageview data of mytracker
ga(function(){
var nonDefaultTracker = ga.getByName('mytracker');
var mytrackerPageview = nonDefaultTracker.get('pageview');
console.log(mytrackerPageview);
});
//3- Update mytracker pageview data
ga('mytracker.set','pageview');
//4- Send mytracker and default tracker pageview data
ga('send','pageview');
ga('mytracker.send','pageview');
</script>
Someone already did this ?
Thanks!
You do not need step 2 and 3. The get method of the tracker returns the field specified in the argument, and since GA does not have a data field called pageview this will be undefined (which is the value you set as page path - you probably do not want this).
However pageview tracking will work fine without trying to retrieve values from the previous tracker. Unless you have set fields specifically set for a named tracker both will report the same thing.

Google analytics page tracking is not working properly in angularjs app

My project is on angularjs, which is both for mobile app and desktop site.
I have saved analytics.js on my local and used it in GA tracking code.
My tracking code is in index.html file:
(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','js/analytics.js','ga');
ga('create', 'UA-57325467-1', 'auto');
ga('set', 'checkStorageTask', null);
ga('set', 'checkProtocolTask', null);
ga('send', 'pageview');
In cotroller , I have used following code for page tracking:
ga('send', 'pageview', $location.url());
It's showing 1 user online(that's me).It's tracking events on all pages.But problem is with pageview.
When i am visiting different pages of my application and checking in goolge analytics real time->overview
I am seeing "/" there ,whereas when I am coming back to the pages then page tracking is working fine(but not always).
Basically it's not tracking pages all the time.Sometimes it works and sometimes I see only "/".
For using google analytics in angular projects I recommend using the angular module.
It does what you want automatically.
I know it is not an direct answer to your question, but I thought instead of debugging yourself you might use a unit tested library.
Ensure that have enabled the Real time Analytics. Follow the steps in this reference document to enable Real time analytics
https://support.google.com/analytics/answer/1638635?hl=en
Other Reasons you are data may not be shown are
Data collection limit:
If a property sends more hits per month to Analytics than allowed by the Analytics Terms of Service,
there is no assurance that the excess hits will be processed. If the property's hit volume exceeds this limit,
a warning may be displayed in the user interface and you may be prevented from accessing reports.
Data processing latency:
Processing latency is 24-48 hours. Standard accounts that send more than 200,000 sessions per day to Analytics will result in the reports being refreshed only once a day.
This can delay updates to reports and metrics for up to two days.
To restore intra-day processing, reduce the number of sessions your account sends to < 200,000 per day.
Reference:
https://support.google.com/analytics/answer/1070983?hl=en

Categories

Resources