Skip to main content
All CollectionsGetting started
Managing identity in Analytics.js
Managing identity in Analytics.js
Updated over a month ago

This page explains how Analytics.js identifies users, and passes userID and anonymousID data, and how to override and change this information.

June ID Persistence

To ensure high fidelity, first-party customer data, June writes the user’s IDs to the user’s local storage, and uses that as the June ID on the cookie whenever possible. Local Storage is meant for storing this type of first-party customer information.

If a user returns to your site after the cookie expires, Analytics.js looks for an old ID in the user’s localStorage, and if one is found, sets it as the user’s ID again in the new cookie. If a user clears their cookies and localstorage, all of the IDs are removed, and the user gets a completely new anonymousID when they next visit the page.

Anonymous IDs

Analytics.js generates a universally unique ID (UUID) for the viewer during the library’s initialization phase, and sets this as anonymousId for each new visitor to your site. This happens before Analytics.js loads any device-mode destinations, and so before these destination-libraries can generate their own user IDs.

Retrieve the Anonymous ID

You can get the user’s current anonymousId using the following call:

analytics.user().anonymousId();

If the user’s anonymousId is null (meaning not set) when you call this function, Analytics.js automatically generated and sets a new anonymousId for the user.

If you are using the npm library, the previous call returns a promise for user(). As a workaround, you’ll need to grab the user’s current anonymousId in the following way:

analytics.instance?.user().anonymousId()

Refreshing the Anonymous ID

A user’s anonymousId changes when any of the following conditions are met.

  • The user clears their cookies and localstorage.

  • Your site or app calls analytics.reset() during in the user’s browser session.

  • Your site or app calls analytics.identify() with a userId that is different from the current userId.

  • Your site or app is setting ajs_user_id to an empty string or calling analytics.user().id('') before calling analytics.identify(). This sequence of events will result in a new anonymousId being set when analytics.identify() is called.

Override the Anonymous ID from the June snippet

You can also set the anonymousId immediately inside your June snippet, even before the ready method returns.

analytics.load('writekey'); analytics.page(); analytics.setAnonymousId('ABC-123-XYZ');

Use this method if you are queueing calls before ready returns and they require a custom anonymousId. Keep in mind that setting the anonymousId in Analytics.js does not overwrite the anonymous tracking IDs for any destinations you’re using.

Device-mode destinations that load their code on your site might also set their own anonymous ID for the user that is separate and different from the June generated one. Some destinations use the June anonymousId. Read the documentation for each Destination to find out if a Destination sets its own ID.

Override the default Anonymous ID with a call

If the default generated UUID does not meet your needs, you can override it anonymousId for the current user using either of the following methods.

analytics.user().anonymousId('ABC-123-XYZ');
analytics.setAnonymousId('ABC-123-XYZ')

These methods behave exactly the same.

Override anonymousId in an Identify call

analytics.identify('user_123', { name: 'Jane Kim' }, { anonymousId: 'ABC-123-XYZ' });

Override anonymousId on a Page call

analytics.page({}, { anonymousId: 'ABC-123-XYZ' });

Override anonymousId on a Track call

analytics.track('Email Clicked', { callToAction: 'Signup' }, { anonymousId: 'ABC-123-XYZ' });

Anonymizing IP

June automatically collects the user’s IP address for device-based (iOS, Android, Analytics.js and Xamarin) events.

At the moment, June doesn’t support automatically collecting IPv6 addresses.

You can manually set the IP by passing a value for options.context.ip to prevent the June systems from recording the IP address for the request, as in the example below.

analytics.track("Order Completed", {}, { context: { ip: "0.0.0.0" }});

You must add this override to every Track call to explicitly override IP collection. If you reset this trait in the context object, June defaults to the normal IP collection behavior.

Did this answer your question?