Integrating the Braze Web SDK
This reference article covers how to install the Braze Web SDK. The Braze Web SDK lets you collect analytics and display rich in-app messages, push, and Content Card messages to your web users. For more information, see Braze JavaScript reference documentation. To learn more about the SDK in general, see Getting started: Integration overview.
This guide uses code samples from the Braze Web SDK 4.0.0+. To upgrade to the latest Web SDK version, see SDK Upgrade Guide.
Integrating the SDK
Not sure if the standard integration method is right for you? Check out our other methods before continuing.
Step 1: Install the Braze library
You can install the Braze library using one of the following methods. If your website uses a Content-Security-Policy
, refer to our Content security policy headers guide before installing the library.
While most ad blockers will not block the Braze Web SDK, some more restrictive ad blockers are known to cause issues.
If your site uses NPM or Yarn package managers, you can add the Braze NPM package as a dependency.
Typescript definitions are now included as of v3.0.0. For notes on upgrading from 2.x to 3.x, see our changelog.
1
2
3
npm install --save @braze/web-sdk
# or, using yarn:
# yarn add @braze/web-sdk
Once installed, you can import
or require
the library in the typical fashion:
1
2
3
import * as braze from "@braze/web-sdk";
// or, using `require`
const braze = require("@braze/web-sdk");
The Braze Web SDK can be installed from the Google Tag Manager Template Library. Two tags are supported:
- Initialization tag: loads the Web SDK onto your website and optionally sets the External User ID.
- Actions tag: used to trigger custom events, purchases, change user IDs, or toggle SDK tracking.
Visit the Google Tag Manager integration guide for more information.
Add the Braze Web SDK directly to your HTML by referencing our CDN-hosted script, which loads the library asynchronously.
Step 2: Initialize the SDK (optional)
If you’ve configured your Braze initialization options in a Tag Manager, you can skip this step.
Otherwise, after the Braze Web SDK is added to your website, initialize the library with the API key and SDK endpoint URL found in Settings > App Settings within your Braze dashboard. For a complete list of options for braze.initialize()
, along with our other JavaScript methods, see Braze JavaScript documentation.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// initialize the SDK
braze.initialize('YOUR-API-KEY-HERE', {
baseUrl: "YOUR-SDK-ENDPOINT-HERE"
});
// optionally show all in-app messages without custom handling
braze.automaticallyShowInAppMessages();
// if you use Content Cards
braze.subscribeToContentCardsUpdates(function(cards){
// cards have been updated
});
// optionally set the current user's external ID before starting a new session
// you can also call `changeUser` later in the session after the user logs in
if (isLoggedIn){
braze.changeUser(userIdentifier);
}
// `openSession` should be called last - after `changeUser` and `automaticallyShowInAppMessages`
braze.openSession();
Anonymous users on mobile or web devices may be counted towards your MAU. As a result, you may want to conditionally load or initialize the SDK to exclude these users from your MAU count.
Optional configurations
Logging
To quickly enable logging, you can add ?brazeLogging=true
as a parameter to your website URL. Alternatively, you can enable basic or custom logging.
Basic logging
Use enableLogging
to log basic debugging messages to the javascript console before the SDK is initialized.
1
enableLogging: true
Your method should be similar to the following:
1
2
3
4
5
braze.initialize('API-KEY', {
baseUrl: 'API-ENDPOINT',
enableLogging: true
});
braze.openSession();
Use braze.toggleLogging()
to log basic debugging messages to the javascript console after the SDK is initialized. Your method should be similar to the following:
1
2
3
4
5
6
braze.initialize('API-KEY', {
baseUrl: 'API-ENDPOINT',
});
braze.openSession();
...
braze.toggleLogging();
Basic logs are visible to all users, so consider disabling, or switch to setLogger
, before releasing your code to production.
Custom logging
Use setLogger
to log custom debugging messages to the javascript console. Unlike basic logs, these logs are not visible to users.
1
setLogger(loggerFunction: (message: STRING) => void): void
Replace STRING
with your message as a single string parameter. Your method should be similar to the following:
1
2
3
4
5
braze.initialize('API-KEY');
braze.setLogger(function(message) {
console.log("Braze Custom Logger: " + message);
});
braze.openSession();
Upgrading the SDK
This guide uses code samples from the Braze Web SDK 4.0.0+. To upgrade to the latest Web SDK version, see SDK Upgrade Guide.
When you reference the Braze Web SDK from our content delivery network, for example, https://js.appboycdn.com/web-sdk/a.a/braze.min.js
(as recommended by our default integration instructions), your users will receive minor updates (bug fixes and backward compatible features, versions a.a.a
through a.a.z
in the above examples) automatically when they refresh your site.
However, when we release major changes, we require you to upgrade the Braze Web SDK manually to ensure that nothing in your integration will be impacted by any breaking changes. Additionally, if you download our SDK and host it yourself, you won’t receive any version updates automatically and should upgrade manually to receive the latest features and bug fixes.
You can keep up-to-date with our latest release following our release feed with the RSS Reader or service of your choice, and see our changelog for a full accounting of our Web SDK release history. To upgrade the Braze Web SDK:
- Update the Braze library version by changing the version number of
https://js.appboycdn.com/web-sdk/[OLD VERSION NUMBER]/braze.min.js
, or in your package manager’s dependencies. - If you have web push integrated, update the service worker file on your site - by default, this is located at
/service-worker.js
at your site’s root directory, but the location may be customized in some integrations. You must access the root directory to host a service worker file.
These two files must be updated in coordination with each other for proper functionality.
Other integration methods
Accelerated Mobile Pages (AMP)
See more
Step 1: Include AMP web push script
Add the following async script tag to your head:
1
<script async custom-element="amp-web-push" src="https://cdn.ampproject.org/v0/amp-web-push-0.1.js"></script>
Step 2: Add subscription widgets
Add a widget to the body of your HTML that allows users to subscribe and unsubscribe from push.
1
2
3
4
5
6
7
8
9
<!-- A subscription widget -->
<amp-web-push-widget visibility="unsubscribed" layout="fixed" width="250" height="80">
<button on="tap:amp-web-push.subscribe">Subscribe to Notifications</button>
</amp-web-push-widget>
<!-- An unsubscription widget -->
<amp-web-push-widget visibility="subscribed" layout="fixed" width="250" height="80">
<button on="tap:amp-web-push.unsubscribe">Unsubscribe from Notifications</button>
</amp-web-push-widget>
Step 3: Add helper-iframe
and permission-dialog
The AMP Web Push component creates a popup to handle push subscriptions, so you’ll need to add the following helper files to your project to enable this feature:
Step 4: Create a service worker file
Create a service-worker.js
file in the root directory of your website and add the following snippet:
Step 5: Configure the AMP web push HTML element
Add the following amp-web-push
HTML element to your HTML body. Keep in mind, you need to append your apiKey
and baseUrl
as query parameters to service-worker-URL
.
1
2
3
4
5
6
7
<amp-web-push
layout="nodisplay"
id="amp-web-push"
helper-iframe-url="FILE_PATH_TO_YOUR_HELPER_IFRAME"
permission-dialog-url="FILE_PATH_TO_YOUR_PERMISSION_DIALOG"
service-worker-url="FILE_PATH_TO_YOUR_SERVICE_WORKER?apiKey={YOUR_API_KEY}&baseUrl={YOUR_BASE_URL}"
>
AMD: Disable support
See more
If your site uses RequireJS or another AMD module-loader, but you prefer to load the Braze Web SDK through one of the other options in this list, you can load a version of the library that does not include AMD support. This version of the library can be loaded from the following CDN location:
AMD: Module loader
See more
If you use RequireJS or other AMD module-loaders we recommend self-hosting a copy of our library and referencing it as you would with other resources:
1
2
3
4
5
require(['path/to/braze.min.js'], function(braze) {
braze.initialize('YOUR-API-KEY-HERE', { baseUrl: 'YOUR-SDK-ENDPOINT' });
braze.automaticallyShowInAppMessages();
braze.openSession();
});
Electron
See more
Electron does not officially support web push notifications (see: this GitHub issue). There are other open source workarounds you may try that have not been tested by Braze.
Jest framework
See more
When using Jest, you may see an error similar to SyntaxError: Unexpected token 'export'
. To fix this, adjust your configuration in package.json
to ignore the Braze SDK:
1
2
3
4
5
"jest": {
"transformIgnorePatterns": [
"/node_modules/(?!@braze)"
]
}
SSR frameworks
See more
If you use a Server-Side Rendering (SSR) framework such as Next.js, you may encounter errors because the SDK is meant to be run in a browser environment. You can resolve these issues by dynamically importing the SDK.
You can retain the benefits of tree-shaking when doing so by exporting the parts of the SDK that you need in a separate file and then dynamically importing that file into your component.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// MyComponent/braze-exports.js
// export the parts of the SDK you need here
export { initialize, openSession } from "@braze/web-sdk";
// MyComponent/MyComponent.js
// import the functions you need from the braze exports file
useEffect(() => {
import("./braze-exports.js").then(({ initialize, openSession }) => {
initialize("YOUR-API-KEY-HERE", {
baseUrl: "YOUR-SDK-ENDPOINT",
enableLogging: true,
});
openSession();
});
}, []);
Alternatively, if you’re using webpack to bundle your app, you can take advantage of its magic comments to dynamically import only the parts of the SDK that you need.
1
2
3
4
5
6
7
8
9
10
11
12
13
// MyComponent.js
useEffect(() => {
import(
/* webpackExports: ["initialize", "openSession"] */
"@braze/web-sdk"
).then(({ initialize, openSession }) => {
initialize("YOUR-API-KEY-HERE", {
baseUrl: "YOUR-SDK-ENDPOINT",
enableLogging: true,
});
openSession();
});
}, []);
Tealium iQ
See more
Tealium iQ offers a basic turnkey Braze integration. To configure the integration, search for Braze in the Tealium Tag Management interface, and provide the Web SDK API key from your dashboard.
For more details or in-depth Tealium configuration support, check out our integration documentation or reach out to your Tealium account manager.
Vite
See more
If you use Vite and see a warning around circular dependencies or Uncaught TypeError: Class extends value undefined is not a constructor or null
, you may need to exclude the Braze SDK from its dependency discovery:
1
2
3
optimizeDeps: {
exclude: ['@braze/web-sdk']
},
Other tag managers
See more
Braze may also be compatible with other tag management solutions by following our integration instructions within a custom HTML tag. Reach out to a Braze representative if you need help evaluating these solutions.