Engineers & Developers

Extensibility at Segment: Use Mobile Plugins to enable Location-aware Marketing

Follow OwlEats, a food delivery startup, that uses Segment's mobile plugins to seamlessly integrate location data, enhancing customer targeting and incentivizing in-store pickups.

Nov 14, 2023

By Didier Garcia, Rahul Lalmalani


Our story follows OwlEats, a food delivery startup. In an effort to attract customers to eat local, vendors have asked OwlEats to begin a new promotional effort, providing coupons to customers incentivizing in-store pickup.

Extensibility at Segment v2 (2)

What can Segment do?

The engineers at OwlEats are already using Segment as their CDP to instrument their mobile apps and learn about customer behavior. Now their goal is to enrich customer data with location so they can use their existing marketing channels (push, email, SMS) to offer location-based coupons.

Segment offers multiple approaches to enrich customer data, i.e. augment data with additional user context to make it more valuable and capable of generating more insight and engagement. 

Specifically, they have the options of either:

  1. Capturing and adding location data directly via mobile hardware, before it leaves the source

  2. Using a reverse-lookup to translate IP Address into location data

  3. Using RETL (Reverse ETL) to import user location data from OwlEats databases

Out of these three options, the first makes the most sense. OwlEats already requires its end-users to turn on location access to use the app, all they need to do is capture it and add it to each event.

Passive Location Enrichment with Mobile Plugins

Now that they’ve decided on the approach of collecting location directly from the phone, it’s important that they do this passively

Passive enrichment means that the location Plugin should not have to pester the user with any sort of popup or permission. The app user experience itself should be responsible for requesting the user its access to Location services as part of the onboarding experience. The location Plugin they’re going to write works because users have already consented to sharing their location with OwlEats. 

The Segment Plugin architecture allows developers to intercept all generated events and transform them via a single code snippet, so that we can easily enrich all events before they leave the system. 

The OwlEats mobile apps are already instrumented with Analytics-Swift & Analytics-Kotlin (since these are backward compatible with my legacy codebases, have significantly improved performance, and make extensibility a breeze), so they can just get started on their Plugin!

Step 1: Define a new plugin

Write the below plugin in Kotlin for Android, (and separately in Swift for my iOS app)

class PassiveLocationPlugin(val context: Context) : Plugin { override lateinit var analytics: Analytics override val type: Plugin.Type = Plugin.Type.Enrichment // intercept every event in the pipeline override fun execute(event: BaseEvent): BaseEvent? { // Update the context property event.context = buildJsonObject { // Add all existing context properties event.context.forEach { (key, value) -> put(key, value) } // If we have Location Permission (Fine or Coarse) if (haveAnyLocationPermission()) { val passiveLastKnownLocation = getLastKnownLocation() // Build top-level event.context.location object. put("location", buildJsonObject { put("latitude", JsonPrimitive(passiveLastKnownLocation?.latitude)) put("longitude", JsonPrimitive(passiveLastKnownLocation?.longitude)) }) } } return event } }

You can find the complete Kotlin example here.

Step 2: Let’s add the plugin that we’ve defined and add it to the Analytics event timeline:

// Use the plugin analytics.add(PassiveLocationPlugin(this))

That’s it! Once this is done, they can redeploy their app to the app store and watch the data roll in! Now, when they receive events, this is what the JSON looks like. OwlEats can access location data via event.location, and use it to drive proximity-based targeting of coupons with better real-time accuracy, and better overall engagement!

Screenshot 2023-11-13 at 5.09.58 PM

Conclusion

Plugins are an extremely powerful way to enrich / transform CDP data directly at their source, and tap into hardware-specific functionality like location. By using Mobile Plugins, OwlEats is able to quickly transform all its event data to be contextually richer and more valuable, allowing us to implement custom engagement programs for OwlEats users. 

Here are some resources to learn more about Plugin Architecture on Mobile. We look forward to seeing what you build!

The State of Personalization 2023

Our annual look at how attitudes, preferences, and experiences with personalization have evolved over the past year.

Get the report
SOP 2023

The State of Personalization 2023

Our annual look at how attitudes, preferences, and experiences with personalization have evolved over the past year.

Get the report
SOP 2023

Share article

Want to keep updated on Segment launches, events, and updates?

We’ll share a copy of this guide and send you content and updates about Twilio Segment’s products as we continue to build the world’s leading CDP. We use your information according to our privacy policy. You can update your preferences at any time.