Skip to main content

CRM Module

The CRM Module provides functionality for managing web push notifications and in-app notifications in the Gamanza Engage Web Client SDK. It allows developers to register players for web push notifications, manage notification permission requests, and handle OneSignal in-app push notifications.

Accessing the Module

The CrmModule is initialized internally by the SDK and should not be instantiated directly. It's accessible through the main SDK instance.

// Example of accessing the CrmModule through the SDK
const { Crm } = GamanzaEngageWebClientSDK.getInstance();

Methods

playerRegisterWebPushSubscription

Registers the current authenticated player for Web Push Notifications powered by Gamanza Engage CRM. This function registers the service worker, validates if the player user agent supports push, and additionally validates if the permissions for Notifications have been granted or denied.

Signature:

playerRegisterWebPushSubscription(): Promise<{
status: WebPushSubscriptionRequestStatus;
}>

Returns:

TypeDescription
Promise<{ status: WebPushSubscriptionRequestStatus }>A promise that resolves to an object containing the status of the web push subscription request.

Example:

// Register for web push notifications
const handleRequestPermissionsClick = async (evt) => {
evt.preventDefault();
// This call displays the native user agent push notifications permissions request
const result = await Crm.playerRegisterWebPushSubscription();
if (result.status === WebPushSubscriptionRequestStatus.SUCCESS) {
alert('Congratulations, you are subscribed to receive Push Notifications');
} else if (result.status === WebPushSubscriptionRequestStatus.WEB_PUSH_NOT_SUPPORTED) {
alert('Web Push Notifications are not supported in this browser');
} else if (result.status === WebPushSubscriptionRequestStatus.WEB_PUSH_PERMISSION_DENIED) {
alert('Permission to receive notifications was denied');
}
};

// Attach to a button click event
document.getElementById('notification-permission-btn').addEventListener('click', handleRequestPermissionsClick);

Notes:

  • For Safari, the request needs to be behind a user interaction. When the user completes the gesture, call the push subscription method immediately from the gesture's event handler code.
  • For requirements, check the Web Push Configuration documentation.

shouldShowNotificationRequestPermissionsBanner

Helper function to allow you to decide when to show the permissions request banner for Web Push Notifications. This is particularly useful for Safari, where the push subscription method must be called immediately from a user gesture's event handler code.

Signature:

shouldShowNotificationRequestPermissionsBanner(days: number = 30, override: boolean = true): Promise<boolean>

Parameters:

NameTypeDefaultDescription
daysnumber30Number of days to wait before showing the banner again. The system stores this value in persistent storage.
overridebooleantrueDefines if you want to write in the persistence storage the next time the function should return true for the current player.

Returns:

TypeDescription
Promise<boolean>A promise that resolves to a boolean indicating whether the notification request permissions banner should be shown.

Example:

Automatic check and override the next check:

// Check if we should show the notification permissions banner
if (await Crm.shouldShowNotificationRequestPermissionsBanner(5)) {
// Display the banner in the UI
showNotificationBanner();
}

Control override in persistence:

// Check if we should show the banner without updating the persistence
if (await Crm.shouldShowNotificationRequestPermissionsBanner(5, false)) {
// Display the banner in the UI
showNotificationBanner();
}

// Handler for player closing the banner
const onCloseBannerClick = async (evt) => {
evt.preventDefault();
hideNotificationBanner();

// Update the next time the banner should be displayed to 5 days
await Crm.shouldShowNotificationRequestPermissionsBanner(5, true);
};

playerOneSignalInAppPushNotificationsSubscription

Registers the current authenticated player for OneSignal In-App Push Notifications. This method allows registering a player to receive push notifications through OneSignal within the application.

Signature:

playerOneSignalInAppPushNotificationsSubscription(subs: OneSignalInAppNotificationSubscription): Promise<SimpleResponse<null>>

Parameters:

NameTypeDescription
subsOneSignalInAppNotificationSubscriptionThe OneSignal subscription details.

Returns:

TypeDescription
Promise<SimpleResponse<null>>A promise that resolves to a SimpleResponse object indicating success or failure of the registration.

Example:

const subscription = {
appId: "your-onesignal-app-id",
subscriptionId: "subscription-id",
playerId: "player-id",
deviceType: "web",
oneSignalId: "onesignal-id"
};

const result = await Crm.playerOneSignalInAppPushNotificationsSubscription(subscription);
if (result.ok) {
console.log("Successfully registered for OneSignal notifications");
} else {
console.error("Failed to register for OneSignal notifications:", result.error);
}

OneSignalInAppNotificationSubscription

Interface representing the subscription details for OneSignal In-App Push Notifications.

type OneSignalInAppNotificationSubscription = {
appId: string;
subscriptionId: string;
playerId: string;
deviceType: string;
oneSignalId: string;
};
PropertyTypeDescription
appIdstringThe OneSignal application ID
subscriptionIdstringThe OneSignal subscription identifier
playerIdstringThe player's unique identifier
deviceTypestringThe type of device being registered
oneSignalIdstringThe OneSignal unique identifier

SimpleResponse

Generic interface representing a simple response from the API.

interface SimpleResponse<T> {
ok: boolean;
error?: ErrorResponse;
data: T | null;
}
PropertyTypeDescription
okbooleanIndicates if the operation was successful
errorErrorResponse (optional)Error details if the operation failed
dataT | nullThe response data (null in this case of OneSignal registration)

ErrorResponse

Interface representing an error response from the API.

interface ErrorResponse {
message: string;
details: {
faultCode: number;
fields: {
field: string;
error: string;
}[];
};
request: {
xRequestId: string;
method: string;
headers: Record<string, string>[];
};
timestamp: string;
}
PropertyTypeDescription
messagestringError message
details.faultCodenumberNumeric code identifying the error
details.fieldsArrayList of field-specific errors
details.fields[].fieldstringName of the field with an error
details.fields[].errorstringError message for the field
request.xRequestIdstringRequest identifier
request.methodstringHTTP method used for the request
request.headersArrayRequest headers
timestampstringTimestamp when the error occurred

WebPushSubscriptionRequestStatus

Enum representing the possible status values returned by the playerRegisterWebPushSubscription method.

enum WebPushSubscriptionRequestStatus {
SUCCESS = 'SUCCESS',
ALREADY_GRANTED = 'ALREADY_GRANTED',
MISSING_SERVICE_WORKER = 'MISSING_SERVICE_WORKER',
WEB_PUSH_NOT_SUPPORTED = 'WEB_PUSH_NOT_SUPPORTED',
WEB_PUSH_PERMISSION_DENIED = 'WEB_PUSH_PERMISSION_DENIED',
WEB_PUSH_PERMISSION_CANCELLED = 'WEB_PUSH_PERMISSION_CANCELLED',
ERROR = 'ERROR',
}
ValueDescription
SUCCESSThe player has successfully registered for web push notifications.
ALREADY_GRANTEDThe player has already granted permission for web push notifications.
MISSING_SERVICE_WORKERThe service worker required for web push notifications is missing.
WEB_PUSH_NOT_SUPPORTEDWeb push notifications are not supported in the current browser.
WEB_PUSH_PERMISSION_DENIEDThe player has denied permission for web push notifications.
WEB_PUSH_PERMISSION_CANCELLEDThe player cancelled the permission request for web push notifications.
ERRORAn error occurred during the web push subscription process.

OnsiteVariantEnum

Enum representing the different display variants for onsite notifications.

enum OnsiteVariantEnum {
DEFAULT = 'DEFAULT',
TITLE = 'TITLE',
IMAGE_TOP = 'IMAGE_TOP',
IMAGE_RIGHT = 'IMAGE_RIGHT',
IMAGE_LEFT = 'IMAGE_LEFT',
}
ValueDescription
DEFAULTDefault display variant for onsite notifications.
TITLETitle-only display variant for onsite notifications.
IMAGE_TOPDisplay variant with image at the top for onsite notifications.
IMAGE_RIGHTDisplay variant with image on the right for onsite notifications.
IMAGE_LEFTDisplay variant with image on the left for onsite notifications.

AnalyticsTrackingType

Enum representing the different types of analytics tracking.

enum AnalyticsTrackingType {
DELIVERED = 'DELIVERED',
OPENED = 'OPENED',
CLICKED = 'CLICKED',
ERROR = 'ERROR',
}
ValueDescription
DELIVEREDTracking for when a notification is delivered.
OPENEDTracking for when a notification is opened.
CLICKEDTracking for when a notification is clicked.
ERRORTracking for when an error occurs with a notification.

AnalyticsTrackingChannel

Enum representing the different channels for analytics tracking.

enum AnalyticsTrackingChannel {
ON_SITE = 'ON_SITE',
WEB_PUSH = 'WEB_PUSH',
}
ValueDescription
ON_SITEAnalytics tracking for onsite notifications.
WEB_PUSHAnalytics tracking for web push notifications.

OnSiteNotificationEventData

Interface for onsite notification event data.

interface OnSiteNotificationEventData extends BaseEventData {
msg: {
type: OnsiteVariantEnum;
contentHtml: string;
language: string;
};
playerId: string;
}
PropertyTypeRequiredDescription
msg{ type: OnsiteVariantEnum; contentHtml: string; language: string; }YesMessage content for the onsite notification.
msg.typeOnsiteVariantEnumYesDisplay variant for the onsite notification.
msg.contentHtmlstringYesHTML content for the onsite notification.
msg.languagestringYesLanguage of the onsite notification content.
playerIdstringYesID of the player receiving the notification.

AnalyticsTrackingEventEmitter

Interface for analytics tracking event emitters.

interface AnalyticsTrackingEventEmitter {
message: InternalRawEvent<'messageGateway:on:sendOnSiteNotification'>;
trackingType: AnalyticsTrackingType;
channel: AnalyticsTrackingChannel;
errorDetail?: {
code: string;
description?: string;
metadata: Record<string, unknown>;
};
}
PropertyTypeRequiredDescription
messageInternalRawEvent<'messageGateway:on:sendOnSiteNotification'>YesThe message event being tracked.
trackingTypeAnalyticsTrackingTypeYesThe type of tracking (DELIVERED, OPENED, CLICKED, ERROR).
channelAnalyticsTrackingChannelYesThe channel being tracked (ON_SITE, WEB_PUSH).
errorDetail{ code: string; description?: string; metadata: Record<string, unknown>; }NoDetails about any error that occurred.
errorDetail.codestringYesA unique error code to identify the problem (e.g., PUSH_001).
errorDetail.descriptionstringNoA human-readable description of the error to be displayed in the Analytics UI.
errorDetail.metadataRecord<string, unknown>YesGeneral error information in a more technical way for internal audit.