Skip to content

Commit

Permalink
GTM activiated by user (#1979)
Browse files Browse the repository at this point in the history
* GTM activates after user accepts in banner

https://eaflood.atlassian.net/browse/IWTF-4081

Update banner to activate or deactiviate GTM instead of Hapi Gapi

* change to ga and forced to remove some of hapi gapi

* add page data container id back, change logging

* test setup rename

* refactor follwing user testing

* remove log
  • Loading branch information
ScottDormand96 committed Jun 19, 2024
1 parent b3e7b5b commit 8b07cee
Show file tree
Hide file tree
Showing 12 changed files with 114 additions and 310 deletions.
82 changes: 0 additions & 82 deletions packages/gafl-webapp-service/src/__tests__/plugin.spec.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
import { getPlugins } from '../plugins'
import { ANALYTICS } from '../constants.js'
import { trackAnalyticsAccepted, getAnalyticsSessionId, pageOmitted } from '../handlers/analytics-handler.js'
import db from 'debug'

jest.mock('../constants', () => ({
ANALYTICS: {
acceptTracking: 'you-may-watch-me',
seenMessage: 'seen-it',
pageAnalyticsOmitted: 'ignored-page'
},
CommonResults: {
OK: 'ok'
},
Expand All @@ -17,20 +9,13 @@ jest.mock('../constants', () => ({
}
}))

jest.mock('../handlers/analytics-handler.js', () => ({
pageOmitted: jest.fn(() => false),
trackAnalyticsAccepted: jest.fn(() => true),
getAnalyticsSessionId: jest.fn(() => '123')
}))
jest.mock('../server.js', () => ({
getCsrfTokenCookieName: jest.fn()
}))
jest.mock('debug', () => jest.fn(() => jest.fn()))
const { value: debug } = db.mock.results[db.mock.calls.findIndex(c => c[0] === 'webapp:plugin')]

describe('plugins', () => {
const findPlugin = (pluginArray, pluginName) => pluginArray.find(plugin => plugin?.plugin?.plugin?.name === pluginName)
const findPlugins = (pluginArray, pluginName) => pluginArray.find(plugin => plugin?.plugin?.plugin?.pkg?.name === pluginName)

describe('initialiseHapiI18nPlugin', () => {
it.each([
Expand All @@ -50,71 +35,4 @@ describe('plugins', () => {
}
)
})

describe('trackAnalytics', () => {
const generateRequestMock = (analytics = {}) => ({
cache: jest.fn(() => ({
hasSession: () => true,
helpers: {
analytics: {
get: jest.fn(() => analytics)
}
}
}))
})

beforeEach(jest.clearAllMocks)

const pluginArray = getPlugins()
const hapiGapiPlugin = findPlugins(pluginArray, '@defra/hapi-gapi')

it.each([
[true, true],
[false, false]
])('when acceptedTracking property equals %s, trackAnalytics should return %s', async (tracking, expectedResult) => {
const analytics = {
[ANALYTICS.acceptTracking]: tracking
}
trackAnalyticsAccepted.mockReturnValueOnce(tracking)

const result = await hapiGapiPlugin.options.trackAnalytics(generateRequestMock(analytics))

expect(result).toBe(expectedResult)
})

it.each([
[true, 'session_id_example', 'Session is being tracked for: session_id_example', false],
[false, 'testing_session_id', 'Session is not being tracked for: testing_session_id', false],
[true, 'example_session_id', 'Session is being tracked for: example_session_id', false],
[false, 'test_session_id', 'Session is not tracking current page for: test_session_id', true]
])(
'when tracking is %s it logs an ID of %s and %s and sets ENABLE_ANALYTICS_OPT_IN_DEBUGGING to true when pageOmitted returns %s',
async (tracking, id, expectedResult, skip) => {
const analytics = {
[ANALYTICS.acceptTracking]: tracking,
[ANALYTICS.omitPageFromAnalytics]: skip
}
process.env.ENABLE_ANALYTICS_OPT_IN_DEBUGGING = true

trackAnalyticsAccepted.mockReturnValueOnce(tracking)
getAnalyticsSessionId.mockReturnValueOnce(id)
pageOmitted.mockReturnValueOnce(skip)

await hapiGapiPlugin.options.trackAnalytics(generateRequestMock(analytics))

expect(debug).toHaveBeenCalledWith(expectedResult)
}
)

it('debug isnt called if ENABLE_ANALYTICS_OPT_IN_DEBUGGING is set to false', async () => {
const analytics = {
[ANALYTICS.acceptTracking]: true
}
process.env.ENABLE_ANALYTICS_OPT_IN_DEBUGGING = false

await hapiGapiPlugin.options.trackAnalytics(generateRequestMock(analytics))

expect(debug).toBeCalledTimes(0)
})
})
})

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ exports[`The page handler function sets the value of pageData with displayAnalyt
"altLang": Array [],
"analyticsMessageDisplayed": false,
"analyticsSelected": false,
"approvedGTM": false,
"backRef": null,
"displayAnalytics": false,
"gtmContainerId": false,
Expand Down Expand Up @@ -40,6 +41,7 @@ exports[`The page handler function sets the value of pageData with displayAnalyt
"altLang": Array [],
"analyticsMessageDisplayed": false,
"analyticsSelected": false,
"approvedGTM": false,
"backRef": null,
"displayAnalytics": true,
"gtmContainerId": false,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { ANALYTICS } from '../../constants.js'
import analyticsHandler from '../analytics-handler.js'
import analyticsHandler, { trackGTM } from '../analytics-handler.js'
import db from 'debug'
const { value: debug } = db.mock.results[db.mock.calls.findIndex(c => c[0] === 'webapp:analytics-handler')]

jest.mock('../../constants', () => ({
ANALYTICS: {
Expand All @@ -9,6 +11,8 @@ jest.mock('../../constants', () => ({
}
}))

jest.mock('debug', () => jest.fn(() => jest.fn()))

describe('The analytics handler', () => {
beforeEach(() => {
jest.clearAllMocks()
Expand Down Expand Up @@ -101,6 +105,64 @@ describe('The analytics handler', () => {
expect(mockAnalyticsSet).not.toHaveBeenCalled()
})

describe('trackGTM', () => {
it.each([
[true, true],
[false, false]
])('when acceptedTracking property equals %s, trackGTM should return %s', async (tracking, expectedResult) => {
const analytics = {
[ANALYTICS.acceptTracking]: tracking
}
const result = await trackGTM(generateRequestMock('payload', analytics))

expect(result).toBe(expectedResult)
})

it.each([
[true, false, 'Session is being tracked'],
[false, false, 'Session is not being tracked'],
[true, false, 'Session is being tracked'],
[false, true, 'Session is not being tracked for current page']
])(
'when tracking is %s, GTM container Id has value, ENABLE_ANALYTICS_OPT_IN_DEBUGGING is true and pageOmitted is %s, trackGTM returns %s',
async (tracking, skip, expectedResult) => {
const analytics = {
[ANALYTICS.acceptTracking]: tracking,
[ANALYTICS.omitPageFromAnalytics]: skip
}
process.env.GTM_CONTAINER_ID = 'ABC123'
process.env.ENABLE_ANALYTICS_OPT_IN_DEBUGGING = true

await trackGTM(generateRequestMock('payload', analytics))

expect(debug).toHaveBeenCalledWith(expectedResult)
}
)

it('debug isnt called if ENABLE_ANALYTICS_OPT_IN_DEBUGGING is set to false', async () => {
const analytics = {
[ANALYTICS.acceptTracking]: true
}
process.env.ENABLE_ANALYTICS_OPT_IN_DEBUGGING = false

await trackGTM(generateRequestMock('payload', analytics))

expect(debug).toBeCalledTimes(0)
})

it('debug isnt called if GTM_CONTAINER_ID is undefined', async () => {
const analytics = {
[ANALYTICS.acceptTracking]: true
}
process.env.ENABLE_ANALYTICS_OPT_IN_DEBUGGING = 'true'
delete process.env.GTM_CONTAINER_ID

await trackGTM(generateRequestMock('payload', analytics))

expect(debug).toBeCalledTimes(0)
})
})

const mockAnalyticsSet = jest.fn()

const generateRequestMock = (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ANALYTICS } from '../../constants.js'
import { CacheError } from '../../session-cache/cache-manager.js'
import { trackAnalyticsAccepted, getAnalyticsSessionId, pageOmitted } from '../analytics-handler.js'
import { trackAnalyticsAccepted, pageOmitted } from '../analytics-handler.js'

jest.mock('../../constants', () => ({
ANALYTICS: {
Expand Down Expand Up @@ -62,34 +62,6 @@ describe('pageOmitted', () => {
})
})

describe('getAnalyticsSessionId', () => {
beforeEach(() => {
jest.clearAllMocks()
})

it.each([['session_id_example'], ['testing_session_id'], ['example_session_id']])('returns the value sessionId', async id => {
const result = await getAnalyticsSessionId(
generateRequestMock(
() => null,
() => id
)
)
expect(result).toEqual(id)
})

it('empty session cache returns false', async () => {
const result = await getAnalyticsSessionId(
generateRequestMock(
() => null,
() => {
throw new CacheError()
}
)
)
expect(result).toEqual(null)
})
})

const generateRequestMock = (analytics, getId = () => {}) => ({
cache: jest.fn(() => ({
getId: jest.fn(getId),
Expand Down
Loading

0 comments on commit 8b07cee

Please sign in to comment.