Ad

Google Analytics / Campaign Measurement

I am not able to understand one thing in Google Analytics. I need a functionality in my app like if A user refer my app to B user then he got some rewards but I want to track A user id via link when B user hit that link then I can get A user id in B user app on first Activity.

I have generated this link as a example : https://play.google.com/store/apps/details?id=com.example.app&referrer=utm_source%3Dgoogle%26utm_medium%3Demail%26utm_term%3Dtesting%26utm_content%3Dcontent%26utm_campaign%3Dglobus

gradle file :

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.example.app"
        minSdkVersion 18
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    implementation 'com.google.android.gms:play-services-analytics:10.2.4'
    implementation 'com.android.support:support-v4:28.0.0'
    implementation 'com.android.support:appcompat-v7:28.0.0'
}

Analytics class:

public class AnalyticsApplication  extends Application {

    private static GoogleAnalytics sAnalytics;
    private static Tracker sTracker;

    @Override
    public void onCreate() {
        super.onCreate();

        sAnalytics = GoogleAnalytics.getInstance(this);
    }

    /**
     * Gets the default {@link Tracker} for this {@link Application}.
     * @return tracker
     */
    synchronized public Tracker getDefaultTracker() {
        // To enable debug logging use: adb shell setprop log.tag.GAv4 DEBUG
        if (sTracker == null) {
            sTracker = sAnalytics.newTracker(R.xml.global_tracker);
        }

        return sTracker;
    }
}

upper all classes and code referred from this link : https://developers.google.com/analytics/devguides/collection/android/v4/

Now I want track user ID. How can I do that

Ad

Answer

Create a Broadcast receiver with following code:

public class CampaignBroadCastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
       String referrer= intent.getExtras().getString("referrer", ""); //your referrer
       new com.google.android.gms.analytics.CampaignTrackingReceiver().onReceive(context, intent); //update the same to Google Analytics  
    }
}

In your manifest:

     <service android:name="com.google.analytics.tracking.android.CampaignTrackingService" />
     <receiver android:name="yourpackage.CampaignBroadCastReceiver">
                <intent-filter>
                    <action android:name="com.android.vending.INSTALL_REFERRER" />
                </intent-filter>
     </receiver>
Ad
source: stackoverflow.com
Ad