Search code examples
androidgoogle-analyticsgoogle-analytics-firebase

How can I set campaign with Google Analytics using setCampaignParamsFromUrl()


Update

This was caused by a bug that Google have fixed in "Release Version 4.5 - Google Play Services 7.3 (May 1, 2015)". When using Google Play Services 7.3 or later it should be possible to call setCampaignParametersFromUrl() with a full URL as expected.


Original question

I'm using Google Analytics V4 in my Android app. On startup I send a screen view hit to Google Analytics and I set the campaign parameters on my HitBuilders.ScreenViewBuilder by calling setCampaignParamsFromUrl() like this:

String url = "http://example.com/?referrer=utm_source%3Down-build%26utm_campaign%3Dinternal-testing";
builder.setCampaignParamsFromUrl(url); 

It seems like this is working because I can see that the campaign is included in the logs from Google Analytics:

V/GAV4﹕ Thread[GAThread,5,main]: Sending hit to service ...,  cn=internal-testing...

However when I'm looking at my data at the Google Analytics web interface the next day this campaign does not show up. I only have users with campaign "(not set)".

I've uninstalled my app, cleared the advertising id and re-installed the app and I see this installation as a new user in the Google Analytics web interface so I know the data is sent there. But the campaign I use does not show up.

Am I using setCampaignParamsFromUrl() wrongly or have I missed something else? Do I have to configure the campaigns I have somewhere or should Google Analytics pick up values it hasn't seen before as new campaigns automatically?

(As for why I'm not listening to the install referrer event see: What is the scope of the utm_campaign dimension in Google Analytics v4 on Android?)


Solution

  • Update: issue 596 have been fixed by Google, it is now possible to call setCampaignParamsFromUrl() with a URL when using the latest version of Google Play Services (7.3 released May 1st 2015)

    After some testing I managed to figure out what is going on and exactly what format the URL passed to setCampaignParamsFromUrl() should have.

    TL;DR: Generate an URL using the Google Play URL Builder, but pass only the referrer string (part after referrer=) to setCampaignParamsFromUrl().

    When setCampaignParamsFromUrl() is called it starts by taking everything after the '?' and splitting it on each '&' to get a list of all parameters it should send to GA.

    For each parameter it then splits on '=' and takes the first substring as parameter and the second as value for that parameter.

    Normally this works good, but it does not work for URLs generated by the Google Play URL builder. After un-escaping the parameter string of the URL used in the original question we get the string:

    referrer=utm_source=own-build&utm_campaign=internal-testing
    

    After splitting on '&' we get the following two strings

    1. referrer=utm_source=own-build
    2. utm_campaign=internal-testing
    

    The second can be handled nicely where utm_campaign gets assigned the value "internal-testing", but the first is problematic. There we get the parameter referrer with the value "utm_source". Since referrer is not a valid campaign parameter it is not reported to GA, and since utm_source is a required parameter GA will treat this campaign data as invalid and ignore all the other parameters reported at the same time.

    So to get setCampaignParamsFromUrl() to work you can pass in only the referrer string, that is everything after referrer= in the URL generated by the Google Play URL Builder. So the URL used in the original question should be

    utm_source%3Down-build%26utm_campaign%3Dinternal-testing
    

    The package name that is part of the URL generated by the URL builder is not needed at all since the GA SDK picks up the package automatically.

    Since this behavior feels like a bug to me this has been reported as issue 596 in the GA bug tracker.