Skip to content

Commit 2ccf86d

Browse files
authored
Merge pull request #1092 from Financial-Times/ADSDEV-2064/gtm-first-party-mode--example
2 parents e8b107e + 1f69707 commit 2ccf86d

File tree

4 files changed

+38
-3
lines changed

4 files changed

+38
-3
lines changed

examples/ft-ui/__test__/gtm.test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const baseUrl = 'http://localhost:3456'
2+
3+
describe('examples/ft-ui/gtm', () => {
4+
let response
5+
6+
it('By default it loads GTM from googletagmanager.com', async () => {
7+
await page.goto(baseUrl, { waitUntil: 'load' })
8+
await expect(page).toMatchElement('script[src="https://www.googletagmanager.com/gtm.js?id=GTM-NWQJW68"]')
9+
})
10+
11+
// Remove this once "ads-first-party-gtm" Toggler flag is removed
12+
if (!process.env.CI) {
13+
it('Behind a flag it loads GTM from first-party proxy', async () => {
14+
await page.goto(`${baseUrl}?ads-first-party-gtm=true`, { waitUntil: 'load' })
15+
16+
// Since the query sets the appName to "home-page", the PG load is triggered
17+
// This acts as a proxy for verifying GTM's behaviour
18+
await page.waitForSelector('script[src^="https://www.ft.com/__assets/hashed/pg-ads/adapters"]')
19+
20+
await expect(page).toMatchElement('script[src="https://www.ft.com/page-resources/"]')
21+
})
22+
}
23+
})

examples/ft-ui/server/controllers/home.jsx

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ import ReactDOM from 'react-dom/server'
33
import { Shell } from '@financial-times/dotcom-ui-shell'
44
import { Layout } from '@financial-times/dotcom-ui-layout'
55

6+
const flags = {
7+
ads: true,
8+
enableGTM: true,
9+
adsEnableTestCreatives: true
10+
}
11+
612
export function homeController(request, response, next) {
713
const appContext = {
814
appName: 'ft-ui',
@@ -14,11 +20,17 @@ export function homeController(request, response, next) {
1420
contents: '<div align="center"><p>Hello, welcome to Page Kit.</p></div>'
1521
}
1622

23+
if (request.query['ads-first-party-gtm']) {
24+
flags['ads-first-party-gtm'] = true
25+
appContext.appName = 'home-page'
26+
}
27+
1728
const shellProps = {
1829
scripts: ['public/scripts.bundle.js'],
1930
stylesheets: ['public/page-kit-layout-styles.css', 'public/styles.css'],
2031
pageTitle: pageData.title,
21-
appContext: appContext
32+
appContext,
33+
flags
2234
}
2335

2436
const userIsLoggedIn = request.query.userIsLoggedIn

packages/dotcom-ui-shell/src/__test__/components/__snapshots__/GTMHead.test.tsx.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ exports[`dotcom-ui-shell/src/components/GTMHead renders the first party gtm scri
1010
w[l]=w[l]||[];w[l].push({'gtm.start': new Date().getTime(),event:'gtm.js'});
1111
var f=d.getElementsByTagName(s)[0],j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';
1212
j.async=true;
13-
j.src='https://www.ft.com/page-resources'+dl;
13+
j.src='https://www.ft.com/page-resources/'+dl;
1414
f.parentNode.insertBefore(j,f);
1515
})(window,document,'script','dataLayer');",
1616
}

packages/dotcom-ui-shell/src/components/GTMHead.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const GTMHead = ({ flags }: { flags: TFlagsData }) => {
88
}
99

1010
const src = flags['ads-first-party-gtm']
11-
? 'https://www.ft.com/page-resources'
11+
? 'https://www.ft.com/page-resources/'
1212
: 'https://www.googletagmanager.com/gtm.js?id=GTM-NWQJW68'
1313

1414
const tagManager = `(function(w,d,s,l){

0 commit comments

Comments
 (0)