Skip to main content

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.
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 > General on the Dashboard).
  • Service Worker (For Web Push): If enabling Web Push, create a file named serviceworker.js in your root directory.
  • Whitelisting (CSP): If your site uses a Content Security Policy, whitelist the necessary MoEngage URLs. For more information, refer here.
Follow these steps to generate your initialization script:
  1. Navigate to the Web SDK Initialization Website.
  2. Configure the values based on your application requirements. Refer to the Configuration Parameters table below for detailed descriptions of each field.
  3. Click Generate Code at the bottom of the form to create your validated JavaScript snippet.
  4. 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 your serviceworker.js file.
    • 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 import statement and the Moengage.initialize({...}) block into your main JavaScript file.
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.

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_id and project_id are now appId and projectId. 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.
For more information, refer here.
You can manually integrate the SDK by configuring the initialization parameters directly in your codebase. Select your platform or framework from the list below to view the specific manual integration guide: For any manual integration, ensure you complete the following configuration steps:
  1. Specify the MoEngage Data Center for Web SDK data collection.
  2. 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:
  1. Insert the following code in the <head> tag of every page that requires tracking.
  2. Replace “APP ID” available in the settings page of MoEngage Dashboard.
    Navigate to Dashboard --> Settings --> App --> General and copy the APP ID.
  3. Replace the value of sdkVersion with the version of Web SDK you intend to use. It is recommended to use the format x (major)
  4. Replace “DC” with the data center form the above table.
  5. if you have Portfolio enabled for your workspace, you need to pass the projectId key and the project ID of your project as value.

PRODUCTION or LIVE Integration

To integrate into the production or live environment:
  1. Insert the following code in the <head> tag of every page that requires tracking.
  2. Replace “APP ID” available in the settings page of MoEngage Dashboard.
    Navigate to Dashboard --> Settings --> App --> General and copy the APP ID.
  3. Replace the value of sdkVersion with the version of Web SDK you intend to use. It is recommended to use the format x (major)
  4. Replace “DC” with the data center form the above table
  5. If you have Portfolio enabled for your workspace, you need to pass the projectId key 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 sdkVersion in 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.js at 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}
These 3 integrations must be updated with the same version number for proper functionality. In case you are using NPM, use the version number of @moengage/web-sdk dependency that is in your package.json file.

Method 2: Using NPM

If your site uses NPM or Yarn package managers, you can add the Moengage NPM package  as a dependency.
Once installed, you can import or require the library in the typical fashion:
Then, initialize it by passing the configurations in all the pages before using it:
You can pass any configuration here. For example,
  • if you have Portfolio enabled for your workspace, you need to pass the projectId key.
  • if you want to change the integration settings for specialized projects, then pass it as shown in the following snippet:
After that, you can use any method of the Moengage SDK, as shown in the snippet below:
 
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
If your website is configured to block the external endpoints, then all the network calls to MoEngage fails.
Ensure that you whitelist the following endpoints for smooth integration.
script-src: image-src: connect-src: frame-src: style-src: font-src: You will get the error 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 the logLevel is set to 1 (or 2 for verbose) while initializing the MoEngage SDK.

How to see SDK logs in Live environment?

Configure the logLevel 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 the env parameter to route data correctly:
  • env: 'TEST': Routes data to your Test dashboard.
  • env: 'LIVE': Routes data to your production dashboard.
You can independently control log verbosity using the logLevel parameter regardless of the environment selected.