Skip to content

Commit

Permalink
Only insert snippet if container ID is present
Browse files Browse the repository at this point in the history
  • Loading branch information
irisfaraway committed Oct 17, 2023
1 parent c288a3e commit 4a06d76
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 0 deletions.
1 change: 1 addition & 0 deletions docker/env/gafl_webapp.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ DEBUG=webapp:*

# Tracking
#ANALYTICS_PRIMARY_PROPERTY=G-DJMSHRPMW8 // this is the non-prod tracking update as required
#GTM_CONTAINER_ID=GTM-ABC1234

# Welsh language feature switch
SHOW_WELSH_CONTENT=true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ exports[`The page handler function sets the value of pageData with displayAnalyt
"analyticsSelected": false,
"backRef": null,
"displayAnalytics": false,
"gtmContainerId": null,
"mssgs": undefined,
"pageLanguageSetToWelsh": false,
"uri": Object {
Expand Down Expand Up @@ -41,6 +42,7 @@ exports[`The page handler function sets the value of pageData with displayAnalyt
"analyticsSelected": false,
"backRef": null,
"displayAnalytics": true,
"gtmContainerId": null,
"mssgs": undefined,
"pageLanguageSetToWelsh": false,
"uri": Object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,25 @@ describe('The page handler function', () => {
expect(toolkit.view).toMatchSnapshot()
})

it('sets the value of gtmContainerId to the GTM_CONTAINER_ID env var', async () => {
const expectedValue = 'expected'
process.env.GTM_CONTAINER_ID = expectedValue

const { get } = pageHandler('', 'view', '/next/page')
const toolkit = getMockToolkit()

await get(getMockRequest(), toolkit)

expect(toolkit.view).toHaveBeenCalledWith(
expect.any(String),
expect.objectContaining({
gtmContainerId: expectedValue
})
)

delete process.env.GTM_CONTAINER_ID
})

it.each([
['payment cancelled', PAYMENT_CANCELLED.uri],
['payment failed', PAYMENT_FAILED.uri],
Expand Down
1 change: 1 addition & 0 deletions packages/gafl-webapp-service/src/handlers/page-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ export default (path, view, completion, getData) => ({
pageData.analyticsMessageDisplayed = analytics ? analytics[ANALYTICS.seenMessage] : false
pageData.analyticsSelected = analytics ? analytics[ANALYTICS.selected] : false
pageData.acceptedTracking = analytics ? analytics[ANALYTICS.acceptTracking] : false
pageData.gtmContainerId = process.env.GTM_CONTAINER_ID || null

pageData.displayAnalytics = displayAnalytics(request)

Expand Down
3 changes: 3 additions & 0 deletions packages/gafl-webapp-service/src/pages/layout/layout.njk
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
<meta http-equiv="Cache-Control" content="no-store" />
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Expires" content="-1">
{% if gtmContainerId %}
<!-- GTM snippet goes here with container ID {{ gtmContainerId }} -->
{% endif %}
{% endblock %}

{% block header %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ describe('guidance page handlers', () => {

expect(mockToolkit.view).toHaveBeenCalledWith(uri.NEW_PRICES.page, {
altLang: ['that-locale'],
gtmContainerId: null,
mssgs: catalog,
uri: {
back: mockUri
Expand Down Expand Up @@ -262,6 +263,24 @@ describe('guidance page handlers', () => {
})
)
})

it('sets the value of gtmContainerId to the GTM_CONTAINER_ID env var', async () => {
const expectedValue = 'expected'
process.env.GTM_CONTAINER_ID = expectedValue

const toolkit = getMockToolkit()
const request = getMockRequest()
await pageHandler(request, toolkit)

expect(toolkit.view).toHaveBeenCalledWith(
expect.any(String),
expect.objectContaining({
gtmContainerId: expectedValue
})
)

delete process.env.GTM_CONTAINER_ID
})
})

const getMockRequest = (i18nValues, search = '') => {
Expand Down
8 changes: 8 additions & 0 deletions packages/gafl-webapp-service/src/routes/misc-routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,20 @@ import { addLanguageCodeToUri } from '../processors/uri-helper.js'
import analytics from '../handlers/analytics-handler.js'
import { welshEnabledAndApplied } from '../processors/page-language-helper.js'

const gtmContainerIdOrNull = () => process.env.GTM_CONTAINER_ID || null

const simpleView = view => ({
method: 'GET',
path: view.uri,
handler: async (request, h) => {
const mssgs = request.i18n.getCatalog()
const altLang = request.i18n.getLocales().filter(locale => locale !== request.i18n.getLocale())
const gtmContainerId = gtmContainerIdOrNull()
const pageLanguageSetToWelsh = welshEnabledAndApplied(request)
return h.view(view.page, {
mssgs,
altLang,
gtmContainerId,
pageLanguageSetToWelsh,
uri: {
back: addLanguageCodeToUri(request, CONTROLLER.uri)
Expand Down Expand Up @@ -95,10 +99,12 @@ export default [
path: COOKIES.uri,
handler: async (request, h) => {
const altLang = request.i18n.getLocales().filter(locale => locale !== request.i18n.getLocale())
const gtmContainerId = gtmContainerIdOrNull()
const pageLanguageSetToWelsh = welshEnabledAndApplied(request)

return h.view(COOKIES.page, {
altLang,
gtmContainerId,
pageLanguageSetToWelsh,
mssgs: request.i18n.getCatalog(),
cookie: {
Expand All @@ -118,10 +124,12 @@ export default [
path: NEW_PRICES.uri,
handler: async (request, h) => {
const altLang = request.i18n.getLocales().filter(locale => locale !== request.i18n.getLocale())
const gtmContainerId = gtmContainerIdOrNull()
const pageLanguageSetToWelsh = welshEnabledAndApplied(request)

return h.view(NEW_PRICES.page, {
altLang,
gtmContainerId,
pageLanguageSetToWelsh,
mssgs: request.i18n.getCatalog(),
uri: {
Expand Down

0 comments on commit 4a06d76

Please sign in to comment.