Available since version 2.0.0, Pilgrim SDK allows geofencing around a configurable set of venues or points. Geofences can be set for the venues, categories, or chains of your choosing. By default, Foursquare will use its knowledge of the venue’s location to return the right fence for a given place. You may also configure a radius of your choosing, but we recommend letting Pilgrim pick the right one to give you the optimal experience.

In SDK version 2.2.0, support for polygon shapes and arbitrary latitude/longitude points is also available. This allows greater customization and removes the reliance upon geofencing only Foursquare venue locations.

Why use Geofences

For use cases that rely on accuracy, regular Pilgrim SDK place visit detection is superior to geofencing. However, there are certain instances where geofencing is preferable:

  • When speed and proximity to a specific subset of venues is more important than accurately detecting visits there.
  • When it’s more important to know if a user is nearby a certain place rather than visiting.
  • When you want to track when users that are en-route to a specific venue, and you want to know as soon as they arrive.

Geofencing runs independently alongside regular Pilgrim SDK visit detection so you can still get the benefit of understanding everywhere your users go while enabling additional use cases.

Event Types

Geofences have five potential event types that are delivered directly to the client and through an optional webhook:

Event Type Description
entrance Triggered on the first GPS signal that is received inside of the geofence.
dwell Triggered after the user has “dwelled” within the geofence for a configurable length of time. Default is 1 minute.
venue confirmed Triggered when the device has dwelled inside a geofence radius and confirmed a stop at the venue within the radius. Only available in SDK versions 2.1.2 or greater
exit Triggered on the first GPS signal that is received outside of the geofence.
presence Triggered when the device is in a geofence radius during a get location request. Only available in SDK versions 2.1 or greater

Receiving Events

To receive geofence events on the client, add the event handler below:

// In your implementation of the pilgrim delegate, add:
func pilgrimManager(_ pilgrimManager: PilgrimManager, handle geofenceEvents: [GeofenceEvent]) {
    // Code to handle geofenceEvents...
}
// In your implementation of the PilgrimNotificationHandler, add:
@Override
public void handleGeofenceEventNotification(Context context, PilgrimSdkGeofenceEventNotification pilgrimSdkGeofenceEventNotification) {
    List<GeofenceEvent> geofenceEvents = pilgrimSdkGeofenceEventNotification.getGeofenceEvents();
    // Code to handle geofenceEvents...
}
    

Geofences will need to be set to a specific Foursquare venue, chain, category or shape (see below). For example, a partner could set up geofences for Foursquare HQ, all coffee shops, and all Chick-fil-a’s. You can also set up a geofence for an arbitrary lat/lng or custom polygon shapes. For Foursquare venues, this means that some venue harmonization may need to take place beforehand if you already have a specific list of places you want to geofence.

Note: Geofences are set up globally across all users of the app. User specific geofences are not yet something we provide.

A geofence event will contain the following:

Field Description
eventType entrance, dwell, venueConfirmed, exit, or presence.
venue Same as regular pilgrim venue object.
categoryIDs Array of categoryIDs used by triggered geofence.
chainIDs Array of chainIDs used by triggered geofence.
partnerVenueID String of harmonized venueId.
location Object containing location information about the geofence event.
timestamp Unix/epoch timestamp in milliseconds of when the event occured.

Webhook Examples

{
    "eventType":"geofenceEnter",
    "timestamp":1545078269223,
    "geofenceEvent":{
        "eventType":"entrance",
        "eventLat":41.8893897,
        "eventLng":-87.6297896,
        "radius":100.0,
        "venueId":"4a9037fef964a5209b1620e3",
        "categoryIds":"4bf58dd8d48988d1e0931735",
        "chainIds":"",
        "venues":[
            {
                "id":"4a9037fef964a5209b1620e3",
                "name":"Einstein Bros Bagels",
                "location":{
                    "address":"400 N Dearborn St",
                    "crossStreet":"",
                    "city":"Chicago",
                    "state":"IL",
                    "postalCode":"60654",
                    "country":"US",
                    "lat":41.8893897,
                    "lng":-87.6297896
                },
                "categories":[
                    {
                        "id":"4bf58dd8d48988d179941735",
                        "name":"Bagel Shop",
                        "pluralName":"Bagel Shops",
                        "shortName":"Bagels",
                        "icon":{
                            "prefix":"https://ss3.4sqi.net/img/categories_v2/food/bagels_",
                            "suffix":".png"
                        }
                    },
                    {
                        "id":"4bf58dd8d48988d1e0931735",
                        "name":"Coffee Shop",
                        "pluralName":"Coffee Shops",
                        "shortName":"Coffee Shop",
                        "icon":{
                            "prefix":"https://ss3.4sqi.net/img/categories_v2/food/coffeeshop_",
                            "suffix":".png"
                        }
                    }
                ],
                "venueChains":[
                    {
                        "id":"556ce8d9aceaff43eb0588d6",
                        "name":"Einstein Bros."
                    }
                ]
            }
        ]
    },
    "lat":41.88928251303856,
    "lng":-87.62870316744883,
    "user":{
        "adid":"40C23EBD-E21A-4ACC-A915-B1C80BDAF4FE",
        "userId":"CB25EFFB-C2B2-4E0F-B0E9-55C05BEFA4D4"
    },
    "installId":"CB25EFFB-C2B2-4E0F-B0E9-55C05BEFA4D4",
    "sdkType":"iossdk"
}
{
    "eventType":"geofenceDwell",
    "timestamp":1545078408140,
    "geofenceEvent":{
        "eventType":"dwell",
        "eventLat":41.8893897,
        "eventLng":-87.6297896,
        "radius":100.0,
        "venueId":"4a9037fef964a5209b1620e3",
        "categoryIds":"4bf58dd8d48988d1e0931735",
        "chainIds":"",
        "venues":[
            {
                "id":"4a9037fef964a5209b1620e3",
                "name":"Einstein Bros Bagels",
                "location":{
                    "address":"400 N Dearborn St",
                    "crossStreet":"",
                    "city":"Chicago",
                    "state":"IL",
                    "postalCode":"60654",
                    "country":"US",
                    "lat":41.8893897,
                    "lng":-87.6297896
                },
                "categories":[
                    {
                        "id":"4bf58dd8d48988d179941735",
                        "name":"Bagel Shop",
                        "pluralName":"Bagel Shops",
                        "shortName":"Bagels",
                        "icon":{
                            "prefix":"https://ss3.4sqi.net/img/categories_v2/food/bagels_",
                            "suffix":".png"
                        }
                    },
                    {
                        "id":"4bf58dd8d48988d1e0931735",
                        "name":"Coffee Shop",
                        "pluralName":"Coffee Shops",
                        "shortName":"Coffee Shop",
                        "icon":{
                            "prefix":"https://ss3.4sqi.net/img/categories_v2/food/coffeeshop_",
                            "suffix":".png"
                        }
                    }
                ],
                "venueChains":[
                    {
                        "id":"556ce8d9aceaff43eb0588d6",
                        "name":"Einstein Bros."
                    }
                ]
            }
        ]
    },
    "lat":41.889335266663636,
    "lng":-87.62856037188838,
    "user":{
        "adid":"40C23EBD-E21A-4ACC-A915-B1C80BDAF4FE",
        "userId":"CB25EFFB-C2B2-4E0F-B0E9-55C05BEFA4D4"
    },
    "installId":"CB25EFFB-C2B2-4E0F-B0E9-55C05BEFA4D4",
    "sdkType":"iossdk"
}
{
    "eventType":"geofenceVenueConfirmed",
    "timestamp":1545078428140,
    "geofenceEvent":{
        "eventType":"venueConfirmed",
        "eventLat":41.8893897,
        "eventLng":-87.6297896,
        "radius":100.0,
        "venueId":"4a9037fef964a5209b1620e3",
        "categoryIds":"4bf58dd8d48988d1e0931735",
        "chainIds":"",
        "venues":[
            {
                "id":"4a9037fef964a5209b1620e3",
                "name":"Einstein Bros Bagels",
                "location":{
                    "address":"400 N Dearborn St",
                    "crossStreet":"",
                    "city":"Chicago",
                    "state":"IL",
                    "postalCode":"60654",
                    "country":"US",
                    "lat":41.8893897,
                    "lng":-87.6297896
                },
                "categories":[
                    {
                        "id":"4bf58dd8d48988d179941735",
                        "name":"Bagel Shop",
                        "pluralName":"Bagel Shops",
                        "shortName":"Bagels",
                        "icon":{
                            "prefix":"https://ss3.4sqi.net/img/categories_v2/food/bagels_",
                            "suffix":".png"
                        }
                    },
                    {
                        "id":"4bf58dd8d48988d1e0931735",
                        "name":"Coffee Shop",
                        "pluralName":"Coffee Shops",
                        "shortName":"Coffee Shop",
                        "icon":{
                            "prefix":"https://ss3.4sqi.net/img/categories_v2/food/coffeeshop_",
                            "suffix":".png"
                        }
                    }
                ],
                "venueChains":[
                    {
                        "id":"556ce8d9aceaff43eb0588d6",
                        "name":"Einstein Bros."
                    }
                ]
            }
        ]
    },
    "lat":41.889335266663636,
    "lng":-87.62856037188838,
    "user":{
        "adid":"40C23EBD-E21A-4ACC-A915-B1C80BDAF4FE",
        "userId":"CB25EFFB-C2B2-4E0F-B0E9-55C05BEFA4D4"
    },
    "installId":"CB25EFFB-C2B2-4E0F-B0E9-55C05BEFA4D4",
    "sdkType":"iossdk"
}
{
    "eventType":"geofenceExit",
    "timestamp":1545091987992,
    "geofenceEvent":{
        "eventType":"exit",
        "eventLat":41.8893897,
        "eventLng":-87.6297896,
        "radius":100.0,
        "venueId":"4a9037fef964a5209b1620e3",
        "categoryIds":"4bf58dd8d48988d1e0931735",
        "chainIds":"",
        "venues":[
            {
                "id":"4a9037fef964a5209b1620e3",
                "name":"Einstein Bros Bagels",
                "location":{
                    "address":"400 N Dearborn St",
                    "crossStreet":"",
                    "city":"Chicago",
                    "state":"IL",
                    "postalCode":"60654",
                    "country":"US",
                    "lat":41.8893897,
                    "lng":-87.6297896
                },
                "categories":[
                    {
                        "id":"4bf58dd8d48988d179941735",
                        "name":"Bagel Shop",
                        "pluralName":"Bagel Shops",
                        "shortName":"Bagels",
                        "icon":{
                            "prefix":"https://ss3.4sqi.net/img/categories_v2/food/bagels_",
                            "suffix":".png"
                        }
                    },
                    {
                        "id":"4bf58dd8d48988d1e0931735",
                        "name":"Coffee Shop",
                        "pluralName":"Coffee Shops",
                        "shortName":"Coffee Shop",
                        "icon":{
                            "prefix":"https://ss3.4sqi.net/img/categories_v2/food/coffeeshop_",
                            "suffix":".png"
                        }
                    }
                ],
                "venueChains":[
                    {
                        "id":"556ce8d9aceaff43eb0588d6",
                        "name":"Einstein Bros."
                    }
                ]
            }
        ]
    },
    "lat":41.88709567122635,
    "lng":-87.63105850113463,
    "user":{
        "adid":"40C23EBD-E21A-4ACC-A915-B1C80BDAF4FE",
        "userId":"CB25EFFB-C2B2-4E0F-B0E9-55C05BEFA4D4"
    },
    "installId":"CB25EFFB-C2B2-4E0F-B0E9-55C05BEFA4D4",
    "sdkType":"iossdk"
}
{
    "eventType":"geofencePresence",
    "timestamp":1549553712000,
    "geofenceEvent":{
        "eventType":"presence",
        "eventLat":41.8893897,
        "eventLng":-87.6297896,
        "radius":100.0,
        "venueId":"4a9037fef964a5209b1620e3",
        "categoryIds":"4bf58dd8d48988d1e0931735",
        "chainIds":"",
        "venues":[
            {
                "id":"4a9037fef964a5209b1620e3",
                "name":"Einstein Bros Bagels",
                "location":{
                    "address":"400 N Dearborn St",
                    "crossStreet":"",
                    "city":"Chicago",
                    "state":"IL",
                    "postalCode":"60654",
                    "country":"US",
                    "lat":41.8893897,
                    "lng":-87.6297896
                },
                "categories":[
                    {
                        "id":"4bf58dd8d48988d179941735",
                        "name":"Bagel Shop",
                        "pluralName":"Bagel Shops",
                        "shortName":"Bagels",
                        "icon":{
                            "prefix":"https://ss3.4sqi.net/img/categories_v2/food/bagels_",
                            "suffix":".png"
                        },
                        "primary":true
                    },
                    {
                        "id":"4bf58dd8d48988d1e0931735",
                        "name":"Coffee Shop",
                        "pluralName":"Coffee Shops",
                        "shortName":"Coffee Shop",
                        "icon":{
                            "prefix":"https://ss3.4sqi.net/img/categories_v2/food/coffeeshop_",
                            "suffix":".png"
                        }
                    }
                ],
                "venueChains":[
                    {
                        "id":"556ce8d9aceaff43eb0588d6",
                        "name":"Einstein Bros."
                    }
                ]
            }
        ]
    },
    "lat":41.889356,
    "lng":-87.628725,
    "user":{
        "adid":"40C23EBD-E21A-4ACC-A915-B1C80BDAF4FE",
        "userId":"CB25EFFB-C2B2-4E0F-B0E9-55C05BEFA4D4"
    },
    "installId":"CB25EFFB-C2B2-4E0F-B0E9-55C05BEFA4D4",
    "sdkType":"iossdk"
}

Geofence Management

Geofences can be managed through the Geofence builder in your Pilgrim developer console or via our Geofence API.

Was this page helpful?
Yes
No
Thank you!