Skip to contentSkip to navigationSkip to topbar
On this page
Looking for more inspiration?Visit the
(information)
You're in the right place! Segment documentation is now part of Twilio Docs. The content you are used to is still here—just in a new home with a refreshed look.

Analytics Swift Firebase Plugin


Firebase is Google's platform for mobile apps. The Segment Firebase destination requires that you bundle the Firebase SDK with your project. The Segment-wrapped destination code then runs on the user's device, and sends its tracking calls to the Firebase API endpoints, and a copy to Segment for archiving.

Firebase's destination plugin code is open source and available on GitHub. You can view it here.(link takes you to an external page)


Adding the dependency

adding-the-dependency page anchor
(warning)

Warning

The Firebase library itself will be installed as an additional dependency.

Xcode

xcode page anchor

In the Xcode File menu, click Add Packages. You'll see a dialog where you can search for Swift packages. In the search field, enter the URL to this repository:

https://github.com/segment-integrations/analytics-swift-firebase

You'll then have the option to pin to a version, or specific branch, as well as which project in your workspace to add it to. Once you've made your selections, click the Add Package button.

Open your Package.swift file and add the following to your dependencies section:

1
.package(
2
name: "Segment",
3
url: "https://github.com/segment-integrations/analytics-swift-firebase.git",
4
from: "1.1.3"
5
),

Using the plugin in your app

using-the-plugin-in-your-app page anchor

Open the file where you setup and configure the Analytics-Swift library. Add this plugin to the list of imports.

1
import Segment
2
import SegmentFirebase // <-- Add this line

Just under your Analytics-Swift library setup, call analytics.add(plugin: ...) to add an instance of the plugin to the Analytics timeline.

1
let analytics = Analytics(configuration: Configuration(writeKey: "<YOUR WRITE KEY>")
2
.flushAt(3)
3
.trackApplicationLifecycleEvents(true))
4
analytics.add(plugin: FirebaseDestination())

Your events will now flow to Firebase in device mode.


When you call Identify, Segment maps events and properties to the corresponding Firebase Analytics calls:

  • If there is a userId on your Identify call, Segment triggers setUserId using the Firebase SDK
  • If there are traits included, Segment will set user properties for each trait you include on the identify call

You can use these traits to create audiences and views to analyze your users' behavior.

(warning)

Sending PII to Firebase

Google prohibits sending PII to Firebase unless "robust notice" is given to your app users(link takes you to an external page). For iOS apps, some Analytics features, like audiences and campaign attribution, and some user properties, like Age and Interests, require you to enable the AdSupport framework(link takes you to an external page). Learn more about Firebase's reporting dashboard here(link takes you to an external page) in Google's documentation.

Firebase has strict requirements for User Property names. User Property names must:

  • Begin with a letter (not a number or symbol, including an underscore)
  • Contain only alphanumeric characters and underscores
  • Be no longer than 40 characters

User Property values must be fewer than 100 characters.

You are limited to 25 unique user properties per Firebase Console.

Firebase automatically collects these user properties(link takes you to an external page).


When you call Track, Segment logs the event with Firebase and maps from the Segment spec to Firebase's spec. For anything that does not match, Segment will pass the event to Firebase as a custom event. Custom parameters cannot be seen directly in the Firebase Analytics dashboard but they can be used as filters in Audiences.

Firebase automatically tracks the events listed here(link takes you to an external page) and will still do so when bundling with Segment.

(information)

Firebase has a limit of 500 distinctly named events

Given this limit, Segment recommends that you're intentional in what you track.

Like with user properties, Segment performs the following transformations on both your event names and event parameters. Unlike user properties, you do not need to pre-define event parameters in your Firebase dashboard.

  • Trims leading and trailing whitespace from property names
  • Replaces spaces with underscores
  • Trims property names to 40 characters (Android only)

Event parameter values must be fewer than 100 characters.

Segment adheres to Firebase's semantic event specification and maps the following Segment spec-matching events (left) to the corresponding Firebase events (right):

Segment EventFirebase Event
Products Searchedsearch
Product List Viewedview_item_list
Product Viewedview_item
Product Clickedselect_content
Product Sharedshare
Product Addedadd_to_cart
Product Added To Wish Listadd_to_wishlist
Checkout Startedbegin_checkout
Promotion Viewedpresent_offer
Payment Info Enteredadd_payment_info
Order Completedpurchase
Order Refundedpurchase_refund

Segment maps the followed Segment spec-matching properties (left) to the corresponding Firebase event parameters (right):

Segment PropertyFirebase PropertyAccepted Value(s)
categoryitem_category(String) "kitchen supplies"
product_iditem_id(String) "p1234"
nameitem_name(String) "Le Creuset pot"
priceprice(double) 1.0
quantityquantity(long) 1
querysearch_term(String) "Le Creuset"
shippingshipping(double) 2.0
taxtax(double) 0.5
totalvalue(double) 3.99 or (long) 3.99
revenuevalue(double) 3.99 or (long) 3.99
order_idtransaction_id(String) "o555636"
currencycurrency(String) "USD"

Passing revenue and currency

passing-revenue-and-currency page anchor

Ecommerce events containing "revenue" or "total" must also include the appropriate ISO 4217 "currency" string for revenue data to populate to the Firebase dashboard. If a "currency" value is not included, Segment default to "USD".

1
Properties properties = new Properties()
2
.putValue("orderId", "p966540")
3
.putValue("revenue", 25.00)
4
.putCurrency("USD");
5
6
7
Analytics.with(this).track("Order Completed", properties);
1
struct TrackProperties: Codable {
2
let orderId: String
3
let revenue: Int
4
let currency: String
5
}
6
7
analytics.track(name: "Order Completed", properties: TrackProperties(orderId: "order-123", revenue: 23.00, currency: "USD"))

Segment doesn't map Screen events to Firebase, because Firebase's SDK collects screen information out of the box for you.

For iOS, you can configure recordScreenViews which will automatically track screen views, or pass in a screen manually using a Screen call. You should be able to disable the Automatic Screen reporting by adding the plist flag FirebaseScreenReportingEnabled to Info.plist and set its value to NO (Boolean).

Google Analytics for Firebase iOS does NOT support the case of manual-only screen reporting. Firebase only supports automatic + manual screen reporting or no screen reporting at all.

Conversion tracking and Adwords conversions

conversion-tracking-and-adwords-conversions page anchor

Firebase is Google's recommended method for reporting conversions to Adwords. To use Firebase, track the conversion events as you normally would with Segment and Segment will send them through to Firebase.


Firebase has great logging. If you are having any issues, enable debug mode as outlined in Google's Debug events(link takes you to an external page) docs.