3.1.0-alpha.6ebbac7 • Published 3 years ago

@dfds-platform/tracking v3.1.0-alpha.6ebbac7

Weekly downloads
360
License
UNLICENSED
Repository
-
Last release
3 years ago

@dfds-platform/tracking

Installation

Install with npm:

npm install @dfds-platform/tracking

Install with yarn:

yarn add @dfds-platform/tracking

Setup Google Tag Manager

You have to setup Google Tag Manager (GTM) in your application yourself, tracking package does not set it up automatically. You can follow these instructions. these instructions

Alternatively, you can use an existing package to set up GTM. E.g. you can use this plugin in a Gatsby applicaiton. Below is the Google Tag Manager ID that you should use in both: development and production.

**GTM-WZLN3V4**

Publishing

New releases are created in github which will create a new tag. ADO will pick up on the new tag and publish a version with that tag using the ci:publish npm script

Tracking context provider setup

Wrap your top app component with TrackingProvider to provide tracking context in order to use various tracking hooks.

import { TrackingProvider } from '@dfds-platform/tracking'

return (
  <TrackingProvider>
    <YourApp />
  </TrackingProvider>
)

Now you can use pre-defined tracking hooks / events. The list of tracking events and their implementation guidelines can be found in "Tracking events" folder. Alternatively, you can dispatch custom javascript events and TrackingProvider will catch and track those events. Additional information can be found in "Custom event listener" folder.

Visit Storybook to see a full list of available tracking hooks.

Developing using yarn link

It can be handy to use developing functionality in the context of an existing app. yarn link can be used in that case.

Getting started

A common problem when using yarn link is that you end of with multiple versions of packages (eg. react). This is because dependencies are resolved upwards from within the symlinked react-components folder.

webpack

If you are using webpack you can try setting resolve.symlinks to false in your webpack.config.js to only resolve dependencies from the apps node_modules folder.

Gatsby

Gatsby uses webpack under the hood, so in order to set resolve.symlinks add the following to the gatsby-node.js file

exports.onCreateWebpackConfig = ({ getConfig, actions, stage }) => {
  const config = getConfig()
  config.resolve.symlinks = false
  actions.replaceWebpackConfig(config)
}

Setting up the link

In a terminal navigate to the packages/tracking folder

cd ./packages/tracking

Run build which will create a dist folder

yarn run build

Run watch to watch for changes and re-build

yarn run watch

Navigate into the dist folder and run yarn link

cd ./dist
yarn link

You are now ready to use the linked package from within you app while developing. Change to you app source folder (where the package.json file is located) and use the linked version of the @dfds-platform/tracking package

yarn link @dfds-platform/tracking

Tracking package accepts CustomEvents

You can dispatch custom javascript events and TrackingProvider will catch and track those events. This might be benefitial if you e.g. are building a microfrontend or a package that is beying implemented in a hosting application that already has tracking package and TrackingProvider installed. Then instead of setting tracking package as dependency for your microfrontend or package, you could rely on the tracking implementation in the hosting application and simply expose custom events that will get captured and tracked by a TrackingProvider in the hosting application. However, if you decide to use CustomEvents instead of the pre-defined tracking hooks, then it is your application and not the tracking package that is fully responsible for those tracking events and their payloads / types. So always make sure that tracking events and their payloads are aligned with the MarTech team.

Example of a CustomEvent dispatch

First, make sure that the hosting application is wrapper in a TrackingProvider (this is where the custom event listener lives) Then you may dispatch a custom event in you app as following:

// Example...
const TRACKING_EVENT_NAME = 'track-event'

window.dispatchEvent(
    new CustomEvent(TRACKING_EVENT_NAME, {
      detail: {
        event: 'formSubmit',
        eventOrigin: 'ukAPI',
        formName: 'ukApi_login',
        formId: 'ukApi_login',
        ..rest,
      },
    })
  )

The name of your CustomEvent must always be track-event event and eventOrigin must always be a part of the event payload. The rest of the event payload will vary depending on an event beying sent. event - the name of the event that is beying captured by Google Tag Manager eventOrigin - the name of the microfrontend or package or application where the event is being sent from. Ususally a hardcoded value representing your app, needs to be agreed with MarTech team.

Cypress tests to verify tracking

Cypress tests for tracking are not being a part of the tracking package. This page only shows examples of how Cypress tests for tracking verification could be implemented by you in your applicaiton.

Test if Google Tag Manager is set up in your application

The very first test you should consider writing for your tracking implementation is GTM verification test. If GTM is not set up correctly, tracking data will not get to the end points where it is being consumed. It is recommended to run this test as early as possible and before you start tracking any events.

Cypress.Commands.add('verifyGoogleTagManager', () => {
  cy.window().then((window) => {
    assert.isDefined(window.dataLayer, 'window.dataLayer is defined')
    assert.isDefined(
      window.dataLayer.find((x) => x.event === 'gtm.js'),
      'Google Tag Manager is loaded'
    )
  })
})

Test tracking events and their payloads

This custom command alows to find all the events with a specified name in window.dataLayer. It will then return a reversed array where the latest tracking event of a specified name will be at position 0.

Cypress.Commands.add('getTrackingEventByName', (name) => {
  return cy.window().then((win) => {
    const events = win.dataLayer.filter((obj) => obj.event == name)
    return events.reverse() // reversing the array will put the latest tracking event with the specified name at possition [0] in the reverset events array.
  })
})

To test if a tracking event was fired the correct number of times, you could do the following:

cy.getTrackingEventByName('userLogin').then(($e) => {
  assert.equal($e.length, 1, 'Correct number of "userLogin" events')
})

To test if a tracking event payload is correct, you could do the following:

cy.getTrackingEventByName('userLogin').then(($e) => {
  assert.equal($e.length, 1, 'Correct number of userLogin events')
  assert.equal($e[0].bookingEngine, 'tourOperator', 'Correct bookingEngine')
  assert.equal($e[0].businessArea, 'passenger', 'Correct businessArea')
  expect($e[0].correlationId).not.to.be.empty
  assert.equal($e[0].errorMessage, 'Authentication failed', 'Correct errorMessage')
  assert.equal($e[0].errorMessageId, 'E0000004', 'Correct errorMessageId')
  assert.equal($e[0].errorType, 'APIError', 'Correct errorType')
  assert.equal($e[0].locale, 'en', 'Correct locale')
  assert.equal($e[0].status, 'fail', 'Correct status')
})

// OR

Cypress.Commands.add('verifyEventUserRegister', (status) => {
  cy.getTrackingEventByName('userRegister').then(($e) => {
    assert.equal($e.length, 1, 'Correct number of userRegister events')
    assert.equal($e[0].bookingEngine, 'seabookFlow', 'Correct bookingEngine')
    assert.equal($e[0].businessArea, 'passenger', 'Correct businessArea')
    assert.equal($e[0].locale, 'en-GB', 'Correct locale')
    assert.equal($e[0].status, status, 'Correct status')
    if (status === 'success') {
      cy.getDfdsUserId().then((id) => {
        assert.equal($e[0].userID, id, 'Correct userID')
      })
    }
    if (status === 'fail') {
      expect($e[0].correlationId).not.to.be.empty
      assert.equal($e[0].errorMessage, 'User already exists', 'Correct errorMessage')
      assert.equal($e[0].errorMessageId, 'E0000001', 'Correct errorMessageId')
      assert.equal($e[0].errorType, 'APIError', 'Correct errorType')
    }
  })
})
10.0.0

3 years ago

11.0.0

3 years ago

12.0.0

3 years ago

12.0.2

3 years ago

13.0.0

3 years ago

9.0.0

3 years ago

9.1.0

3 years ago

14.0.0

3 years ago

5.4.0

3 years ago

7.0.0

3 years ago

7.2.3

3 years ago

7.2.2

3 years ago

7.2.0

3 years ago

8.0.0

3 years ago

4.0.0

3 years ago

5.0.1

3 years ago

5.2.0

3 years ago

6.0.1

3 years ago

6.0.0

3 years ago

5.3.0

3 years ago

7.1.0

3 years ago

3.1.0

3 years ago

3.3.0

3 years ago

3.2.0

3 years ago

3.0.4

3 years ago

3.0.3

3 years ago

3.4.1

3 years ago

3.0.2

3 years ago

3.0.1

3 years ago

3.0.0

3 years ago

2.1.2

3 years ago

2.1.1

3 years ago

2.1.0

3 years ago

2.0.5

4 years ago

2.0.4

4 years ago

2.0.7

4 years ago

2.0.6

4 years ago

2.0.8

4 years ago

2.0.3

4 years ago

1.5.9

4 years ago

1.5.8

4 years ago

2.0.2

4 years ago

2.0.1

4 years ago

2.0.0

4 years ago

1.5.5

4 years ago

1.5.4

4 years ago

1.5.3

4 years ago

1.5.2

4 years ago

1.5.1

4 years ago

1.5.0

4 years ago

1.4.2

4 years ago

1.5.7

4 years ago

1.5.6

4 years ago

1.4.1

4 years ago

1.4.0

4 years ago

1.3.9

4 years ago

1.3.7

4 years ago

1.3.6

4 years ago

1.3.5

4 years ago

1.3.4

4 years ago

1.3.3

4 years ago

1.3.2

4 years ago

1.3.1

4 years ago

1.3.0

4 years ago

1.2.3

4 years ago

1.2.2

4 years ago

1.2.1

4 years ago

1.2.0

4 years ago

1.1.9

4 years ago

1.1.8

4 years ago

1.1.7

4 years ago

1.1.6

4 years ago

1.1.5

4 years ago

1.1.4

4 years ago

1.1.3

4 years ago

1.1.2

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.9

5 years ago

1.0.8

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago

0.9.8

5 years ago

0.9.9

5 years ago

0.9.7

5 years ago

0.9.6

5 years ago

0.9.5

5 years ago

0.9.4

5 years ago

0.9.3

5 years ago

0.9.2

5 years ago

0.9.1

5 years ago

0.9.0

5 years ago

0.8.9

5 years ago

0.8.8

5 years ago

0.8.7

5 years ago

0.8.6

5 years ago

0.8.5

5 years ago

0.8.4

5 years ago

0.8.3

5 years ago

0.8.2

5 years ago

0.8.1

5 years ago

0.8.0

5 years ago

0.7.9

5 years ago

0.7.8

5 years ago

0.7.7

5 years ago

0.7.6

5 years ago

0.7.5

5 years ago

0.7.4

5 years ago

0.7.3

5 years ago

0.7.2

5 years ago

0.7.1

5 years ago

0.7.0

5 years ago

0.6.9

5 years ago

0.6.8

5 years ago

0.6.7

5 years ago

0.6.6

5 years ago

0.6.5

5 years ago

0.6.4

5 years ago

0.6.3

5 years ago

0.6.2

5 years ago

0.6.1

5 years ago

0.6.0

5 years ago

0.5.8

5 years ago

0.5.7

5 years ago

0.5.6

5 years ago

0.5.5

5 years ago

0.5.4

5 years ago

0.5.3

5 years ago

0.5.2

5 years ago

0.5.1

5 years ago

0.5.0

5 years ago

0.4.9

5 years ago

0.4.8

5 years ago

0.4.7

5 years ago

0.4.6

6 years ago

0.4.5

6 years ago

0.4.4

6 years ago

0.4.3

6 years ago

0.4.2

6 years ago

0.4.1

6 years ago

0.4.0

6 years ago

0.3.8

6 years ago

0.3.9

6 years ago

0.3.7

6 years ago

0.3.6

6 years ago

0.3.5

6 years ago

0.3.4

6 years ago

0.3.3

6 years ago

0.3.2

6 years ago

0.3.1

6 years ago

0.3.0

6 years ago

0.2.9

6 years ago

0.2.7

6 years ago

0.2.8

6 years ago

0.2.6

6 years ago

0.2.5

6 years ago

0.2.4

6 years ago

0.2.3

6 years ago

0.2.2

6 years ago

0.2.1

6 years ago