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

冷暖自知 提交于 2019-12-05 19:44:13

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.

Try building your URL with the form provided at the end of the Campaigns dev guide: https://developers.google.com/analytics/devguides/collection/android/v4/campaigns

Also, make sure you don't have sampling enabled. If you are testing this from a single device/emulator and you have sampling enabled your campaign data might be ignored.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!