Using Firebase and Google Tag Manager with AppsFlyer—Sending installs and events

At a glance: Instructions for setting up event sending reported via Firebase SDK to AppsFlyer in your app.

Configuring events in the app

This section discusses how to set up and create events in the app with Google Tag Manager (GTM).

When the event is sent, GTM checks to see if there is a tag that is configured to process this event with the help of the event trigger. The event trigger is set to fire the tag whenever a specific event is sent. If a specific event is sent and it has a corresponding trigger, the tag is fired. When the tag is fired, Google Tag Manager collects all the data in the event. Such data includes the AppsFlyer ID,  event name, and event parameters. After collecting all the data, Google Tag Manager sends the event to AppsFlyer.

Creating event variables for revenue and price

Android

JavaKotlin
  1. In the MainActivity class, create a variables to hold AppsFlyer Device ID:
    public static String appsFlyerID; 
  2. Back in the MainActivity class, in the onCreate method and after super.onCreate, pass the AppsFlyer ID to the variable:
    appsFlyerID = AppsFlyerLib.getInstance().getAppsFlyerUID(this);

iOS

For iOS, parameters are available across the app. You can retrieve them through the AppsFlyerTracker instance.

Objective C Swift
  1. AppsFlyer Device ID -
    [AppsFlyerTracker sharedTracker].getAppsFlyerUID
  2. Apple App ID -
    [AppsFlyerTracker sharedTracker].appleAppID

Adding events in the app

The first step is to configure the event in the app. The event is sent using Firebase Event Logging In the event you specify the AppsFlyer ID, event name, and event parameters. The AppsFlyer ID is retrieved from the variable that is created in the setup step.

Google Tag Manager uses Firebase Analytics Events to trigger tag events. Whenever an event is sent to Firebase, Tag Manager recognizes the event and sends it to AppsFlyer as well.

Android

JavaKotlin
  1. In the desired activity, add the import statement for Firebase:
    import com.google.firebase.analytics.FirebaseAnalytics;
  2. Add the following code to run whenever a purchase event occurs:
    Bundle bundle = new Bundle();
      // notice "af_id", this is the name of the event parameter 
      // for AppsFlyer ID that we created in the previous step
      bundle.putString("af_id", appsFlyerID);
      bundle.putString("af_revenue", "200");
      bundle.putString("af_price", "250");
      mFirebaseAnalytics.logEvent("af_purchase", bundle);
        

iOS

Objective-C Swift
  1. In the view where the event is sent add @import Firebase
  2. Add the following code to run whenever a purchase event occurs:
    NSString *id_prefix = @"id";
    NSString *apple_app_id = [id_prefix stringByAppendingString:[AppsFlyerTracker sharedTracker].appleAppID];

    [FIRAnalytics logEventWithName:@"af_purchase" parameters:@{ @"apple_app_id": apple_app_id, @"af_id": [AppsFlyerTracker sharedTracker].getAppsFlyerUID, @"af_revenue": @250, @"af_price": @375 }];

Sending install events to Firebase

You can also send install events to Firebase with all install-related data.

Android

Java Kotlin

To send install events to Firebase in Android, you can use the conversionDataobject.

Creating the send event method

Add the following method in the AFApplication class right below the onCreate method:

public void sendInstallToFirebase(Map<String, String> conversionData){
       mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);
       Bundle bundle = new Bundle();
       bundle.putString("install_time", conversionData.get("install_time") == null ? String.valueOf(new Date().getTime()) : conversionData.get("install_time"));
       bundle.putString("click_time", conversionData.get("click_time"));
       bundle.putString("media_source", conversionData.get("media_source") == null ? "organic": conversionData.get("media_source"));
       bundle.putString("campaign", conversionData.get("campaign") == null ? "organic": conversionData.get("campaign"));
       bundle.putString("install_type", conversionData.get("af_status"));
       mFirebaseAnalytics.logEvent("install", bundle);
    }

This method accepts a conversionData object. The method checks whether install time, media source, and campaign are null and if so sets the install time to the current time and the media source and campaign organic. Otherwise, it takes the data from the conversionData object and sends it to Firebase.

Sending the install event

Add the following code in the onConversionDataSuccess method:

if(conversionData.get("is_first_launch").equals("true")){
       sendInstallToFirebase(conversionData);
    }

This code checks if this is the first time the app is launched. If so, it calls the sendInstallToFirebase method.

iOS

Objective-C Swift

Creating the send event method

In AppDelegate.m, add the following method at the end of the file:

- (void)sendInstallToFirebase:(NSDictionary *)installData{
      NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
      [dateFormatter setDateFormat:@"dd/MM/yyyy HH:mm:ss"];
      NSDate *date = [NSDate date];
      NSString *newDate = [dateFormatter stringFromDate:date];
    
      if([[installData objectForKey: @"af_status"] isEqual: @"Organic"]){
        [FIRAnalytics logEventWithName:@"install"
         parameters:@{
          @"install_time": newDate,
          @"media_source": @"organic",
          @"campaign": @"organic"
    
        }];
      }
      else {
        [FIRAnalytics logEventWithName:@"install"
          parameters:@{
          @"install_time": [installData objectForKey: @"install_time"],
          @"click_time": [installData objectForKey: @"click_time"],
          @"install_type": [installData objectForKey: @"af_status"],
          @"media_source": [installData objectForKey: @"media_source"],
          @"campaign": [installData objectForKey: @"campaign"]
        }];
      }
    }

This method receives the installData object and checks if the install is organic or not. If it's organic, it sets the install time to the current time and the media source, campaign, and install type to organic.

If the install is non-organic, the method gets the relevant non-organic install data.

Once the parameters are set, the method sends the install event to Firebase.

Sending the install event

In AppDelegate.m, in the method didFinishLaunchingWithOptions, add [FIRApp configure]

In AppDelegate.m, in the onConversionDataSuccess method, add the following code at the end of the method:

if([installData objectForKey:@"is_first_launch"]){
      [self sendInstallToFirebase: installData];
    }

The code snippet above checks if this is the first time the app is launched. If it is, it calls the sendInstallToFirebase method.

 Warning

Certain media sources don't allow their data to be shared with third-party platforms and services. If you are working with partners such as Meta ads, Twitter, Snap, Pinterest, etc. who have set restrictions on sharing their data with third-party platforms and services, please make sure to follow their guidelines and remove any data which is under these restrictions.