Real-Time Uninstall Tracking for Android

Learn to integrate Real-Time Uninstall Tracking for Android using FCM and Netcore API.

Overview

The Real-Time Uninstall Tracking feature allows businesses to monitor app uninstall events on Android devices by integrating Firebase Analytics and Netcore's API. This feature captures the app_remove event from Firebase processes it via cloud functions and pushes the data to Netcore for re-engagement or analysis.

Prerequisites

You can do the following to integrate Real-Time Uninstall Tracking for Android.

  1. Ensure the android app is integrated with Firebase Analytics.
  2. Ensure you have a Firebase Blaze Plan Account which is required to utilize Firebase Cloud Functions to call third-party HTTP endpoints.
  3. Install the latest versions for setting up Firebase Cloud Functions.

Implementation Steps

Real-Time Uninstall Tracking consists of three key steps:

  1. Set up a common identifier in your app
  2. Set the app_remove event as a conversion event in Firebase
  3. Use the Firebase Cloud function to send uninstall data to Netcore

1. Set Up a Common Identifier

To ensure a common data structure between Firebase and Netcore, include the following code snippet in your Android app:

val partnerParams = Smartech.getInstance(WeakReference(application)).getPartnerParametersString()
val bundle = Bundle()
bundle.putString(SMTPartnerConstants.SMT_PARTNER_PARAMETER_KEY, partnerParams)

// Additional info from the customer
val appId = Smartech.getInstance(WeakReference(application)).getAppID()
val guid = Smartech.getInstance(WeakReference(application)).getDeviceUniqueId()
bundle.putString("ncAppId", appId)
bundle.putString("ncGuid", guid)
val ncExternalId = Smartech.getInstance(WeakReference(application)).getExternalIdentity()
bundle.putString(SMTPartnerConstants.SMT_PARTNER_EXTERNAL_IDENTITY, ncExternalId)

// Set the data to Firebase
mFirebaseAnalytics?.setDefaultEventParameters(bundle)

2. Set the app_remove Event as a Conversion Event in Firebase

Firebase automatically collects the app_remove event when an app is uninstalled from an Android device. Follow these steps to enable it as a conversion event:

  1. Log in to your Firebase dashboard and select the integrated project.
  2. Navigate to Analytics > Events.
  3. Locate the app_remove event in the list.
  4. Toggle the Mark as conversion switch to enable it as a conversion event.

📘

Note

The app_remove event is only applicable to Android apps and is automatically tracked when an app package is removed or uninstalled.

3. Create and Deploy a Firebase Cloud Function

Create a Cloud Function

The Firebase Cloud Function sends app_remove data to Netcore using HTTP requests. Refer to here for Firebase documentation.

Perform the following steps:

  1. Install Firebase CLI using the command:
npm install -g firebase-tools
  1. Log in to Firebase CLI.
  2. Initialize Firebase SDK for Cloud functions.
  3. Select JavaScript as the language.
  4. Open the index.js file in your project directory and add the following code:
const functions = require("firebase-functions/v1");
const admin = require("firebase-admin");
const request = require("request");
const logger = require("firebase-functions/logger");

admin.initializeApp();

function handleAppRemoveEvent(event, config) {
  logger.log("Sending event to Netcore");
  const ncPartnerParam = event.params.ncPartnerParameter;
  if (!ncPartnerParam) {
    logger.warn("ncPartnerParameter not found, skipping API call.");
    return Promise.resolve(0);
  }

  const postEventData = JSON.stringify({
    data: [{ eventName: "app_remove", ncPartnerParameter: ncPartnerParam }],
  });

  const requestOptions = {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
      "x-api-key": config.X_API_KEY,
    },
    body: postEventData,
    url: config.WEBHOOK_URL,
    maxAttempts: 5,
  };

  return new Promise((resolve, reject) => {
    request(requestOptions, (err, response, body) => {
      if (err) {
        logger.error("API request error:", err);
        reject(err);
      } else if (response && response.statusCode === 200) {
        logger.log("API response body:", body);
        resolve(0);
      } else {
        logger.error("Unexpected API response:", response && response.statusCode);
        reject(new Error("Unexpected response"));
      }
    });
  });
}

exports.sendAndroidUninstallToNetcore = functions.analytics
  .event("app_remove")
  .onLog((event) => {
    logger.log("Firebase app_remove event payload:", JSON.stringify(event));

    const NETCORE_CONFIG = {
      X_API_KEY: "YOUR_API_KEY_HERE",
      WEBHOOK_URL: "YOUR_NETCORE_WEBHOOK_URL_HERE",
    };

    return handleAppRemoveEvent(event, NETCORE_CONFIG);
  });
  1. Replace placeholders (YOUR_API_KEY_HERE and YOUR_NETCORE_WEBHOOK_URL_HERE) with your actual Netcore API key and Webhook URL.

Deploy the Function

  1. Fix any ESLint issues:
    ./node_modules/.bin/eslint index.js --fix
    
  2. Deploy the function:
    firebase deploy --only functions
    
  3. For updates, redeploy with the function name:
    firebase deploy --only "functions:sendAndroidUninstallToNetcore"
    

Once deployed successfully, uninstall events are sent to Netcore in real-time. These events can be utilized to:

  • `Re-engage users via personalized messages or campaigns.
  • Analyze uninstall trends to improve retention strategies.