google analytic, set and get new variable - google-analytics

I wonder how to add new variable to analytic when user visit page.
ga('set', 'userId', 'HP2202'); // Set the user ID using signed-in user_id.
ga('_setCustomVar',
1,
'dimension1',
'Member',
2 // Sets the scope to session-level. Optional parameter.
);
ga('send', 'pageview', {
'dimension1': 'SKU12345'
});
How could I know it success or not?
I using google analytic addon stylesheet to get report. where option I should add ga:dimension1 to ?

Related

google analytics not tracking custom dimension in context to events

I need to differentiate the users (Internal and External) who are performing some event action. so I am using the following code to track the user using custom dimension. My end goal is to know who performed the specific event by internal or external user
ga('send', 'event', 'Contract Change', 'click', 'Landing Page', 'My Value', {
'dimension3': 'External'
});
ga('send', 'event', 'Contract Change', 'click', 'Landing Page', 'My Value', {
'dimension3': 'Internal'
});
I have triggered this tracking code from javascript console for the testing purpose however it's not logging any data and I am not able to get the custom dimension value at any of the place.
I would change a couple of things:
Integers for event value: like Eike Pierstorff said, the event value has to be an integer. Technically you could leave the field out as it's optional, but it's best practice to assign values to your goal, so you can quantity the importance of user actions on your website:
https://developers.google.com/analytics/devguides/collection/analyticsjs/events#event_fields
Set dimensions before events: what you're doing should work, however it makes the code harder to read (most code formatting will break the {} syntax into 2 lines). Also, if you're sending multiple events which should be associated with the dimension, it's just cleaner to have the dimension declaration first, then all your events. Finally, if it's a user-scope dimension (internal vs. external users), it's more logical to declare the dimension before events, otherwise it looks like the dimension is hit-scope:
For instance:
ga('set', 'dimension3', 'Internal');
ga('send', 'event', 'Contract Change', 'click', 'Landing Page', 10);
ga('send', 'event', 'Other event with dimension', 'foo', 'bar', 20);

How do I set appVersion for Google Analytics Event tracking

When I try to set the appVersion in google analytics, then my event tracking stops working. Specifically, I'm trying to include the app version with event tracking so I can know which version of the app caused an event.
I've tried setting the app version like this:
ga('set', 'appVersion', app.version);
I've tried like this:
ga('send',
'event',
eventCategory,
eventAction,
{'page': pageName, 'appVersion' : app.version });
And I've also tried the alternative syntax:
ga('send',
{'hitType' : 'event',
'eventCategory' : eventCategory,
'eventAction' : eventAction,
'page' : pageName,
'appVersion' : app.version});
If I include appVersion, then event tracking stops working. No events show in realtime and no show the next day in the Behavior/Events section. The PageViews still work though.
As requested in the comments, I am editing to add in my event tracking code. It's been through several variations while I tried solve this problem. Here's what it looks like currently.
var app = {
trackEvent: function (pageName, eventCategory, eventAction, optionalEventLabel, optionalEventValue) {
var eventObject = {
'eventCategory' : eventCategory,
'eventAction' : eventAction,
'optionalEventLabel' : optionalEventLabel,
'optionalEventValue' : optionalEventValue,
'page' : pageName,
};
console.log("app.trackEvent - " + JSON.stringify(eventObject));
ga('send', 'event', eventObject);
}
}
I call this method from many places using a call like:
app.trackEvent("PageNameValue", "EventCategoryValue", "EventActionValue", "EventLabelValueIfIHaveOne", AnIntegerValueIfIHaveOne);
Any help or suggestions will be greatly appreciated.
Edit...
I found the following bug report that seems to apply: https://code.google.com/p/analytics-issues/issues/detail?id=366
The bug reporter mentions solving this problem by setting up a custom dimension. I will give that a try.
This appears to be be a Google Analytics bug. See https://code.google.com/p/analytics-issues/issues/detail?id=366 for more information.
As suggested by the bug reporter, the workaround is to use a custom dimension that you define in the Admin / Custom Definitions / Custom Dimensions section of the Google Analytics console.
Click "New Custom Dimension"
Enter name ( I entered customAppVersion )
Choose scope ( I chose Hit )
Click Create
Google will then suggest code examples for you, like...
var dimensionValue = 'SOME_DIMENSION_VALUE';
ga('set', 'dimension1', dimensionValue);
The only thing in the code sample that you need to change is the value of 'dimensionValue'. So I ended up with the following code.
ga('create', 'UA-########-#', 'auto');
ga('set', 'checkProtocolTask', null); // Disable file protocol checking (so that GA will work on Android devices)
ga('set', 'dimension1', app.version);
ga('send', 'pageview');
After this, the custom dimension will be applied to each hit recorded by Google Analytics and you can use that custom dimension to filter your results in the Google Analytics console.
As per google
Since the appName field must be sent with all app hits, it's often
best to set that field on the tracker itself using the set command or,
alternatively, when the tracker is created:
ga('create', 'UA-XXXXX-Y', 'auto', {
'appName': 'myAppName'
});
// The `appName` field is now set on the tracker, so
// screenview hits don't need to include it.
ga('send', 'screenview', {appVersion: '1.2'});
// Sending multiple parameters
ga('send', 'screenview', {appName: 'com.company.app', appVersion: '1.2'});
More information here
It works if you set at least the "appName", it's a good practice to set "appName" and "appId" before to set "appVersion"
ga('set', 'appId', app.id);
ga('set', 'appName', app.id);
ga('set', 'appVersion', app.version);

Google Analytics Single Application Issue

I have setup google analytics on a small 1 page application website with 3 sections. I have implemented page fields and tracker object. However, every page title in google analytics is coming across as "Contact" although I have designated page titles for each pageview as below:
ga('create', 'UA-50789730-1', 'auto');
ga('set', {
page: '/',
title: 'Home'
});
ga('set', {
page: '/#features',
title: 'Features'
});
ga('set', {
page: '/#download',
title: 'Download'
});
ga('set', {
page: '/#contact',
title: 'Contact'
});
ga('send', 'pageview', '/');
ga('send', 'pageview', '/#features');
ga('send', 'pageview', '/#download');
ga('send', 'pageview', '/#contact');
Am I doing something wrong here?
Thanks!
Whenever you call set you're overriding the previously set data. That's why "Contact" is all you're seeing for the title in the code shown above, since that set call overrides the previous two.
I'd recommend reading the analytics.js overview to get a better idea of how the underlying library works:
https://developers.google.com/analytics/devguides/collection/analyticsjs/
Then once you have a better understanding, you should read the guide on tracking single page applications, as there are some gotchas you should be aware of:
https://developers.google.com/analytics/devguides/collection/analyticsjs/single-page-applications

Hard code GA event with GTM

I want to be able to hard code some GA event such as below. I'm using GTM and I understand its not possible in this way. Is there a way round this?
ga('send', 'event', 'Mobile', 'Original', 'App');
This is a problem because GTM creates a randomly named tracker instead of the default tracker (t0). You can either use the set fields method on the "name" field to set the tracker name to a known values (i.e. "myTracker") and adapt your calls accordingly:
ga('myTracker.send', 'event', 'Mobile', 'Original', 'App');
Or you can use the ge function to send your event tracking calls to all trackers in the page:
ga(function() {
var trackers = ga.getAll();
for (var i=0; i<trackers.length; ++i) {
var tracker = trackers[i];
tracker.send('event', 'Mobile', 'Original', 'App');
}
});
which will probably create more headache than it's worth. However it is unlikely that there is a scenario that can't be covered without a need to hardcode events - the proper way would be to push a custom GTM event (and your GA event data) to the dataLayer and trigger an GA event tracking call from there.
So for hardcoding event, just don't.
To implement ga event use this syntax wit your onclick.
replace ga('send', 'event', 'Mobile', 'Original', 'App');
with this and it will work:
gtag('event', 'click', {'event_category' : 'Mobile',
'event_action' : 'Original', 'event_label' : 'App'});

google anlytics new version for setting custom variables

this is my new 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-000000-0', 'mysite.com');
ga('send', 'pageview');
</script>
I found in the analytics api, that I set a custom var like:
_gaq.push(['_setCustomVar',
1, // This custom var is set to slot #1. Required parameter.
'Items Removed', // The name acts as a kind of category for the user activity. Required parameter.
'Yes', // This value of the custom variable. Required parameter.
2 // Sets the scope to session-level. Optional parameter.
]);
but the variable "_gaq" is not defiend, and where do I place this code?
my analytics is just after the tag
Universal Analytics doesn't have the .push it has a ga() function.
Also there are no custom variables in Universal Analytics. In Universal Analytics we get custom dimensions and metrics.
Your custom variables are pretty much the same as the new custom dimensions.
The main difference is that they are set up in the GA interface instead of via arguments to the old .push function.
ga('set', 'dimension1', 'Yes');
Set up or edit custom dimensions & metrics

Resources