Contents
Introduction
Across every industry, Google Analytics is used by many organisations to understand user engagement and the performance of the campaigns. This document details use cases on how the Firebase SDK can be incorporated alongside the Emarsys SDK.
Enable Google Analytics on Firebase Project
On the Firebase console, go to Project Overview > Settings > Integrations tab.
Confirm that Google Analytics is enabled for the project.
In the Firebase console, click Android icon. If you've already added an app to your Firebase project, click Add app to display the platform options.
Enter the app package name in the Android package name field.
Click Register app.
Add a Firebase configuration file by clicking Download google-services.json to obtain the Firebase Android config file.
Move the config file into the app folder of the app. Click Next.
Add Firebase SDK to the app by adding the dependency for the Firebase SDK for Google Analytics in the app-level build gradle file.
implementation 'com.google.firebase:firebase-analytics:17.5.0'
Sync the project grade as it has been updated.
Declare the com.google.firebase.analytics.FirebaseAnalytics
object at the top of your activity.
private lateinit var firebaseAnalytics: FirebaseAnalytics
Initialise it in the onCreate()
method.
firebaseAnalytics = FirebaseAnalytics.getInstance(this)
In the Firebase console, click iOS icon. If you've already added an app to your Firebase project, click Add app to display the platform options.
Enter the app's bundle ID in the iOS bundle ID field.
Click Register app.
Add a Firebase configuration file by clicking Download GoogleService-Info.plist to obtain your Firebase iOS config file.
Move your config file into the root of your Xcode project. Click Next.
Add Firebase SDK to the app by adding the Firebase pod for Google Analytics in the Podfile.
pod 'Firebase/Analytics'
Run pod install.
Initialise Firebase by importing the Firebase module in UIApplicationDelegate.
import Firebase
Configure a FirebaseApp shared instance in the application:didFinishLaunchingWithOptions:
method.
FirebaseApp.configure()
Track Custom Events
To track Emarsys custom events, place Google Analytics custom events next to Emarsys custom events call.
Kotlin
val bundle = Bundle()
bundle.putString("key1", "value1")
bundle.putString("key2", "value2")
firebaseAnalytics.logEvent("event_name", bundle)
Swift
Analytics.logEvent("event_name", parameters: ["key1": "value1", "key2": "value2"])
Track Purchases
To track Emarsys purchase events, place Google Analytics Purchase events when users checks out.
Kotlin
val item1 = Bundle()
item1.putString(FirebaseAnalytics.Param.ITEM_NAME, "jeggings")
item1.putString(FirebaseAnalytics.Param.ITEM_CATEGORY, "pants")
val item2 = Bundle()
item2.putString(FirebaseAnalytics.Param.ITEM_NAME, "boots")
item2.putString(FirebaseAnalytics.Param.ITEM_CATEGORY, "shoes")
val bundle = Bundle()
bundle.putParcelableArray(FirebaseAnalytics.Param.ITEMS, arrayOf(item1, item2))
bundle.putString(FirebaseAnalytics.Param.COUPON, "SummerPromo")
bundle.putString(FirebaseAnalytics.Param.CURRENCY, "USD")
bundle.putDouble(FirebaseAnalytics.Param.VALUE, 59.99)
bundle.putDouble(FirebaseAnalytics.Param.SHIPPING, 3.99)
bundle.putString(FirebaseAnalytics.Param.TRANSACTION_ID, "192803301")
firebaseAnalytics.logEvent(FirebaseAnalytics.Event.PURCHASE, bundle)
Swift
Analytics.logEvent(AnalyticsEventPurchase, parameters: [
AnalyticsParameterItems: [
[AnalyticsParameterItemName: "jeggings", AnalyticsParameterItemCategory: "pants"],
[AnalyticsParameterItemName: "boots", AnalyticsParameterItemCategory: "shoes"]
],
AnalyticsParameterCoupon: "SummerPromo",
AnalyticsParameterCurrency: "USD",
AnalyticsParameterValue: 59.99,
AnalyticsParameterShipping: 3.99,
AnalyticsParameterTransactionID: "192803301",
])
Track CTA Buttons
To track Emarsys CTA events, send events to Google Analytics in the existing in-app event handler.
Kotlin
override fun handleEvent(context: Context, eventName: String, payload: JSONObject?) {
val bundle = Bundle()
bundle.putString("key1", "value1")
bundle.putString("key2", "value2")
firebaseAnalytics.logEvent("event_name", bundle)
}
Swift
override func handleEvent(_ eventName: String, payload: [String: NSObject]?) {
super.handleEvent(eventName, payload: payload)
Analytics.logEvent("event_name", parameters: ["key1": "value1", "key2": "value2"])
}
Track Deep Links
To track Emarsys deep link clicking events:
- Get the Deep Link payload in handleEvent method
- Extract the URL from the payload
- Extract the UTM parameters from URL
- Send logs to Google Analytics
Kotlin
val bundle = Bundle()
bundle.putString(FirebaseAnalytics.Param.CAMPAIGN, <UTM_CAMPAIGN>)
bundle.putString(FirebaseAnalytics.Param.CONTENT, <UTM_CONTENT>)
bundle.putString(FirebaseAnalytics.Param.MEDIUM, <UTM_MEDIUM>)
bundle.putString(FirebaseAnalytics.Param.SOURCE, <UTM_SOURCE>)
bundle.putString(FirebaseAnalytics.Param.TERM, <UTM_TERM>)
firebaseAnalytics.logEvent("deep_link", bundle)
Swift
Analytics.logEvent("deep_link", parameters: [
AnalyticsParameterCampaign: <UTM_CAMPAIGN>,
AnalyticsParameterContent: <UTM_CONTENT>,
AnalyticsParameterMedium: <UTM_MEDIUM>,
AnalyticsParameterSource: <UTM_SOURCE>,
AnalyticsParameterTerm: <UTM_TERM>