Using the Web SDK

Objective

After completing this lesson, you will be able to develop and debug applications with the Javascript Web SDK methods and events.

Understanding the Web SDK

The SAP Customer Data Cloud Web SDK provides a JavaScript library that simplifies the integration of SAP Customer Data Cloud functionalities into web applications.

It acts as a wrapper around the SAP Customer Data Cloud's REST API, making it easier to manage user identities and access user data.

SAP Customer Data Cloud Service architecture diagram illustrating API integration between a client application and cloud server components through Web SDK and API layer.

Where <DC> is:

  • us1.gigya.com - For the US data center.

  • eu1.gigya.com - For the European data center.

  • au1.gigya.com - For the Australian data center.

  • eu2.gigya.com - For the European Azure data center.

  • cn1.sapcdm.cn - For the Chinese data center.

  • global.gigya.com - For all Global sites or site groups.

Alternatively, when loading the Web SDK on US, EU or AU sites, or when using the global domain, you can use the following structure:

JavaScript
1
<script type="text/javascript" lang="javascript" src="https://cdns.gigya.com/js/gigya.js?apikey=<A_Valid_API_Key_For_Your_Site>"></script>

The gigya.jssrc (source) must be loaded in the <head> of every page within your website or webapp that uses SAP Customer Data Cloud services and should be the first element, whenever possible. And it should only be called once on any page.

By including gigya.js in your code, your JavaScript code gets access to the gigya.* JavaScript API namespace.

Core Methods of the Web SDK

The Web SDK offers a range of methods organized under different namespaces such as gigya.socialize, gigya.accounts, gigya.ds, etc. thus, all methods are referenced with the gigya. prefix.

A diagram illustrating SAP Customer Data Cloud accounts API methods hierarchy including showScreenset, addEventHandler, getAccountInfo, and logout functions with method call syntax notation.

All the methods in the SAP Customer Data Cloud Web SDK have a common signature:

gigya.<namespace>.<method_name>(params);

For example, gigya.socialize.showShareBarUI(params)

The params object is a Javascript object that may contain multiple member fields. 

To implement Customer Identity manually via Javascript (without the use of any module, cartridge, plugin, etc.), we need to master four methods offered by the Web SDK:

  • gigya.accounts.showScreenset(): Injects the HTML code of a screen-set into the webpage's DOM, allowing you to display registration, login, and profile update forms. Screen-sets are pre-built UI components that handle common identity management tasks.

  • gigya.accounts.addEventHandler(): Listens for user login and logout events, enabling you to trigger actions based on these events. This allows you to customize the user experience based on their authentication status.

  • gigya.accounts.getAccountInfo(): Checks for an active user session and provides access to the user's profile data if a session exists. A response.errorCode of 0 indicates that a user is logged in.

  • gigya.accounts.logout(): Logs the user out of the current session. This ensures that user sessions are properly terminated when the user is finished.

Note

After the Web SDK completes loading, it will then call the onGigyaServiceReady function.

For SAP Customer Data Cloud Web SDK methods to function properly, they can only be called after the SAP Customer Data Cloud Web SDK finishes loading altogether.

To ensure that the SAP Customer Data Cloud Web SDK is always loaded before any method calls on your webpage, you can place all SAP Customer Data Cloud-related Javascript functions inside a handler and then call that handler using the onGigyaServiceReady variable. It is a variable that the SAP Customer Data Cloud Web SDK always checks for as soon as it completes loading.

Handling Events for the Web SDK

The SAP Customer Data Cloud service generates many events driven by user interactions such as user login, button clicks, etc. It also generates global application events whenever they occur, regardless of the action that triggered the event.

Applications may register event handlers that listen for particular events and execute code when these events are received.

A list of SAP Customer Data Cloud event handlers including onLogin, onLogout, onConnectionAdded, onConnectionRemoved, and onError.

These are the available global application events:

  • onLogin - Fired when a user successfully logs in to SAP Customer Data Cloud.

  • onLogout - Fired when a user logs out from SAP Customer Data Cloud.

  • onConnectionAdded - Fired when a user is connected to a provider.

  • onConnectionRemoved - Fired when a user is disconnected from a provider.

  • onError - Fired if an error occurs when loading the Web SDK.

Here’s an example code snippet for defining and registering event handlers using the Web SDK.

Event handler code examples demonstrating function signatures and addEventHandlers registration syntax for onLogin and onLogout events.

Having defined an event handler, you can register it for an event using the accounts.addEventHandlers or socialize.addEventHandlers Web SDK methods. Like any other Web SDK method, addEventHandlers receives the params object.

You may register one event handler (one function) for several events and several events within one addEventHandlers method call.

For more information, refer to the Events documentation in the SAP Help Portal.

Debugging with the Web SDK

During development, it's crucial to monitor the Web SDK calls and responses occurring in the background.

SAP Customer Data Cloud debug console displaying API callbacks, event logs, timestamped method calls, and browser console interface for development and troubleshooting.

You may encounter an error during user registration and want to know if SAP Customer Data Cloud provided an error code or a more detailed technical message.

To view this within the browser, SAP Customer Data Cloud has a debug widget available within the Web SDK:

gigya.showDebugUI()

To use the debug widget:

  1. Go to your browser's console (on a Web SDK-ready page)

    • Chrome: Ctrl + Shift + J (Windows), Cmd + Option + J (Mac)

    • Firefox: Ctrl + Shift + K (Windows), Cmd + Option + K (Mac)

  2. Execute the method, gigya.showDebugUI()

  3. You will see a widget at the top of the page that will logs all Web SDK calls made to SAP Customer Data Cloud along with the responses.

Web SDK Configuration

The Web SDK Configuration tab in the SAP Customer Data Cloud Console allows you to set configurations that apply to all webpages of your site or site group that load the Web SDK. This provides a centralized location for managing global settings.

Web SDK Configuration illustrating site configuration setup, variable initialization, and global application scope with annotated code example.

To access the Web SDK Configuration tab in the Console, navigate to the Web SDK Configuration by selecting Advanced → WebSDK Configuration on the left-hand menu.

These configurations (previously called Global Configuration) apply to all the webpages of your site or site group, which loads the Web SDK. You can manually add additional parameters to the global Conf JSON object page-by-page if necessary. You can add any supported parameters to the Web SDK conf object supported by any Javascript APIs. When calling an API via the Web SDK, if the API accepts a parameter that is predefined in the conf object, it uses the values there unless overridden in the actual API call.

The parameters defined in the Web SDK Configuration work by default on mobile devices. This provides great flexibility, such as defining custom buttons for an OIDC provider across your login screen devices.

Web SDK Configuration defines the configuration for your site or site group, accessible by the SAP Customer Data Cloud Web SDK, hosted by SAP Customer Data Cloud, and edited within the Console.

Use Cases for Web SDK Configuration include:

  • A standard event map exists by default and is used by gigya.js. It is possible to override it by defining the following method: customEventMap.

  • Let's define whether to enable or disable the accepted social providers: enabledProviders: '*' where the * means all social providers. Here's is an example for just Facebook: { enabledProviders: ['facebook'] }

  • Force the screen sets to be displayed in a specific language despite a different website default language. Here's is an example to force the language of the screen sets in French for France: { lang: "fr-fr" }

  • Control the user session expiration. For example { sessionExpiration: -2},  that means the session never expires.

  • The autoLogin parameter determines if, after a user logs in once to the site and revisits the same site, the user is automatically logged in or if the user is logged into Facebook or Google+ at that time.

  • Create custom buttons for SAML providers.

  • The user is forced to provide social network credentials during login when forceAuthentication is set to True.

Here's the sample code:

JavaScript
123456789
customEventMap: { eventMap: [{ events: '*', // Allow all events to be called args: [function(e) { // Do whatever you need to do here return e; }],

Note

SessionExpiration  values can be:

 0: Expire when the browser closes.

-1: Expires after 60 seconds.

-2: Never expires.

By default, AutoLogin is set to False. You need to set it to True to enable this feature.

  • Key advice for using the Web SDK Config:

    • The gigya.js src must be loaded in the <head> of every page within your site that uses SAP Customer Data Cloud services (better if it is loaded as the first element).

    • The gigya.js src should only be called once on any page.

    • There is a cache (approx. 4 hours) - You will not see immediate changes.

    • Web SDK configurations are applied at the moment the gigya.js is loaded and initialized on the page.

  • Any variable can be set in the Web SDK configuration and reached on every website that uses the same API KEY.

  • The Web SDK configuration field only accepts a JSON definition, so JavaScript logic must be contained in the JSON structure.

For more information, refer to the List of all possible attributes and the Web SDK documentation in the SAP Help Portal.

Best Practices for Using the Web SDK

  1. All of the Customer Identity functionality is defined under the accounts namespace.

  2. SAP Customer Data Cloud's general approach to consume Web SDK follows a well-known convention:

    1. Define a config JSON object that contains all required and optional fields relevant to the method and use case.

    2. Some optional fields that you can add in the config JSON are custom events pointing to an existing callback function.

    3. Call the SAP Customer Data Cloud Javascript method with the config JSON object as a parameter.

Flowchart illustrating SAP Customer Data Cloud configuration workflow with code example showing JSON config object setup, custom event parameters, and method invocation syntax.

Summary

  • The Web SDK simplifies integrating SAP Customer Data Cloud functionalities into web applications by providing a JavaScript wrapper around the REST API.

  • Key methods like gigya.accounts.showScreenset(), gigya.accounts.addEventHandler(), gigya.accounts.getAccountInfo(), and gigya.accounts.logout() are essential for implementing user identity management.

  • The onGigyaServiceReady function ensures that SAP Customer Data Cloud methods are called only after the Web SDK has fully loaded.

  • Event handlers can be registered to respond to user interactions like login and logout, allowing for dynamic updates to the user interface.

  • The Web SDK Configuration tab in the SAP Customer Data Cloud Console allows you to set global configurations that apply to all webpages using the Web SDK.