简体   繁体   中英

How to set up google analytics with custom campaign attribution (not urls)

I am trying to set up my google analytics on my android app with "campaign attribution". Basically I have various nfc tags that launch my app and each has an extra NDEF message on them with the "campaign ID". And I want each tag to be attributed to a separate campaign in my analytics account. However the strings are already being passed in correctly from the tags, and this part is working. Now I need to send them as part of the tracker.

I have a seperate MyApplication.java (copied directly from google's dev site) file with:

public class MyApplication extends Application {
    private static final String PROPERTY_ID = "UA-XXXXXXX-X";
    public enum TrackerName {
        APP_TRACKER,
        GLOBAL_TRACKER,
        ECOMMERCE_TRACKER,      //I don't really need this

    }
    HashMap<TrackerName, Tracker> mTrackers = new HashMap<TrackerName, Tracker>();
    synchronized Tracker getTracker(TrackerName trackerId) {
        if (!mTrackers.containsKey(trackerId)) {

            GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);
            Tracker t = (trackerId == TrackerName.APP_TRACKER) ? analytics.newTracker(PROPERTY_ID)
                    : (trackerId == TrackerName.GLOBAL_TRACKER) ? analytics.newTracker(R.xml.global_tracker)
                    : analytics.newTracker(R.xml.global_tracker);
            mTrackers.put(trackerId, t);

        }
        return mTrackers.get(trackerId);
    }

} this is pretty much straight from the google developers site on analytics sdk4

Then in my cameraActivity where I want the custom campaign parameter to be tracked, I have

public class CameraActivity extends Activity implements PictureCallback {

    String campaignID = "Tag_1";    //in reality this is passed in from the tag. but i hard-coded it here for simplicity
    protected void onCreate(Bundle savedInstanceState) {
        Tracker t = ((com.snappiesticker.app) getActivity().getApplication()).getTracker(TrackerName.APP_TRACKER);
        t.send(new HitBuilders.ScreenViewBuilder().set("tag",campaignID).build());
    }
}

right now it is having issues with the app's name: com.snappiesticker.app and also with getActivty(). The name is my app's package name. Should it be something else? Also it can't find where getActivity() is defined. However again, this is basically copied directly from the google dev website.

How can I send this tracker correctly?

I assume that you have setup your Application class ( MyApplication ) in your app manifest with something like this:

<application android:name=".MyApplication" ...>

Moreover, I assume that both, the MyApplication class and the CameraActivity class, reside in the same package (your application package com.snappiesticker.app ). Otherwise package-level visibility method getTracker() . If that's not the case, you would need to change the visibility to public:

public synchronized Tracker getTracker(TrackerName trackerId) {

In order to get an instance of your Application class from an activity within your app, you would call the getApplication() method of that activity. You would then cast the Application object into an instance of your MyApplication class. Then you will be able to access the getTracker() method.

public class CameraActivity extends Activity implements PictureCallback {
    protected void onCreate(Bundle savedInstanceState) {
        Tracker t = ((MyApplication)getApplication()).getTracker(TrackerName.APP_TRACKER);
        t.send(new HitBuilders.ScreenViewBuilder().set("tag",campaignID).build());
    }
}

Note that you will only need a tracker XML file (eg global_tracker.xml ) if you want to instantiate a tracker from an XML file by using

Tracker t = analytics.newTracker(R.xml.global_tracker);

If you only instantiate trackers from a PROPERTY_ID string value, there's no need for such an XML file.

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