How To Test Install Referrer Android
April 30, 2018
3.68K VIEWS
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
//............ //............ buildscript { repositories { google ( ) jcenter ( ) } dependencies { classpath 'com.android.tools.build:gradle:3.0.1' classpath 'com.google.gms:google-services:3.0.0' } } //............ //............ |
And then add Google Analytics dependency to your app level build.gradle file.
//............ //............ dependencies { implementation fileTree ( dir : 'libs' , include : [ '*.jar' ] ) implementation 'com.android.support:appcompat-v7:27.0.2' compile 'com.google.android.gms:play-services-analytics:11.8.0' } apply plugin : 'com.google.gms.google-services' |
After adding dependencies we need to set up our AndroidManifest.xml. See below for tested code of AndroidManifest.xml.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | <? xml version="1.0" encoding="utf-8" ?> <manifest xmlns: android="http://schemas.android.com/apk/res/android" package="com.digitstory.trackcampaign" > <uses-permission android: name="android.permission.INTERNET" /> <uses-permission android: name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android: name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android: name="android.permission.WAKE_LOCK" /> <meta-data android: name="com.google.android.gms.version" android: value="@integer/google_play_services_version" /> <application android: allowBackup="true" android: icon="@mipmap/ic_launcher" android: label="@string/app_name" android: roundIcon="@mipmap/ic_launcher_round" android: supportsRtl="true" android: theme="@style/AppTheme" > <activity android: name=".MainActivity" > <intent-filter> <action android: name="android.intent.action.MAIN" /> <category android: name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <!-- Used for Google Play Store Campaign Measurement--> <service android: name="com.google.android.gms.analytics.CampaignTrackingService" android: permission="android.permission.WAKE_LOCK" android: enabled="true" android: exported="true" > </service> <receiver android: name="com.google.android.gms.analytics.CampaignTrackingReceiver" android: enabled="true" android: exported="true" android: permission="android.permission.INSTALL_PACKAGES" > <intent-filter> <action android: name="com.android.vending.INSTALL_REFERRER" /> </intent-filter> </receiver> <receiver android: name=".UtmReceiver" android: enabled="true" android: exported="true" android: permission="android.permission.INSTALL_PACKAGES" > <intent-filter> <action android: name="com.android.vending.INSTALL_REFERRER" /> </intent-filter> </receiver> <receiver android: name="com.google.android.gms.analytics.AnalyticsReceiver" android: enabled="true" > <intent-filter> <action android: name="com.google.android.gms.analytics.ANALYTICS_DISPATCH" /> </intent-filter> </receiver> <service android: name="com.google.android.gms.analytics.AnalyticsService" android: enabled="true" android: exported="false" /> </application> </manifest> |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | package com . digitstory . trackcampaign ; import android . content . Context ; import android . content . Intent ; import android . content . SharedPreferences ; import android . os . Bundle ; import android . util . Log ; import com . google . android . gms . analytics . CampaignTrackingReceiver ; public class UtmReceiver extends CampaignTrackingReceiver { String utm_source , utm_medium , utm_term , utm_content , utm_campaign ; @Override public void onReceive ( Context context , Intent intent ) { try { Bundle extra = intent . getExtras ( ) ; if ( extra !=null ) { String refer= extra . getString ( "referrer" ) ; if ( refer !=null ) { String referrer=refer . substring ( 0 , refer . length ( ) ) ; String referSet [ ]=referrer . split ( "&" ) ; if ( referSet . length > 0 ) { for ( String item : referSet ) { String itemSet [ ]=item . split ( "=" ) ; if ( itemSet . length==2 ) { String key=itemSet [ 0 ] ; String val=itemSet [ 1 ] ; Log . d ( "UTM_TEST" , key+"-"+val ) ; if ( key !=null ) { switch ( key ) { case "utm_source" : utm_source=val ; break ; case "utm_medium" : utm_medium=val ; break ; case "utm_term" : utm_term=val ; break ; case "utm_content" : utm_content=val ; break ; case "utm_campaign" : utm_campaign=val ; break ; } //Saving values in shared preference //for fetching later saveUtmValues ( context , key , val ) ; } } } } } } } catch ( Exception e ) { e . printStackTrace ( ) ; } } public void saveUtmValues ( Context context , String key , String val ) { try { SharedPreferences preference = context . getSharedPreferences ( "utm_campaign" , Context . MODE_PRIVATE ) ; SharedPreferences . Editor editor = preference . edit ( ) ; editor . putString ( key , val ) ; editor . apply ( ) ; } catch ( Exception e ) { e . printStackTrace ( ) ; } } } |
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.
Now look at the keys for which we get the values mentioned in the above image.
utm_source - Campaign Source utm_medium - Campaign Medium utm_term - Campaign Term utm_content - Campaign Content utm_campaign - Campaign Name |
Next we will display the value we received via INSTALL REFERRER. So our MainActivity would look like
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | package com . digitstory . trackcampaign ; import android . content . Context ; import android . content . SharedPreferences ; import android . support . v7 . app . AppCompatActivity ; import android . os . Bundle ; import android . widget . TextView ; public class MainActivity extends AppCompatActivity { private TextView textView ; @Override protected void onCreate ( Bundle savedInstanceState ) { super . onCreate ( savedInstanceState ) ; setContentView ( R . layout . activity_main ) ; textView=( TextView ) findViewById ( R . id . textView ) ; String utmValues= "utm_source : "+getUtmValues ( this , "utm_source" )+"\n"+ "utm_medium : "+getUtmValues ( this , "utm_medium" )+"\n"+ "utm_term : "+getUtmValues ( this , "utm_term" )+"\n"+ "utm_content : "+getUtmValues ( this , "utm_content" )+"\n"+ "utm_campaign : "+getUtmValues ( this , "utm_campaign" )+"\n" ; textView . setText ( utmValues ) ; } //Get values by key public String getUtmValues ( Context context , String key ) { String val=null ; try { SharedPreferences preferences = context . getSharedPreferences ( "utm_campaign" , Context . MODE_PRIVATE ) ; val = preferences . getString ( key , "null" ) ; } catch ( Exception e ) { e . printStackTrace ( ) ; } return val ; } } |
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
echo 'am broadcast \ -a com.android.vending.INSTALL_REFERRER \ -n "com.digitstory.trackcampaign/com.digitstory.trackcampaign.UtmReceiver" \ --es "referrer" \ "utm_source=myapp&utm_medium=email&utm_term=install_refer&utm_content=install_refer&utm_campaign=install_refer"; \ exit' | ./adb shell |
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
Broadcasting : Intent { act=com . android . vending . INSTALL_REFERRER cmp=com . digitstory . trackcampaign/com . google . analytics . tracking . android . AnalyticsReceiver ( has extras ) } Broadcast completed : result=0 |
Now open your app and check for logcat. If campaign data received successfully, you will see a message like
GAv4 : Received installation campaign : source=myapp |
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
Thread [ GAThread , 5 , main ] : No campaign data found . |
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.
How To Test Install Referrer Android
Source: http://www.digitstory.com/install-referrer-android-campaign-track/
Posted by: shepherdcousemen.blogspot.com
0 Response to "How To Test Install Referrer Android"
Post a Comment