Search code examples
cordovagoogle-analyticscordova-plugins

How do I implement Campaign tracking with Cordova?


I am trying to implement campaign tracking in my cordova app, but I am not having success.

I previously used danwilson plugin, which works nice, but it does not have support for campaigns, as I saw here:

https://github.com/danwilson/google-analytics-plugin/issues/68

So I changed my plugin to this fork:

https://github.com/Anu2g/google-analytics-plugin

Which have campaign tracking.

I am currently testing in Android, I have added this to my manifest

    <!-- Used for Google Play Store Campaign Measurement-->
<service android:name="com.google.android.gms.analytics.CampaignTrackingService" />
<receiver android:name="com.google.android.gms.analytics.CampaignTrackingReceiver"
          android:exported="true">
    <intent-filter>
        <action android:name="com.android.vending.INSTALL_REFERRER" />
    </intent-filter>
</receiver>

As is shown in

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

And I have the campaign function in my UniversalAnalyticsPlugin.java

private void trackView(String screenname, String deepLinkUrl, CallbackContext callbackContext) {
    if (! trackerStarted ) {
        callbackContext.error("Tracker not started");
        return;
    }

    addCustomDimensionsToTracker(tracker);

    if (null != screenname && screenname.length() > 0) {
        tracker.setScreenName(screenname);
        tracker.send(new HitBuilders
                .ScreenViewBuilder()
                .setCampaignParamsFromUrl(deepLinkUrl)
                .build()
                );
        callbackContext.success("Track Screen: " + screenname);
    } else {
        callbackContext.error("Expected one non-empty string argument.");
    }
}

I try to make it work using the Google guide for testing:

https://developers.google.com/analytics/solutions/testing-play-campaigns

I launch

adb shell am broadcast -a com.android.vending.INSTALL_REFERRER -n com.my.app/com.google.android.gms.analytics.CampaignTrackingReceiver --es "referrer" "utm_source%3DtestSource%26utm_medium%3DtestMedium%26utm_term%3DtestTerm%26utm_content%3DtestContent%26utm_campaign%3DtestCampaign"

In my cmd, and it returns

    Broadcasting: Intent { act=com.android.vending.INSTALL_REFERRER cmp=com.my.app/com.google.android.gms.analytics.CampaignTrackingReceiver (has extras) }
Broadcast completed: result=0

Which looks to work fine. Then I open my logcat, I open the compiled app, and it logs Thread[GAThread,5,main]: No campaign data found.

As I see in the plugin docs, I have to do this:

To track a Screen (PageView) w/ campaign detilas:

window.analytics.trackView('Screen Title', 'my-scheme://content/1111?utm_source=google&utm_campaign=my-campaign')

But I dont understand how do I receive the real URL with the params, not a hardcoded one.

Someone who successfully have implemented Campaign tracking in Cordova can enlighten me?

Thanks in advance


Solution

  • After some research, and some changes in the plugin by this author, the answer can't be more obvious:

    in the last revision of the plugin: https://github.com/danwilson/google-analytics-plugin the author added an undocumented functionality for campaigns, and it works without any modification. It add the tags in the manifest and it works when a campaign link is launched to Google Play.

    One point: the No campaign data found is also showed, but it works, even if the response is not the one that Google says

    Good luck