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 Braze Plugin


Braze(link takes you to an external page), formerly Appboy, is an engagement platform that empowers growth by helping marketing teams to build customer loyalty through mobile, omni-channel customer experiences.

Braze's destination plugin code is open source and available on GitHub. You can view it on GitHub in the @braze-inc/analytics-swift-braze(link takes you to an external page) repo. This destination plugin is maintained by Braze. For any issues with the destination plugin code, please reach out to Braze's support.


Getting started

getting-started page anchor
  1. From the Segment web app, click Catalog.
  2. Search for "Braze" in the Catalog, select it, and choose which of your sources to connect to the destination.
  3. In the Destination Settings, add the API Key, found in the Braze Dashboard in App Settings > Manage App Group.
  4. Set up a new App Group REST API Key in the Braze Dashboard in App Settings > Developer Console > API Settings. For more information, see Creating and Managing REST API Keys(link takes you to an external page) in the Braze documentation. Select the users.track endpoint in the User Data section.

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-braze

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 the dependencies section:

1
.package(
2
name: "Segment",
3
url: "https://github.com/segment-integrations/analytics-swift-braze.git",
4
from: "1.0.0"
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 SegmentBraze // <-- 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: BrazeDestination())

(information)

Optimize your integration with Braze

Add Segment's open-source Middleware(link takes you to an external page) tool to optimize your integration. This tool limits Data Point(link takes you to an external page) use by debouncing duplicate Identify calls from Segment. For more information, see the project's README(link takes you to an external page).

If you're not familiar with the Segment Specs, take a look to understand what the Identify method does. An example call would look like:

1
struct MyTraits: Codable {
2
let favoriteColor: String
3
}
4
5
analytics.identify(userId: "a user's id", MyTraits(favoriteColor: "fuscia"))

When you Identify a user, Segment passes that user's information to Braze and sets the userId as Braze's External User ID.

If you're using a device-mode connection, Braze's SDK assigns a device_id and a backend identifier, braze_id, to every user. This allows Braze to capture anonymous activity from the device by matching on those identifiers instead of userId.

Capture the braze_id of anonymous users

capture-the-braze_id-of-anonymous-users page anchor

Pass one of the many identifiers that may exist on an anonymous user profile to the Braze's User by Identifier REST endpoint(link takes you to an external page) to capture and export the braze_id. These identifiers include:

  • email address
  • phone number
  • device_id

Choose an identifier that is available on the user profile at that point in the user lifecycle.

For example, if you pass device_id to the User by Identifier endpoint:

1
{
2
"device_id": "{{device_id}}",
3
"fields_to_export": ["braze_id"]
4
}

The endpoint returns:

1
{
2
"users": [
3
{
4
"braze_id": "{{braze_id}}"
5
}
6
],
7
"message": "success"
8
}
(information)

Tip

If you decide to use the braze_id, consider contacting Segment Success Engineering(link takes you to an external page) or a Solutions Architect to verify your Braze implementation.

Segment's special traits recognized as Braze's standard user profile fields (in parentheses) are:

Segment EventBraze Event
firstNamefirst_name
lastNamelast_name
birthdaydob
avatarimage_url
address.cityhome_city
address.countrycountry
gendergender

Segment sends all other traits (except Braze's reserved user profile fields(link takes you to an external page)) to Braze as custom attributes. You can send an array of strings as trait values but not nested objects.


(information)

Tip

To lower Data Point(link takes you to an external page) use, limit the events you send to Braze to those that are relevant for campaigns and segmentation to the Braze destination. For more information, see Schema Controls.

The Braze Swift destination plugin currently only supports sending logPurchase events, and custom events are not supported in device mode. Please review the plugin code(link takes you to an external page) for more information.

Braze supports currency codes as specified in their Purchase Object Specification(link takes you to an external page). Be aware that any currency reported other than USD displays in the Braze UI in USD based on the exchange rate on the date it was reported(link takes you to an external page).