Loyalty Geofence

Overview

Geofencing is a technology that defines a virtual boundary around a real-world geographical area. A geofence establishes a radius of interest that can trigger an action on a geo-enabled mobile device.

Use the Loyalty Geofence service to define boundaries and to configure triggers when a device enters or exits the defined boundaries. The service has many practical uses. Just one example is to create a geofence around a retail store. When a customer, who has downloaded a mobile app on a smartphone, enters the store with the phone, the system can send a coupon to the smartphone.

To define and configure geofencing boundaries and triggers, you must have an administrator role for projects that use the Loyalty Geofence service.


API Reference

/geoFence

This resourceType defines the GET, POST, DELETE methods and their responses for a collection resource.

/geoFence

get

Get GeoFences Data

Accepted scopes:

  • 'sap.loygeofence_view' - Required to get all Geofence
post

Creates a new Geo Fence and replicated the same into Elastic Search

Accepted scopes:

  • 'sap.loygeofence_manage' - Required to post Geofence

/geoFence/aggr

get

Gets Average, Sum and Count aggregation for Geofences

Accepted scopes:

  • 'sap.loygeofence_view' - Required to gets average, sum and count aggregation for Geofence

/geoFence/{geoId}

This resource type defines the GET, PUT, DELETE methods and their responses for a single element resource.

get

Gets a specific geofence based on geoId provided.

Accepted scopes:

  • 'sap.loygeofence_view' - Required to view Geofence by ID
put

Updates the specific geofence and the same in Elastic Search

Accepted scopes:

  • 'sap.loygeofence_manage' - Required to view Geofence by ID
delete

Deletes a specific geofence based on geoId from Document Repository and Elastic Search

Accepted scopes:

  • 'sap.loygeofence_delete' - Required to delete Geofence

/geoFence/index

post

Creates all geofence indices required for Elastic Search

Accepted scopes:

  • 'sap.loygeofence_manage' - Required to create elastic search indices for geofence


Scopes

Scopes are strings that you can use to specify the type of access you need to resources and operations in the Loyalty Geofence service.

You must provide a scope that allows users to perform operations. Grant the scopes in an access token from the OAuth2 service. For more information about the authorization and authentication used in SAP Hybris services, and also about the scopes in general, see:

- Authorization
- Scopes

The table shows the scopes that the Loyalty Geofence service supports:

ScopeDescription
sap.loygeofence_viewUse this scope to view existing geofences.
sap.loygeofence_manageUse this scope to create and modify existing geofences.
sap.loygeofence_deleteUse this scope to delete an existing geofence.

If an operation requires two scopes, both scopes must be assigned to the role of the user who performs the operation. Here are two example scenarios.

  1. To allow a user to view a specified geofence, as well as change and update the geofence properties, you must assign the sap.loygeofence_view and sap.loygeofence_manage scopes to the user's role.
  2. To allow a user to view and delete a specified geofence, the user's role must include the sap.loygeofence_view and sap.loygeofence_delete scopes.


Perform Simple CRUD Operations on Geofence Object

Introduction

You can define and manage your geofences in the Loyalty Geofence service. This tutorial shows how to manage geofences using the basic create, read, update, and delete operations.

Example

assert = chai.assert;
clientId = clientIdPlaceholder;
clientSecret = clientSecretPlaceholder;
tenant = projectIdPlaceholder;
client = appIdPlaceholder;
scopesRequired = 'hybris.tenant='+tenant+' sap.loygeofence_view sap.loygeofence_manage sap.loygeofence_delete';
token = tokenPlaceholder;

Get an access token

To perform CRUD operations, you must have proper authorization, which requires a valid access token in the request header. To obtain an access token, create an API Client for the OAuth2 service:

API.createClient('oAuth2Service',
'/services/oauth2/v1/api.raml');

Now get the token

AccessToken = oAuth2Service.token.post({
  'client_id' : clientId,
  'client_secret': clientSecret,
  'grant_type' : 'client_credentials',
  'token_type': 'Bearer',
  'scope': scopesRequired
});
access_token = AccessToken.body.access_token;

Create API client for Loyalty Geofence service

API.createClient('geofenceService',
'/services/loyaltygeofence/v1/api.raml');

Create a Geofence object

geofence_obj = geofenceService.geoFence.post({
   "description":"Recife San Jose",
   "latitude":-8.065178393196224,
   "longitude":-34.88262176513671,
   "name":"Recife Sao Jose",
   "radius":900,
   "transitionType":1
}, {
  headers: {
    'Authorization': 'Bearer ' + access_token,
    'Content-type' : 'application/json'
           }
    }
  )
locationRef = geofence_obj.headers.location.toString();
geoId = locationRef.substring(locationRef.lastIndexOf('/')+1,locationRef.length);

Retrieve object created with the previous step


getGeofenceObj = geofenceService.geoFence.geoId(geoId).get(null, {
  headers: {
    'Authorization': 'Bearer ' + access_token,
    'Content-type' : 'application/json'
           }
    }
  )

Update the Geofence object


putResponse = geofenceService.geoFence.geoId(geoId).put({
   "description":"Recife San Jose",
   "latitude":-8.065178393196224,
   "longitude":-34.88262176513671,
   "name":"Recife Sao Jose",
   "radius":1000,
   "transitionType":1
}, {
  headers: {
    'Authorization': 'Bearer ' + access_token,
    'Content-type' : 'application/json'
  }
})

Retrieve the same Geofence object to ensure that proper information is updated

Get the Geofence object to make sure that it was updated.


getGeofenceResponse = geofenceService.geoFence.geoId(geoId).get(null, {
  headers: {
    'Authorization': 'Bearer ' + access_token,
    'Content-type' : 'application/json'
           }
    }
  )

Delete the above Geofence Object



deleteResponse = geofenceService.geoFence.geoId(geoId).delete(null, {
  headers: {
   'Authorization': 'Bearer ' + access_token,
  }
})

  assert.equal(deleteResponse.status, 204);

Retrieve the deleted Geofence object to ensure it is really deleted

response= geofenceService.geoFence.geoId(geoId).get(null, {
  headers: {
    'Authorization': 'Bearer ' + access_token,
    'Content-type' : 'application/json'
           }
    }
  )

   assert.equal(response.status, 404);


  • Send feedback

    If you find any information that is unclear or incorrect, please let us know so that we can improve the Dev Portal content.

  • Get Help

    Use our private help channel. Receive updates over email and contact our specialists directly.

  • hybris Experts

    If you need more information about this topic, visit hybris Experts to post your own question and interact with our community and experts.