Skip to content

JavaScript web library

Add the library to your site, identify users, capture custom events and let visitors opt out.

The JavaScript library runs in your visitors' browsers. It captures pageviews and interactions automatically, and gives you a small set of calls for the things only your code knows: who the user is, and which actions matter to your business.

This page covers the essentials. For every call, see Using the library; for start-up options such as autocapture, replay and privacy settings, see Configuring the library.

Installing the library

Copy the web snippet from Settings → Project → General (under SDK setup, choose Web) and paste it into the <head> of every page. It is already filled in with your project token and your region's address. See Install the web snippet for the full steps.

The snippet is the way to install the library on every kind of site, including single-page apps built with React, Vue, Next.js and similar frameworks: it records a pageview each time your app's router changes the page. If your site uses Google Tag Manager, you can load it from there instead; see Install with Google Tag Manager.

Calling the library

The snippet defines a global library object, myjourney, as soon as it runs. Calls made before the library has finished loading are queued and carried out once it is ready, so your code can call myjourney anywhere after the snippet.

Identify your users

Until you identify a visitor, their events are tied to an anonymous ID kept in their browser. When someone signs in or signs up, tell the library who they are:

js
myjourney.identify("user_123", {
  email: "[email protected]",
  plan: "growth",
});
  • Use a stable ID from your own system, such as your database's user ID. Avoid email addresses as the ID, because they can change.
  • Events from before the user signed in are linked to the same person.
  • Pass only the properties you need. Personal information you send becomes data you are responsible for under privacy law.

When the user signs out, reset the library, so the next person on the same device is not linked to them:

js
myjourney.reset();

More on how identifying works: Identifying users.

Capture custom events

Pageviews, clicks and form submissions are captured automatically. For the actions that matter most, capture an event of your own:

js
myjourney.capture("plan_upgraded", {
  from_plan: "starter",
  to_plan: "growth",
  billing_period: "annual",
});
  • Name events with a verb in the past tense, such as signup_completed or report_exported, and keep one naming style across your team.
  • Add properties that you will want to filter or break down by.
  • Custom events appear in Activity and in every insight, funnel and retention chart.

More in Capturing events.

Let visitors opt out

If your consent banner or privacy settings let visitors refuse analytics, stop capturing for that browser:

js
myjourney.opt_out_capturing();

To start again after the visitor gives consent:

js
myjourney.opt_in_capturing();

To check the current choice, once the library has loaded:

js
if (myjourney.has_opted_out_capturing()) {
  // Show "Analytics: off" in your privacy settings.
}

The choice is stored in the visitor's browser and respected on later visits. Calling myjourney.reset() clears it, along with the visitor's ID.

If you need consent before anything is captured, add opt_out_capturing_by_default: true to the snippet's start-up options, and call myjourney.opt_in_capturing() when the visitor accepts. Nothing is captured until then.

Persistence

The library remembers the visitor's ID, session and super properties between page loads. By default it keeps them in the browser's local storage and in a first-party cookie named mj_<project token>_mja. To store them elsewhere, or not at all, set the persistence option; see Storage and cookies.

Next steps

Can't find what you need? Contact support