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.
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:
Capturing and adding location data directly via mobile hardware, before it leaves the source
Using a reverse-lookup to translate IP Address into location data
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!
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 reportThe State of Personalization 2023
Our annual look at how attitudes, preferences, and experiences with personalization have evolved over the past year.
Get the reportShare article
Recommended articles
How to accelerate time-to-value with a personalized customer onboarding campaign
To help businesses reach time-to-value faster, this blog explores how tools like Twilio Segment can be used to customize onboarding to activate users immediately, optimize engagement with real-time audiences, and utilize NPS for deeper customer insights.
Introducing Segment Community: A central hub to connect, learn, share and innovate
Dive into Segment's vibrant customer community, where you can connect with peers, gain exclusive insights, and elevate your success with expert guidance and resources!
Using ClickHouse to count unique users at scale
By implementing semantic sharding and optimizing filtering and grouping with ClickHouse, we transformed query times from minutes to seconds, ensuring efficient handling of high-volume journeys in production while paving the way for future enhancements.