Skip to main content

Events and Error Handling

The WL App SDK communicates with the embedded Loyal Guru module through postMessage. The SDK validates messages from the configured module origin and exposes callbacks to the host app.

Event Flow​

Core Callbacks​

CallbackWhen it firesHost action
onLoadThe iframe loads.Hide loading state or mark module as ready.
onErrorSDK or iframe setup fails.Show fallback UI and log the integration error.
onServerErrorThe embedded module receives a backend error.Map to host error handling or support logging.
onSizeThe module sends a size update.Resize container manually if autoResize is disabled.
onDataThe module emits a module-specific data message.Update host state, analytics, or native UI elements.
onTokenRefreshRequestedThe module needs a fresh JWT.Fetch a new token and call refreshToken(newToken).

Message Types​

MessageDirectionDescription
AUTH_TOKENSDK -> moduleSends the current token and locale after load or refresh.
REQUEST_NEW_TOKENModule -> SDKRequests a fresh Customer JWT.
SIZEModule -> SDKProvides iframe height/width.
SERVER_ERRORSModule -> SDKProvides backend error details.
LG_DATALAYER_EVENTModule -> SDKSends analytics payloads to the host data layer when enabled.
DATA_TYPEModule -> SDKSends module-specific data, for example Rewards or game result information.
NAVIGATION_EXTERNALModule -> SDKThe Customer tapped an external URL that the host should open outside the embedded module.
COUPON_REDEEMEDModule -> SDKA coupon redemption completed inside the embedded experience.
POINTS_UPDATEDModule -> SDKCustomer points balance changed.
GAME_RESULTModule -> SDKCustomer completed a game play and received a win/lose result.
CHALLENGE_COMPLETEDModule -> SDKCustomer completed a Challenge.
SESSION_EXPIREDModule -> SDKSession expired and could not be refreshed.

Server Error Payload​

Example:

{
"type": "SERVER_ERRORS",
"payload": {
"status": 422,
"message": "Validation error",
"url": "https://api.loyalguru.com/...",
"errorCode": "VALIDATION_ERROR",
"errorBody": {},
"ts": 1731612345123
}
}

Recommended handling:

StatusRecommended host behavior
400Show validation fallback or log integration contract issue.
401Refresh token if possible; otherwise redirect to login.
403Show access-denied or unavailable module state.
404Show module/content not found fallback and verify module URL.
422Show user-facing validation copy when relevant, otherwise log for support.
500Show generic fallback and retry option; log error context.

Data Events​

Example:

{
"type": "DATA_TYPE",
"payload": {
"module": "Rewards",
"message": "{ Json with rewards info }",
"ts": 1731612345123
}
}

Use onData only for host-level reactions. The embedded Loyal Guru module remains the source of truth for loyalty UI and state.

Analytics Bridge​

When analytics is enabled, the embedded module can emit LG_DATALAYER_EVENT messages. The SDK can push them into window.dataLayer so the host can route events to GTM, GA4, Segment, Mixpanel, or another analytics tool.

Set up the host data layer before initializing the SDK:

window.dataLayer = window.dataLayer || [];

EmbedLoyaltyApp.init({
containerId: 'loyalty-container',
iframeOrigin: 'https://your-loyalty-module.loyal.guru',
token: 'CUSTOMER_JWT',
module: 'loyalty',
gtmEnabled: true,
});

If the retailer manages tracking entirely outside the embedded module, disable the analytics bridge where supported.

Troubleshooting​

SymptomWhat to check
Nothing renderscontainerId exists before init(), module origin is reachable, SDK script/package loaded.
CONTAINER_NOT_FOUNDThe host calls init() before the DOM node exists.
Iframe fails to loadWrong iframeOrigin, blocked network request, CSP restriction, or environment mismatch.
Token refresh loopRefresh endpoint returns an expired token or does not map to the same Customer.
401 from moduleInvalid token, expired token, unknown Customer, wrong Company slug, or unsupported signing key.
Auto-resize does not workautoResize disabled, parent container fixed height/overflow, or module not emitting SIZE.
Cross-origin blockedMissing allowed origin, strict CSP, or production origin validation mismatch.
Analytics not visiblegtmEnabled disabled, window.dataLayer missing, consent disabled, or host tag manager not listening.

Production Checklist​

AreaCheck
OriginsUse strict allowlists for host and iframe origins.
CSPAllow the SDK script and Loyal Guru module origin.
Token refreshImplement onTokenRefreshRequested before go-live.
Error loggingLog onServerError payloads with enough context for support.
Responsive layoutTest mobile, tablet, and desktop widths.
ConsentConfirm analytics and tracking behavior respects Customer consent.