Install Referrer for Android using Google Play Campaign Measurement


Install Referrer for Android using Google Play Campaign Measurement

After developing and publishing any Android app we go for analytics to see different statistics like installation & uninstallation, usage, user behaviour, cohort analysis, install referrer etc. using Google Analytics. Popular apps use to advertise or run digital campaign in order to grow installation rate. When a user installs a your app from play store via some campaign/advertisement you may want to see how they came to your app and which campaign worked for you. In this article we will discuss how to detect a new user coming to your app via campaign you ran and test it. We will achieve this using Google Play Campaign Attribution. It helps you to see which website or app or advertising tool referred other user to download your app.

SET UP INSTALL REFERRER

Before we move, we need to add Google services JSON (google-services.json) file in app folder in order to access Google services in you app. See this for referrence.

Now, to set up install referrer first you need to add Google Services to your android app. Go to your build.gradle(Project level) and add

And then add Google Analytics dependency to your app level build.gradle file.

After adding dependencies we need to set up our AndroidManifest.xml. See below for tested code of AndroidManifest.xml.

As you can see we have added CampaignTrackingReceiver with INSTALL_REFERRER intent. Play Store app sends data to this intent and here we are adding our receiver UtmReceiver that extends CampaignTrackingReceiver. Now take a look at the UtmReceiver class.

As you can find UtmReceiver extends CampaignTrackingReceiver to receive campaign data and find install referrer. We get the data via bundle extras with the key “referrer”. The inner block describes how to get values based on keys explained in the section below. After getting the values we save it to SharedPreferences to get the values letter in our app.

UNDERSTANDING THE PARAMETERS

We have already got the values against different keys from campaign data. Now we need to understand what are the keys and their purposes.
These keys comes with the Google Play Campaign URL. To see the url format you can check this Google Link and build your own url for campaign tracking.

install refer

Now look at the keys for which we get the values mentioned in the above image.

Next we will display the value we received via INSTALL REFERRER. So our MainActivity would look like

In the above code we fetched the values from SharedPreferences and displayed in a TextView.
Our programming part ends here. In the next section we will see how we test it.

TESTING INSTALL REFERRER CAMPAIGN

So far we have seen how to get campaign tracking attributes and decode them. Now it’s time to test the code running as expected. We may think How to test it without publishing app to Play Store?. Don’t worry! There is alternative way.

To test install referrer first we need to go to adb folder from terminal in Android Studio or the terminal your os provides. The path looks like ~/Android/Sdk/platform-tools/adb

To broadcast INSTALL_REFERRER intent make sure your app is installed but not running, not even in background.

Then issue the following command

The above command consists of values like utm_source, utm_medium etc. as given in Google Play URL builder in above section of this article. If the broadcast is successful you will get following message in return on the terminal

Now open your app and check for logcat. If campaign data received successfully, you will see a message like

Looking good? This is not so easy to get expected result at first chance if no luck. However, if it doesn’t receive any campaign data, you get a message similar like

If you fail to receive data, you can check Google’s Troubleshooting Guide and follow the steps.

If everything runs properly, you will be able to see it to your MainActivity screen. You can download full source code from GitHub here.

REFERRENCE : Google Play Campaign Attribution , Tesing Google Play Campaign Measurement

If you like the article, spread it then.


Author
Abhishek is a developer cum blogger working more than 4 years. He loves programming especially open stack technologies. He has decent knowledge in Android development, Wordpress, MongoDB, Node.js and so on. Beside this Abhishek finds himself busy in painting, front-end designing.

Follow Abhishek


Previous


detect new contact addition
Android : Detect new Contact addition

Next


ExoPlayer
ExoPlayer : Streaming remote video in Android