Complete example
This is the client side: initialize, identify the user, request permission, start background tracking, receive events, and read current state each time the app becomes active.Prerequisites
- A Radar project and its Test publishable key. See Choose the right API key.
- The Radar SDK for iOS, Android, web, React Native, or Flutter. On iOS and Android, initialize it in
AppDelegateorApplication.onCreate(), because the operating system can launch your app in the background. - A stable user ID for
Radar.setUserId(). See Handle login, logout, and user switching.
Create geofences
A geofence is identified by itstag (a group you choose, such as store) and its externalId (your ID for the location, such as store-42). Every import path upserts on that pair, so to change a location, send it again. Geofences live in one environment: import into Test while you build, then into Live.
To create a few geofences by hand, open the Geofences page and click Create. For “near the store” messages, add a second, larger geofence with the same externalId and a different tag, such as nearby. Put per-location flags, such as "curbside": true, in metadata, which Radar returns with every event.
CSV import
On the Geofences page, click Import and select a CSV file with a header row. The import history page shows the error for each row that failed.
Optional columns, such as
enabled, metadata, operatingHours, stopDetection, and dwellThreshold, match the body parameters of Upsert a geofence. In a CSV, metadata is a JSON string.
Longitude comes before latitude in
coordinates. A swapped pair imports without errors but puts the geofence in the wrong place.API
Call the upsert endpoint from your backend, with a secret key, when a location is created, changed, or closed.enabled=false. For every field, see the Geofences API.
Geofence sync
Geofence sync reads a CSV with the same columns from a location you control and upserts its rows once a night.- On the Integrations page, find Geofence Sync and set Enabled to Yes.
- Enter a Notification Email for the result of each sync.
- Select a Protocol: HTTP for a CSV at a public URL, or AWS S3 for a private bucket, with the access key of an IAM user that has
GetObjectpermission. - Enter a file for Test, for Live, or for both, and click Save and Sync. Radar syncs once immediately, then nightly.
Sync and import both upsert. Rows missing from the file aren’t deleted.
enabled to false, or delete the geofence.
Enable Places
Places detects visits to places in Radar’s dataset by chain (such asstarbucks) and category (such as airport), with no geofence for each one. In the Places section of the Settings page, turn on user.entered_place and user.exited_place under Event types, add chain slugs under Chains and categories under Categories, and save.
user.place is a single place, so branch on place.chain.slug or a category. Place matching, which must be enabled for your project, links a geofence to a Radar place through placeId, so that a circle takes the place’s center and shape.
Request permissions and start tracking
Foreground permission is enough forRadar.trackOnce() on open and resume. Events while the app is closed need background permission and Radar.startTracking() with the responsive preset, called only after background access is granted. See Understand foreground and background location permissions and Tracking options.
On iOS, the responsive preset needs the Location updates and Background fetch background modes. Don’t use the continuous preset for geofence detection. It is meant for trips, and on Android it runs a foreground service, which Google Play doesn’t accept for geofencing alone.
Receive events in the app
trackOnce() returns the events generated by that one update. To receive every event, including those generated in the background, register a delegate on iOS or a receiver on Android at app startup. In Flutter, the listener runs only while the Flutter engine is running.
Turn geofence event types on or off in the Geofences section of the Settings page. To drop entries from users who pass by, turn on Geofence stop detection in the same section, so that only a stop in the geofence generates an entry, or ignore events whose
confidence is below 3 (high).
Radar generates an event when the location update that crosses the boundary arrives: immediately in the foreground, and within the preset’s update interval in the background, or later when the operating system delays updates. Check createdAt, the time the location was recorded on the device, before a time-sensitive action.
Show an on-premise experience
Drive anything the app shows only while the user is at a location, such as a store mode, from current state. CallRadar.trackOnce() each time the app opens or returns to the foreground and read user.geofences and user.place. This covers a user who is already inside when the app opens, when Radar generates no new entry event.
When a later check returns no matching geofence, or the call fails, return to the default experience. Don’t wait for an exit event. On the web, the same check reads a per-location flag from geofence metadata:
JavaScript
Send location-triggered messages
Radar sends events and user attributes server-to-server, so the system that sends the message knows who (user.userId), what (event.type), and where (the geofence tag, externalId, and metadata, or place.chain.slug). Turn on a native integration, such as Braze, or create a webhook to an HTTPS endpoint you control when your backend decides what to send. A webhook handler verifies the request, returns 2xx quickly, and deduplicates on event._id. See Integrations and Webhooks. The ID you pass to Radar.setUserId() must match the messaging platform’s ID for the same person.
Use Radar campaigns
Radar can also send the notification or in-app message itself, from campaigns you create on the Campaigns page. The SDK shows an event-based notification as a local notification, so it needs notification permission and no push credentials. To attribute app opens to a campaign on iOS, setautoLogNotificationConversions in the initialization options. For React Native or Expo, see Set up campaign notifications. For campaign types, targeting, scheduling, and frequency capping, see Campaigns.
Log conversions
A conversion records a business action, such as an order, together with where the user was.revenue and metadata are optional. Radar attaches the geofences, place, and regions from the user’s latest location update, and the conversion appears on the Events page. To log one from your backend, see Log a conversion.
Handle results and errors
Test the integration
- Passing case: Stand inside a Test geofence, or simulate a location inside it on the Simulator page, and cold-start the app. The on-premise experience appears, and
user.entered_geofencereaches your webhook. - Failing case: Deny location permission and open the app. The default experience appears, with no repeated prompt.
Production checklist
- Release builds use the Live publishable key, and the secret key exists only on your backend.
- Geofences, Places, event types, and integrations are set up in Live, and a sync or API update keeps geofences current.
- Tracking starts only after background permission is granted, with the responsive or efficient preset.
- The webhook handler verifies requests and deduplicates on
event._id.
Build with an agent
Copy this prompt into a coding agent that has access to your repository.See also
- Geofences API and Events API
- Understand geofences, places, regions, beacons, and fraud context, for regions and user insights
- Track pickups, deliveries, and workers