Skip to content

Commit

Permalink
Switch source of GTM based on flag
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverturner committed Oct 29, 2024
1 parent ec78111 commit b234703
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
12 changes: 12 additions & 0 deletions packages/dotcom-ui-shell/src/__test__/components/GTMHead.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,16 @@ describe('dotcom-ui-shell/src/components/GTMHead', () => {
const tree = renderer.create(<GTMHead {...props} />).toJSON()
expect(tree).toMatchSnapshot()
})

it('renders the first party gtm script when the ads-first-party-gtm flag is on', () => {
const props = {
flags: {
enableGTM: true,
'ads-first-party-gtm': true
}
}

const tree = renderer.create(<GTMHead {...props} />).toJSON()
expect(tree).toMatchSnapshot()
})
})
16 changes: 11 additions & 5 deletions packages/dotcom-ui-shell/src/components/GTMHead.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,17 @@ const GTMHead = ({ flags }: { flags: TFlagsData }) => {
return null
}

const tagManager = `(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-NWQJW68');`
const src = flags['ads-first-party-gtm']
? 'https://www.ft.com/page-resources'
: 'https://www.googletagmanager.com/gtm.js?id=GTM-NWQJW68'

const tagManager = `(function(w,d,s,l){
w[l]=w[l]||[];w[l].push({'gtm.start': new Date().getTime(),event:'gtm.js'});
var f=d.getElementsByTagName(s)[0],j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';
j.async=true;
j.src='${src}'+dl;
f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer');`

return <script dangerouslySetInnerHTML={{ __html: tagManager }} />
}
Expand Down

0 comments on commit b234703

Please sign in to comment.