Search code examples
androidgoogle-analyticsnfcndefandroid-applicationrecord

How to add tracking parameters to an android package name on an NFC tag?


I have NFC tags that should access a certain app on my phone when touched. So I put an Android Application Record on the tag for my com.example.app. Then if a user does not have the app, they get directed to the google play store where it can be downloaded.

However, I want to be able to track which nfc tag the user used. So I want to use tracking parameters like ideally it would be com.example.app/?id=1 for example. but I don't think you can do that to packages in java, and I don't think the google-analytics tracking would register that, or would it?

Perhaps there is code somewhere on the android system that, if it sees a package that it doesn't have, goes to the google play store with a generated url? Where could I find this? Perhaps I could edit this script directly?


Solution

  • If you want to track tag usage for installation/download of your app

    Unfortunately, adding tracking parameters to an Android Application Record (AAR) is only partially possible. For instance, you could create an AAR with its package name set to this:

    com.example.application&referrer=utm_source%3Dtag1
    

    This AAR would cause Play Store to be opened for your app "com.example.application" and adds a tracking parameter indicating the campaign source "tag1" (based on Google Analytics SDK campaign measurement).

    However, this AAR would never cause your app to be opened directly. Regardless of whether your app is installed or not, Play Store would be opened. This is due to the NFC system service not supporting tracking parameters in AARs.

    I don't think that ashoke's work around idea of using separate tracking app would be of much use:

    • Your users would need to install a second app besides your main app. Unless, of course, if you integrate the full functionality of your main app in the tracking app.

    • Unless you use a separate tracking app for each tag that you want to track, your users would need to double tap the tag in order to trigger the installation of your main app: Once they would need to tap in order to install the tracking app and once for running the tracking app and passing parameters to it. Thus, you could just as well enforce that double tap with your main app by requiring the user to tap the tag in order to activate the app on the first launch.

    An alternative would be to use a URI record instead of the AAR. One option would be to directly use a Play Store URL that includes tracking parameters:

    https://play.google.com/store/apps/details?id=com.example.application&referrer=utm_source%3Dtag1
    

    Unfortunately, Android's intent filters do not permit to filter on URL parameters, so you could only register your app to trigger upon any "https://play.google.com/store/apps/details" URL regardless of the package ID. Thus, this option is not really suitable.

    The better option would be to provide a redirector web service. For instance, your redirector could have the URL:

    http://www.example.com/apps/application/tag1
    

    The web service would then redirect the user to Play Store (either by redirecting to a Play Store URL or to a "market://" URL):

    https://play.google.com/store/apps/details?id=com.example.application&referrer=utm_source%3Dtag1
    

    So, if your app is not yet installed on a user's device, tapping the tag would cause the URL "http://www.example.com/apps/application/tag1" to be opend in a web browser (which would in turn open the Play Store page of your app and pass the tracking parameters).

    If your app is already installed, you could catch the NDEF_DISCOVERED event for all your tags by registering the following intent filter:

    <intent-filter>
        <action android:name="android.nfc.action.NDEF_DISCOVERED" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:scheme="http" android:host="www.example.com" android:pathPrefix="/apps/application/" />
    </intent-filter>
    

    Within your app, you can then receive the NDEF_DISCOVERED intent and determine the tag that started your app by parsing the URL.

    If you don't care which tag triggered the download of your app and you just want to track tag usage when your app is already installed

    In that case you would use a regular AAR that contains your app's package name and add an additional NFC Forum external type record as the first record of your NDEF message:

    EXT: example.com:tracking PAYLOAD=tag1
    AAR: com.example.application
    

    You would then register for that external type:

    <intent-filter>
        <action android:name="android.nfc.action.NDEF_DISCOVERED" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:scheme="vnd.android.nfc"
              android:host="ext"
              android:pathPrefix="/example.com:tracking"/>
    </intent-filter>
    

    If your app is not installed, the AAR will make sure that your app's Play Store page is launched. If your app is installed, your app will be started and you can parse the NDEF message that is sent to your app as part of the NDEF_DISCOVERED intent for the NFC Forum external type record.