Skip to content

Commit 91c6846

Browse files
🎨 update gmt to lib and conditionally render only if prod #3851 (#3852)
* 🎨 update gmt to lib and conditionally render only if prod * 🎨 clean up * 🎨 update video analytics
1 parent 76d0fb2 commit 91c6846

7 files changed

Lines changed: 90 additions & 172 deletions

File tree

‎web/app/[locale]/GTMHead.tsx‎

Lines changed: 0 additions & 17 deletions
This file was deleted.

‎web/app/[locale]/NavigationEvents.tsx‎

Lines changed: 0 additions & 32 deletions
This file was deleted.

‎web/app/[locale]/layout.tsx‎

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import '../globals.css'
2-
import dynamic from 'next/dynamic'
2+
import { GoogleTagManager } from '@next/third-parties/google'
33
import localFont from 'next/font/local'
44
import { draftMode } from 'next/headers'
55
import NextLink from 'next/link'
@@ -15,7 +15,6 @@ import { footerAndErrorImageQuery } from '@/sanity/queries/footer'
1515
import Footer from '@/sections/Footer/Footer'
1616
import GoToTopButton from '@/sections/GoToTopButton'
1717
import { routing } from '../../i18n/routing'
18-
import { GoogleTagManagerHead } from './GTMHead'
1918
import { SiteImprove } from './SiteImprove'
2019

2120
const equinor = localFont({
@@ -26,10 +25,6 @@ const equinor = localFont({
2625
],
2726
})
2827

29-
const DraftModeToolbar = dynamic(
30-
() => import('@/sections/DraftMode/DraftModeToolbar'),
31-
)
32-
3328
type Params = Promise<{ locale: string }>
3429

3530
/* export const metadata: Metadata = {
@@ -97,17 +92,24 @@ export default async function LocaleLayout({
9792
</body>
9893
{/** TODO look into scripts */}
9994
{!(isPreview || dataset === 'global-development') && (
100-
<Script
101-
src='https://consent.cookiebot.com/uc.js'
102-
id='Cookiebot'
103-
strategy='beforeInteractive'
104-
data-cbid='f1327b03-7951-45da-a2fd-9181babc783f'
105-
data-blockingmode='auto'
106-
data-culture={locale === 'nb-NO' ? 'nb' : getLocaleFromIso(locale)}
107-
/>
95+
<>
96+
<Script
97+
src='https://consent.cookiebot.com/uc.js'
98+
id='Cookiebot'
99+
strategy='beforeInteractive'
100+
data-cbid='f1327b03-7951-45da-a2fd-9181babc783f'
101+
data-blockingmode='auto'
102+
data-culture={locale === 'nb-NO' ? 'nb' : getLocaleFromIso(locale)}
103+
/>
104+
{process.env.NEXT_PUBLIC_GOOGLE_TAG_MANAGER_ID && (
105+
//https://nextjs.org/docs/app/guides/third-party-libraries#google-third-parties
106+
<GoogleTagManager
107+
gtmId={process.env.NEXT_PUBLIC_GOOGLE_TAG_MANAGER_ID}
108+
/>
109+
)}
110+
<SiteImprove />
111+
</>
108112
)}
109-
<GoogleTagManagerHead />
110-
<SiteImprove />
111113
</html>
112114
)
113115
}

‎web/core/VideoJsPlayer/useVideojsAnalytics.ts‎

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
'use client'
2+
import { sendGTMEvent } from '@next/third-parties/google'
3+
import { useCallback, useEffect, useState } from 'react'
4+
import type Player from 'video.js/dist/types/player'
25
import useConsent from '../../lib/hooks/useConsent'
3-
import { pushToDataLayer } from '../../lib/gtm'
4-
import { useEffect, useCallback, useState } from 'react'
5-
import Player from 'video.js/dist/types/player'
66

77
const GTM_PLAY_EVENT = 'video_play'
88
const GTM_PAUSE_EVENT = 'video_pause'
99
const GTM_COMPLETION_EVENT = 'video_completion'
1010
const GTM_PROGRESS_MILESTONES = [25, 50, 75, 90] // Percentages
1111

12-
type EventType = typeof GTM_PLAY_EVENT | typeof GTM_PAUSE_EVENT | typeof GTM_COMPLETION_EVENT | string
12+
type EventType =
13+
| typeof GTM_PLAY_EVENT
14+
| typeof GTM_PAUSE_EVENT
15+
| typeof GTM_COMPLETION_EVENT
16+
| string
1317

1418
type EventData = {
1519
eventType: EventType
@@ -20,7 +24,12 @@ type EventData = {
2024
}
2125

2226
// Video Analytics Hook
23-
const useVideojsAnalytics = (player: Player | null, src: string, title?: string, autoPlay?: boolean): void => {
27+
const useVideojsAnalytics = (
28+
player: Player | null,
29+
src: string,
30+
title?: string,
31+
autoPlay?: boolean,
32+
): void => {
2433
const allowAnalytics = useConsent(['statistics']) || false
2534

2635
const pushEventToDataLayer = useCallback(
@@ -33,7 +42,7 @@ const useVideojsAnalytics = (player: Player | null, src: string, title?: string,
3342
currentTime: player.currentTime() || 0,
3443
src,
3544
}
36-
pushToDataLayer('video_event', eventData)
45+
sendGTMEvent({ event: 'video_event', ...eventData })
3746
}
3847
},
3948
[title, src, autoPlay],
@@ -120,10 +129,10 @@ const useVideoProgressEvent = (
120129
const currentTime = player.currentTime()
121130
if (currentTime && duration) {
122131
const progress = (currentTime / duration) * 100
123-
GTM_PROGRESS_MILESTONES.forEach((milestone) => {
132+
GTM_PROGRESS_MILESTONES.forEach(milestone => {
124133
if (progress >= milestone && !trackedMilestones.includes(milestone)) {
125134
pushEvent(`video_progress_${milestone}`, player)
126-
setTrackedMilestones((prev) => [...prev, milestone])
135+
setTrackedMilestones(prev => [...prev, milestone])
127136
}
128137
})
129138
}

‎web/lib/gtm.js‎

Lines changed: 0 additions & 16 deletions
This file was deleted.

‎web/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"@friendlycaptcha/sdk": "^0.2.0",
3333
"@hookform/resolvers": "^5.2.2",
3434
"@next/bundle-analyzer": "^16.2.2",
35+
"@next/third-parties": "16.2.12",
3536
"@portabletext/react": "^6.0.3",
3637
"@portabletext/to-html": "^5.0.2",
3738
"@radix-ui/react-accordion": "^1.2.12",

0 commit comments

Comments
 (0)