Last updated on March 13th, 2020 at 11:47 am
Google Play Campaign Measurement allows you to see which campaigns and traffic sources are sending users to download your app from the Google Play Store. In this post i am going to explain you how to create campaign and read data after app is install. Today most of app which implements referral system hey use this technique. Measuring campaigns in Google analytic enables the attribution of campaigns and traffic sources to user activity within your application. When you download app from referral link and your download is complete then Play Store app broadcasts an INSTALL_REFERRER
intent to your app during installation. After installation of app you get your campaign data in intent.
Note: To test the app referral system you have two method one by command and for second one you need to publish your app on google play store.
Step to test live demo
Step 1. Open this link in your mobile device.
https://play.google.com/store/apps/details?id=com.trinitytutscampaigns&referrer=utm_source%3D123456%26utm_medium%3Dtrinity
Step 2. Install sample app and check
Step 3. After installation complete open and check, you get result like this
To create your own campaign link please follow this link
Follow below step to make thing working
Step 1: Create A new Project in android studio and after your project was created open build.gradle, add dependencies to it and synchronized your project
compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:21.0.3' compile 'com.google.android.gms:play-services:7.3.0' compile 'com.google.android.gms:play-services-wearable:7.3.0'
Step 2: Now open your AndroidManifest.xml add permission
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" /> <meta-data android:name="com.google.android.gms.analytics.globalConfigResource" android:resource="@xml/global_tracker" />
Step 3: Add the Google Analytics receiver to your AndroidManifest.xml
file.
<service android:name="com.google.android.gms.analytics.CampaignTrackingService"/> <receiver android:name="com.trinitytutscampaigns.InstallReceiver" android:exported="true" > <intent-filter> <action android:name="com.android.vending.INSTALL_REFERRER" /> </intent-filter> </receiver>
Step 4: Create new java class GoogleAnalyticsApp.java
package com.trinitytutscampaigns; import android.app.Application; import com.google.android.gms.analytics.GoogleAnalytics; import com.google.android.gms.analytics.Tracker; import java.util.HashMap; public class GoogleAnalyticsApp extends Application { // change the following line private static final String PROPERTY_ID = "UA-62514259-2"; public static int GENERAL_TRACKER = 0; public enum TrackerName { APP_TRACKER, GLOBAL_TRACKER, ECOMMERCE_TRACKER, } public HashMap<TrackerName, Tracker> mTrackers = new HashMap<TrackerName, Tracker>(); public GoogleAnalyticsApp() { super(); } public synchronized Tracker getTracker(TrackerName appTracker) { if (!mTrackers.containsKey(appTracker)) { GoogleAnalytics analytics = GoogleAnalytics.getInstance(this); Tracker t = (appTracker == TrackerName.APP_TRACKER) ? analytics.newTracker(PROPERTY_ID) : (appTracker == TrackerName.GLOBAL_TRACKER) ? analytics.newTracker(R.xml.global_tracker) : analytics.newTracker(R.xml.ecommerce_tracker); mTrackers.put(appTracker, t); } return mTrackers.get(appTracker); } }
Step 5: Create BroadCastReciever InstallReceiver.java
package com.trinitytutscampaigns; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log; public class InstallReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { String rawReferrer = intent.getStringExtra("referrer"); if (rawReferrer != null) { Intent in = new Intent("referral"); in.putExtra("referral", rawReferrer); context.sendBroadcast(in); Log.e("refer code", "refer code " + rawReferrer); } } }
Step 6: Complete Code MainActivity.java
package com.trinitytutscampaigns; import android.app.Activity; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.net.Uri; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.TextView; import com.google.android.gms.analytics.GoogleAnalytics; import com.google.android.gms.analytics.HitBuilders; import com.google.android.gms.analytics.Tracker; import java.util.regex.Matcher; import java.util.regex.Pattern; public class MainActivity extends Activity { TextView zText, edit, source, medium; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Tracker t = ((GoogleAnalyticsApp) getApplication()).getTracker(GoogleAnalyticsApp.TrackerName.APP_TRACKER); t.setScreenName("Home"); // t.send(new HitBuilders.AppViewBuilder().build()); zText = (TextView) findViewById(R.id.zText); edit = (TextView) findViewById(R.id.edit); medium = (TextView) findViewById(R.id.mediaun); source = (TextView) findViewById(R.id.source); final String campaignData = "https://play.google.com/store/apps/details?id=com.trinitytutscampaigns&referrer=utm_source%3D123456%26utm_medium%3Dtrinity"; //utm_source=google&utm_medium=cpc zText.setText(campaignData); // Campaign data sent with this hit. t.send(new HitBuilders.ScreenViewBuilder() .setCampaignParamsFromUrl(campaignData) .build() ); registerReceiver(broadcastReceiver, new IntentFilter("referral")); } BroadcastReceiver broadcastReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { Bundle b = intent.getExtras(); String codea = b.getString("referral"); String re1 = ".*?"; String re2 = "(\\d+)"; String re3 = ".*?="; String re4 = "(\\w+)"; Pattern p = Pattern.compile(re1 + re2 + re3 + re4, Pattern.CASE_INSENSITIVE | Pattern.DOTALL); Matcher m = p.matcher(codea); if (m.find()) { String int1 = m.group(1); String word1 = m.group(2); System.out.print("(" + int1.toString() + ")" + "(" + word1.toString() + ")" + "\n"); edit.setText(codea); source.setText(int1.toString()); medium.setText(word1.toString()); Log.e("here","--------------"); }else { Log.e("here","Error--------------"); } } }; @Override protected void onStart() { // TODO Auto-generated method stub super.onStart(); GoogleAnalytics.getInstance(MainActivity.this).reportActivityStart(this); } @Override protected void onStop() { // TODO Auto-generated method stub super.onStop(); GoogleAnalytics.getInstance(MainActivity.this).reportActivityStop(this); } }
That’s all hope this will help you 🙂 . To check this app is working please install it from google play from above link.