@comparaonline/event-tracker

This library it's an interface between any frontend application and some event tracking platforms, currently Snowplow and Tag Manager.

Usage no npm install needed!

<script type="module">
  import comparaonlineEventTracker from 'https://cdn.skypack.dev/@comparaonline/event-tracker';
</script>

README

@comparaonline/event-tracker

npm code style: prettier

This library is an interface between any frontend application and an event tracking platforms.

There's more documentation available for the library in compara's notion

Installation

To install the library in your application use

yarn add @comparaonline/event-tracker

or

npm install @comparaonline/event-tracker

Usage example

After installing the package you can send an event using the function sendEvent. For example, to send an event to snowplow (assuming it was already initialized), you could do it with the following code:

import { endpoints, sendEvent, types } from '@comparaonline/event-tracker/';
import {
  carInsuranceGenerators,
  FormSubmitParams,
} from '@comparaonline/event-tracker/eventGenerators/carInsurance';


const rawFormSubmit: FormSubmitParams = {
  appVersion: '1.0',
  appName: 'test-module',
  countryCode: types.CountryCodeTypes.chile,
  formId: 'asdf',
  formType: types.FormTypes.wizard,
  formSubmitStatus: types.FormStatusTypes.successful,
  funnelType: types.FormRelatedTypes.FunnelTypes.quote,
  formValues: { a: 'asdf', b: 'sdf' },
};
const formSubmit = carInsuranceGenerators.generateFormSubmit(rawFormSubmit);
sendEvent(endpoints.SNOWPLOW_APP, formSubmit);

Or alternatively:

To do the same for tag manager you would need to change the last line of the code to:

sendEvent(endpoints.TAG_MANAGER_APP, eventData);

Endpoints

The endpoints supported by the library are in the object endpoints of the library. The ones currently supported are:

  • SNOWPLOW_APP: the library can initialize snowplow with the functions initializeSnowplow and initializeTracker located in snowplowInitializer. initializeTracker needs an app id as input. The ones supported by the library are in snowplowAppIds and are separated by environment (development or production) and country code (cl,br, co, ar). An example of this would be:
import {
  snowplowInitializer,
  snowplowAppIds
} from "@comparaonline/event-tracker";

initializeSnowplow = (environment, countryCode) => {
  snowplowInitializer.initializeSnowplow();
  snowplowInitializer.initializeTracker(
    snowplowAppIds[countryCode],
    environment
  );
};
  • TAG_MANAGER_APP: currently the library doesn't come with a way to initialize tag manager and it assumes that it exists in the browser.

Migrating from 5.x.x to 6.x.x

  1. Imports: the imports have changed, instead of importing the interfaces of events you need to import eventGenerators, which are functions. These functions are grouped by business unit: carInsurance, travelAssistance, soapBrokerage and leadGen. There are also common eventGenerators which are for any business unit and should be used as preliminary measurements.

    import {
      carInsuranceGenerators,
    } from '@comparaonline/event-tracker/eventGenerators/carInsurance';
    
  2. Types: most events have types for some fields. These can be imported from the main module

    import { types } from '@comparaonline/event-tracker/';
    
    const rawFormSubmit = {
      appVersion: '1.0',
      appName: 'test-module',
      countryCode: types.CountryCodeTypes.chile,
      formId: 'asdf',
      formType: types.FormTypes.wizard,
      formSubmitStatus: types.FormStatusTypes.successful,
      funnelType: types.FormRelatedTypes.FunnelTypes.quote,
      formValues: { a: 'asdf', b: 'sdf' },
    };
    
  3. Generate event: previously the library only exported interfaces which where the direct input for sendEvent, now you need to use the previously mentioned event generators before sending the event. The generators receive objects that match the interfaces with the suffix Params (e.g. generateFormSubmit receives objects with the structure of FormSubmitParams)

    const rawFormSubmit: FormSubmitParams = {
      appVersion: '1.0',
      appName: 'test-module',
      countryCode: types.CountryCodeTypes.chile,
      formId: 'asdf',
      formType: types.FormTypes.wizard,
      formSubmitStatus: types.FormStatusTypes.successful,
      funnelType: types.FormRelatedTypes.FunnelTypes.quote,
      formValues: { a: 'asdf', b: 'sdf' },
    };
    const formSubmit = carInsuranceGenerators.generateFormSubmit(rawFormSubmit);
    sendEvent(endpoints.SNOWPLOW_APP, formSubmit);
    

Publishing a new version

Important: The content to be uploaded is the build from the dist directory. You shouldn't try to publish the root directory.

We use np as part of the publishing process. The recommended way to publish a new version is:

$ npm run np -- [version]

Example

$ npm run np -- 4.0.2

This will test, build, update the package version and commit it. Then, the content of the dist directory will be published to the npm registry.

Publishing package in CI pipeline

The package will be published automatically to npm when the CI pipeline is triggered by the master branch. You need to update the package version in package.json otherwise you will get the following error:

npm ERR! 403 403 Forbidden - PUT https://registry.npmjs.org/@comparaonline%2fevent-tracker - You cannot publish over the previously published versions