Skip to Content
SAP Mobile Platform

SAP Precision Marketing Hybrid SDK

Prerequisites

Follow these steps to get the SDK and set up an SPM account:
  1. Sign up for a Free Trial account on SDN: http://scn.sap.com/docs/DOC-35829
  2. Once you have accepted the EULA you will receive your SPM account, developer credentials and a link where you can download the SDK
  3. Use the developer key/secret you received in the previous step when you connect to the Device API (see below). If you want to run the unit tests, please insert your credentials in the UnitTests/global.js file and follow the instructions in the README.
Development
Including DeviceAPI SDK
In your HTML file, include the following JavaScript files.
Insert the following between the head tags, :
<script src="deviceapi/Request.js"></script>
<script src="deviceapi/DeviceAPI.js"></script>
Instantiating DeviceAPI SDK
To use the DeviceAPI in your JavaScript, you have to instantiate it. By doing so, you create an object that contains the necessary information to connect to the server and the available methods of the SDK as well. The constructor is the following:
DeviceAPI(<hostURL>, <developerId>, <developerSecret>, <timeOut>)
hostURLString that contains the URL to the DeviceAPI host.
developerId
Identifier that belongs to your developer account. (You can look up your id in
the email)
developerSecretSecret that belongs to your developer account.
timeOutDuration until API-Requests time out (in ms)
Use the following Code snippet in your JavaScript to create the SDK Object:
var
deviceAPI = new DeviceAPI('https://sprdeviceapisstaging.hana.ondemand.com', 'yourid', 'yoursecret', 10000);
Register Consumer
After instantiating the API you now need to register a consumer. The consumer represents the person that wants to use SPM. A consumer can be either
authenticated or anonymous. An authenticated consumer registers using E-Mail, first name, and last name. The consumer can later login again and look up the favored offers or shopping list. The anonymous consumer is temporary and does not need to provide any authentication.
registerConsumer(<type>, <parameters>, <asynchronous>,  <callbackFunction>);
type
Defines the Registration Type (anonymous, authenticated, ...). Defined in DeviceAPI.
parameters
A JavaScript object that contains the necessary information for the registration.
asynchronous
Boolean value that specifies whether the method should be called asynchronously (true) or synchronously (false).
callbackFunction
The provided function will be called after the request finished. Can be null.
If you perform a synchronous call, the function returns the response . If you perform an asynchronous call, then you can use the response as a parameter in the callback function.
Example for Register Consumer:
var
parametersRegisterConsumer = {
hardwaremanufacturer: "manufacturer",
hardwaremodel: "model",
screenwidth: screen.width,
screenheight: screen.height,
osname: "Desktop",
osversion: "0.0.0.0"
};
var
response = deviceAPI.registerConsumer(DeviceAPI.REGISTER_ANONYMOUSLY, parametersRegisterConsumer, false, null);
In this example, we create an object with the necessary information for the registerConsumer method and call the method on the deviceAPI object. The registration type is as follows:
  • Anonymous
  • Asynchronous call with no callback function
The response is stored in the response variable. If you want to use this snippet, then you have to insert your Developer ID.
Create Session
Once we have a registered consumer, we need a session (it lasts 30 minutes) to access the API methods. To do so, you have to call the createSession
method as follows.
createSession(<type>, <parameters>, <asynchronous>, <callbackFunction>)
parametersObject, which contains the needed information to create a session.
typeSpecifies the session type (authenticated, limited). Defined in DeviceAPI.
asynchronous

Boolean value that specifies whether the method should be called asynchronously

(true) or synchronously (false).

callbackFunctionThe provided function will be called after the request finished. Can be null.
The function returns the response if you perform a synchronous call. If you perform an asynchronous call, then you can use the response as a parameter
in the callback function.
For Example:

var response = deviceAPI.createSession(DeviceAPI.CREATE_SESSION_LIMITED, { locale: 'en_CA' }, false, null);

After the createSession method call we should have (we could log the response for information on success or failure) a limited session for our anonymous consumer. Now we can access the other methods of the DeviceAPI.
Get Offers
To retrieve offers you need to call the getOffers method.
getOffers(<offersFrom>, <asynchronous>, <callbackFunction>)
offersFrom

You can specify a store or an area from which you want to retrieve offers. If

null, you will get all offers.

asynchronous

Boolean value that specifies whether the method should be called asynchronously

(true) or synchronously (false).

callbackFunctionThe provided function will be called after the request finished. Can be null.
Example for the usage of the method:

var

response = deviceAPI.getOffers(null, false, null);

if(response.data) var offers = response.data.offers; 

if(offers && offers.length > 0) {

          alert(offers[0].description);

} else {

          alert(“No offers found or retrieved”);

In this example we retrieve all offers in a synchronous call. Then we have to check if we have received any data from the API (successful call). If yes, then we display the description of the first offer in an alert popup.

No comments