How do I set appVersion for Google Analytics Event tracking - google-analytics

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

Related

google analytic, set and get new variable

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 ?

Event tracking using Google Analysis

I'm adding a code on the Complete instance of a video played using jwplayer for analysis using google analytics event tracking. There I need to overwrite the base URL that is sent in the analysis code. Could you please help me with the exact parameter that I need to send for that. Below is the code I'm using to send the request to Google Analytics:
ga('send', 'event', 'Vedio', 'play', 'vedio_name');
Use a configuration object to set the parameters and include the page parameter with a page path as value:
ga('send', {
hitType: 'event',
eventCategory: 'Videos',
eventAction: 'play',
eventLabel: 'Fall Campaign',
page: '/my/custom/pagepath/'
});

add Meteor DocHead => DocHead.loadScript with Flow-router-ssr.

Can't figure out how to add the analytics code using DocHead.loadScript . Documentation assume that you more or less already understand it.
Have been trying in component, Client etc. Manage to get DocHead.setTitle(title); etc to work without problem.
Where do i add this DocHead.loadScript to make it work?
var gaScript = 'https://www.google-analytics.com/analytics.js';
DocHead.loadScript(gaScript, function() {
// Google Analytics loaded
ga('create', 'UA-XXXX-Y', 'auto');
ga('send', 'pageview');
});
Github: https://github.com/kadirahq/meteor-dochead

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

Resources