How do I set appVersion for Google Analytics Event tracking
Asked Answered
Q

3

10

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.

Quire answered 8/4, 2016 at 19:54 Comment(5)
Both event formats look to be fine. You might want to set the event Label with the appVersion for convenience. Note that if you set the app Version with set, then it applies to all hits on the page. Also if you set it, then you don't need to do it again in the event hit (so just pick one method, not both). Usual debugging steps: check with GA Debugger, check console hits, make sure no filter set.Forwardlooking
Thanks for the reply nyuen. Thanks for confirming the behavior of set - that seems preferable to me. I did install the GA debugger plugin and turned it on, but I didn't see any difference in the console output. I may not being looking in the correct place for that. Ctrl+Shift+J to bring up the chrome developer console right? When I looked at the Network tab of the console, I could see GA event requests finishing with a status code 200 which should indicate success, but as I said event tracking would stop working completely. I did notice my initial request was redirected (status code 307)Quire
The idea about setting the eventLabel with the version number would be a workaround I guess. I'd like to get it working with actual appVersion so that I could use it in filtering the eventCategory as a secondary dimension.Quire
I'm not sure why the events would stop working. Can you include the code for your subsequent events (ie. the events that should fire after the successful one)?Forwardlooking
The trackEvent code above is currently working. But if I add 'appVersion' : '1.0' to the send event calls then it stops working.Quire
Q
7

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.

  1. Click "New Custom Dimension"
  2. Enter name ( I entered customAppVersion )
  3. Choose scope ( I chose Hit )
  4. 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.

Quire answered 11/4, 2016 at 14:4 Comment(0)
R
3

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

Ruthi answered 10/4, 2016 at 3:43 Comment(4)
Thanks for the reply. Unfortunately, I am trying to set appVersion, not appName. I haven't tried it yet, but the bug report I found says that setting appName would also cause event tracking to fail.Quire
Hi, request to mark it as a correct answer if it does solve your purpose so that others can also be benefited. Many ThanksRuthi
I tried setting appVersion at tracker creation time as you suggested and I got mixed results. Event tracking continued to work, but the recorded events did not have the expected appVersion information.Quire
Can you test using this ga debugger if the request is not going from the client? This tool is available for Chrome as an extension. Sometimes, problem is on the filtered view on the reporting side? chrome.google.com/webstore/detail/google-analytics-debugger/…Ruthi
G
1

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);
Gati answered 10/5, 2019 at 16:38 Comment(2)
The order is not important.Bleb
👍 .. that's true!Gati

© 2022 - 2024 — McMap. All rights reserved.