Skip to content

Using the JavaScript library

The calls the web library offers, from capturing events and identifying users to super properties, groups, session IDs and consent.

Once the web snippet is on your page, the global myjourney object is ready to call. Calls made before the library has finished loading are queued and run once it is ready.

This page lists the calls you are most likely to need. For start-up options, see Configuring the library.

Capture events

js
myjourney.capture("report_exported", {
  format: "csv",
  rows: 1200,
});

The first argument is the event name; the second holds the event's properties. A third, optional argument takes capture options:

OptionWhat it does
$setPerson properties to set, overwriting earlier values.
$set_oncePerson properties to set only if the person does not have them yet.
timestampA Date to record instead of the current time.
uuidYour own UUID for the event. Values that are not valid UUIDs are ignored.
send_instantlySend the event straight away instead of with the next batch.
js
myjourney.capture(
  "plan_upgraded",
  { to_plan: "growth" },
  { $set: { plan: "growth" } },
);

More in Capturing events.

Identify users

js
myjourney.identify(
  "user_123",                       // your own ID for the user
  { email: "[email protected]" },    // set: overwrites earlier values
  { first_plan: "starter" },        // set once: kept if already set
);

Call identify when you know who the user is, such as after they sign in. Their earlier anonymous events are linked to the same person. See Identifying users for how and when to call it.

To read the ID the library is using now:

js
const id = myjourney.get_distinct_id();

Set person properties

To update the current person without identifying them again:

js
myjourney.setPersonProperties(
  { plan: "growth" },               // set
  { signed_up_with: "google" },     // set once
);

myjourney.unsetPersonProperties(["temporary_flag"]);

See Person properties.

Super properties

A super property is added to every event the browser sends from now on. It is stored in the visitor's browser, so it survives page loads.

js
myjourney.register({ site_section: "store" });         // set or overwrite
myjourney.register_once({ first_landing: "/pricing" }); // only if not already set
myjourney.register_for_session({ ab_variant: "b" });   // only for this session
myjourney.unregister("site_section");                  // remove one

Super properties describe events, not people. To store something about the person, use person properties.

Groups

If your customers are companies or accounts, you can tie events to a group as well as a person:

js
myjourney.group("company", "company_42", {
  name: "Acme Pty Ltd",
  employees: 120,
});

Events the browser sends after this call belong to that group. myjourney.resetGroups() clears the groups for the rest of the visit. Group analytics is available on plans that include Group analytics.

alias links another ID to the current user, for example an ID your back end uses for the same person:

js
myjourney.alias("backend_789");

Most sites only need identify. See Identifying users.

Sign out

js
myjourney.reset();

reset clears the stored ID, super properties and groups, and starts a new anonymous visitor, so the next person on a shared device is not linked to the last one. Call it when a user signs out.

Sessions

js
const sessionId = myjourney.get_session_id();
const replayLink = myjourney.get_session_replay_url({ withTimestamp: true });

Pass the session ID to your server if you want server-side events to join the same session; see Sessions.

To start or stop recording a session from your code:

js
myjourney.startSessionRecording();
myjourney.stopSessionRecording();

Feature flags

js
if (myjourney.isFeatureEnabled("new-checkout")) {
  // show the new checkout
}

const variant = myjourney.getFeatureFlag("pricing-test"); // a variant key, true, false or undefined
const payload = myjourney.getFeatureFlagPayload("pricing-test");

myjourney.onFeatureFlags(() => {
  // flags have loaded, or reloaded
});

Flags load when the library starts, and again when identify changes the user's ID. Call myjourney.reloadFeatureFlags() to fetch them again yourself.

Errors

js
try {
  submitOrder();
} catch (error) {
  myjourney.captureException(error, { step: "checkout" });
}
js
myjourney.opt_out_capturing();       // stop capturing in this browser
myjourney.opt_in_capturing();        // start again
myjourney.has_opted_out_capturing(); // true after an opt-out

See Let visitors opt out.

Debugging

js
myjourney.debug();        // log what the library does to the browser console
myjourney.debug(false);   // stop logging

You can also add ?__mj_debug=true to a page's address to turn on logging for that visit. Log lines start with [MyJourney].

Can't find what you need? Contact support