简体   繁体   中英

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

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>

I haven't tried this out yet. But according to the documentation available on Google Analytics Campaign Tracking. The following code should work inside your main activity onCreate or onStart

// Get the intent that started this Activity.
Intent intent = this.getIntent();
Uri uri = intent.getData();
String campaign = uri.getQueryParameter("utm_source");

And add the receiver on your manifest file to get INSTALL_REFERRER broadcasts from play store

<receiver android:name="com.google.android.gms.analytics.CampaignTrackingReceiver"
    android:enabled="true"
    android:exported="true">
    <intent-filter>
        <action android:name="com.android.vending.INSTALL_REFERRER" />
    </intent-filter>
</receiver>

https://developers.google.com/analytics/devguides/collection/android/v3/campaigns

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM