Overview
You can integrate the Web SDK for any of the supported frameworks (CDN, NPM, GTM, SPA, AMP, Flutter, and Shopify) using one of the following two methods:- Script-based Initialization (Recommended): Use the form-based interface to generate a validated code snippet or access module-specific configurations. For more information, refer to Script-based Initialization (Recommended).
- Manual Integration: Manually configure the SDK parameters and initialization code. For more information, refer to Option 2: Manual Integration.
You must have your Workspace ID (App ID) and Data Center. You can retrieve these details by navigating to
Settings > App > General on the Dashboard.Option 1: Script-based Initialization (Recommended)
To ensure a reliable and streamlined setup, we recommend using the Script-based initialization method. This method generates a validated JavaScript snippet, eliminating potential syntax errors and automatically including required flags based on the application type.- Workspace Details: Ensure you have your Workspace ID (App ID) and Data Center (found in
Settings > App > Generalon the Dashboard). - Service Worker (For Web Push): If enabling Web Push, create a file named
serviceworker.jsin your root directory. - Whitelisting (CSP): If your site uses a Content Security Policy, whitelist the necessary MoEngage URLs. For more information, refer here.
- Navigate to the Web SDK Initialization Website.
- Configure the values based on your application requirements. Refer to the Configuration Parameters table below for detailed descriptions of each field.
- Click Generate Code at the bottom of the form to create your validated JavaScript snippet.
- Implement the Generated Code: Copy the code snippets displayed by the tool and paste them into your project as follows:
- Update Service Worker (If Web Push Enabled): Add the generated
importScripts(...)line to the top of yourserviceworker.jsfile. - HTML File Changes (If Web Personalization Enabled): Add the generated
<link>tags and the Personalization<script>tag to the top of the<head>section of your root HTML file. - Initialize the SDK:
- For CDN: Paste the initialization script directly into the
<head>tag. - For NPM: Paste the
importstatement and theMoengage.initialize({...})block into your main JavaScript file.
- For CDN: Paste the initialization script directly into the
- Update Service Worker (If Web Push Enabled): Add the generated
Post-requisiteAfter integrating, verify the setup by opening your browser’s Developer Tools (Network tab). Reload the page and confirm that requests are successfully reaching MoEngage endpoints (e.g.,
cdn.moengage.com or sdk-0X.moengage.com).Configuration Parameters
The initialization method requires specific inputs to generate the correct code. The following table details the available fields in the configuration.Configuration Parameters
Configuration Parameters
Common Configuration Scenarios
You can configure environment selection and log verbosity independently to suit your development and debugging needs:Option 2: Manual Integration
-
MoEngage APIs have transitioned to camelCase. Initialization properties such as
app_idandproject_idare nowappIdandprojectId. Legacy properties will continue to work but will log a deprecation warning in your console. - The updated CDN snippets below include a new array u=[…] that registers both legacy (snake_case) and modern (camelCase) method names. This ensures backward compatibility by allowing either syntax to safely queue network calls before the SDK fully loads on the page.
-
Depending on your setup, follow these guidelines:
- New Integrations: Implement the SDK using camelCase methods. This ensures the integration is natively compatible with modern JavaScript standards and prevents console warnings.
- CDN/NPM: If the script tag pulls the latest version, the SDK automatically maps legacy calls to the new logic. However, refactoring to camelCase is recommended.
- Fixed Version: Critical: New camelCase methods are not available in legacy SDK versions. Do not refactor code until you update the SDK version in the source code to 2.71.00 and above.
- CDN
- NPM
- Google Tag Manager(GTM)
- Single Page App (SPA) Support
- Configure and Integrate AMP Web Push
- Flutter SDK
- Shopify App
- Specify the MoEngage Data Center for Web SDK data collection.
- Enable Whitelisting.
This is a required step for all web modules.
Method 1: MoEngage CDN
TEST Integration
Data Center
Following details of the different data centers you need to set based on the dashboard hosts
Keep the proper Data center handy as it will be used below for integration purpose.
MoEngage recommends that you perform the integration in the test environment first and then integrate it into the production or live environment.
To integrate into the test environment:
- Insert the following code in the
<head>tag of every page that requires tracking. - Replace “APP ID” available in the settings page of MoEngage Dashboard.
Navigate toDashboard --> Settings --> App --> Generaland copy the APP ID. - Replace the value of sdkVersion with the version of Web SDK you intend to use. It is recommended to use the format x (major)
- Replace “DC” with the data center form the above table.
- if you have Portfolio enabled for your workspace, you need to pass the
projectIdkey and the project ID of your project as value.
PRODUCTION or LIVE Integration
To integrate into the production or live environment:- Insert the following code in the
<head>tag of every page that requires tracking. - Replace “APP ID” available in the settings page of MoEngage Dashboard.
Navigate toDashboard --> Settings --> App --> Generaland copy the APP ID. - Replace the value of sdkVersion with the version of Web SDK you intend to use. It is recommended to use the format x (major)
- Replace “DC” with the data center form the above table
- If you have Portfolio enabled for your workspace, you need to pass the
projectIdkey and the projectID of you project as value.
Upgrading the SDK
When you reference the MoEngage Web SDK from our content delivery network, for example,https://cdn.moengage.com/release/{DC}/versions/{sdkVersion}/moe_webSdk.min.latest.js where sdkVersion = x (using sdkVersion = x, i.e. fixing version only till major release is our default integration recommendation), your users will receive new features and bug fixes automatically when they refresh your site.
However, when we release major changes, we require you to upgrade the MoEngage Web SDK manually to ensure that nothing in your integration will be impacted by breaking changes.
You can keep up-to-date with our latest release following our changelog for a full accounting of our Web SDK release history. To upgrade the MoEngage Web SDK:
- Update the MoEngage library version by changing value of
sdkVersionin the integration script. - If you have web push integrated, update version in the serviceworker cdn url in the service worker file on your site - by default, this is located at
/serviceworker.jsat your site’s root directory, but the location may be customized in some integrations.importScripts("https://cdn.moengage.com/release/{DC}/versions/[old-version-number]/serviceworker_cdn.min.latest.js"); - If you have web personalisation integrated, update the version in the web personalisation script-
https://cdn.moengage.com/release/{DC}/versions/[old-version-number]/moe_webSdk_webp.min.latest.js?appId={workspace-id}
Method 2: Using NPM
If your site uses NPM or Yarn package managers, you can add the Moengage NPM package as a dependency.import or require the library in the typical fashion:
- if you have Portfolio enabled for your workspace, you need to pass the
projectIdkey. - if you want to change the integration settings for specialized projects, then pass it as shown in the following snippet:
If you wish to always use the latest version of the Web SDK (instead of the version of @moengage/web-sdk mentioned in your package manager), pass the property
useLatest with value set to true in the configuration:moengage.initialize({appId: 'XXXXXXXXXXXXXXXX', useLatest: true});Use the version of Web SDK that you intend by altering the version of @moengage/web-sdk dependency in your package manager. More information on how to use specific version or range of versions can be found here.
Whitelisting URLs
Optional step for all web modules
Ensure that you whitelist the following endpoints for smooth integration. script-src: image-src:
- https://moe-email-campaigns.s3.amazonaws.com/
- https://image.moengage.com/
- https://image-eu.moengage.com/
- https://image-ap1.moengage.com/
- https://image-04.moengage.com/
- https://image-06.moengage.com/
- https://image.moengage.com/all-campaign-images-moe-dc-100/
- https://sdk-01.moengage.com/
- https://sdk-02.moengage.com/
- https://sdk-03.moengage.com/
- https://sdk-04.moengage.com/
- https://sdk-06.moengage.com/
Refused to connect to https://\*\*\*.moengage.com/\*\*\*\* because it violates the following Content Security Policy directive. if you do not whitelist the URLs.
For more information, refer to Content Security Policy.
Troubleshooting
Not able to debug SDK in the environment.
Check that thelogLevel is set to 1 (or 2 for verbose) while initializing the MoEngage SDK.
How to see SDK logs in Live environment?
Configure thelogLevel to 1 while initializing the MoEngage SDK.
What is the difference between TEST & LIVE environment?
MoEngage provides a staging environment referred to as the TEST environment. The LIVE environment contains data about your actual users/customers from your production website. MoEngage Web SDK uses theenv parameter to route data correctly:
env: 'TEST': Routes data to your Test dashboard.env: 'LIVE': Routes data to your production dashboard.
logLevel parameter regardless of the environment selected.