Frontend tracking with Segment and custom server
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.


This library provides a module to track all frontend usage on, it wraps Segment and sends the same data both to segment and our own endpoint. When defining new metrics, please revise the methodology at the data playbook.


To install the dependencies, execute:

npm install

Usage (local)

To build and run the library locally, you can run npm run dev, that will let you include the library from http://localhost:9999/auth0-metrics.js, you can also test the methods included in http://localhost:9999/

Usage (deploy)


Using our CDN is the preferred solution. Just add the following line in your project's HTML head and you are done.

<script src=""></script>

Then you have to call the constructor with the correct dev/prod variables

var metrics = new Auth0Metrics('segmentKey', 'dwhEndpoint', 'website', {});

Options {}

Additional configuration options can be specified by passing an options object to the constructor. Metrics library will iterate over the object and add each property to the internal configuration options.

Available options:

removeQueryParam []{} (available in v1.6.0)

Allows stripping of query parameters from the window URL. This is useful in cases where URL contains JWT tokens or other information that should not be recorded. Each query parameter that needs to be removed can be specified with an object that contains a key (string) and a value (RegEx string) property.

  removeQueryParam: [
      key: 'token',
      value: '[a-z0-9\._~-]+'

The following url


will be transformed to



A script that will asynchronously load auth0-metrics.js is also provided.

<script src="auth0-metrics-loader.js"></script>
    segmentKey: 'segmentKey',
    dwhEndpoint: 'dwhEndpoint',
    label: 'website',
    options: {}

Once loaded, an instance of Auth0Metrics will be available on metricsLib. Calls to public instance methods of Auth0Metrics on metricsLib will be silently ignored until then.

// tracks the page if auth0-metrics.js has been loaded, otherwise the call is
// ignored and doesn't produce an error.;

If auth0-metrics.js has already been loaded, the script will not attempt load it again, and will simply create an instance of Auth0Metrics with the given configuration.

Using the loader may also be desired when loading auth0-metrics.js synchronously. If there's an exception during the instantiation of Auth0Metrics, the stubs methods will still be in place and calls like the one above to page won't throw.

<script src="auth0-metrics.js"></script>
<script src="auth0-metrics-loader.js"></script>
    segmentKey: 'segmentKey',
    dwhEndpoint: 'dwhEndpoint',
    label: 'website',
    options: {}

Contrast the previous snippet with the following that instantiates Auth0Metrics directly.

<script src="auth0-metrics.js"></script>
  var metricsLib = new Auth0Metrics('segmentKey', 'dwhEndpoint', 'website', {});

If the instantiation doesn't succeed, trying to call a method on metricsLib will throw because it is undefined and may break the behavior of your site.



Sets the userId of the currently connected client


  • uid user id to set


Sends information of the current page to track it


  • callback a function to call after sending this event

.track(event, data, callback)

Sends information of a custom event to track.


  • event a string with the name of the event to track
  • data an object with any data we need to pass
  • callback a function to call after sending this event

.identify(id, traits, callback)

Sends information of an identification (login/signup) to track.

NOTE: This method's signature does not fully pair with Segment's analytics.js. You can proxy directly to Segment by doing metricsLib.segment().identify() instead.


  • id user id to identify the current user to
  • traits additional properties to set to the user
  • callback a function to call after sending this event

You can omit the user id if you want to associate traits with the currently identified user, anonymous or not. You can also omit traits if all you want to do is associate an id. Finally, callback is optional and can always be omitted.

.alias(id, callback)

Sends an alias (renaming a previous id to a new one).


  • id new user id to map to, the previous id will be taken from the cookie.
  • callback a function to call after sending this event


Traits (additional properties) of the current user


Executes a callback when segment finishes loading.


  • cb function to run when segment finishes loading.

Issue Reporting

If you have found a bug or if you have a feature request, please report them at this repository issues section. Please do not report security vulnerabilities on the public GitHub issue tracker. The Responsible Disclosure Program details the procedure for disclosing security issues.




This project is licensed under the MIT license. See the LICENSE file for more info.