Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Google Tag Manager tracker #3497

Merged
merged 7 commits into from
Jun 15, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions catalog/app/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { ExperimentsProvider } from 'components/Experiments'
import * as Intercom from 'components/Intercom'
import Placeholder from 'components/Placeholder'
import App from 'containers/App'
import GTMLoader from 'utils/gtm'
import * as Auth from 'containers/Auth'
import * as Errors from 'containers/Errors'
import * as Notifications from 'containers/Notifications'
Expand Down Expand Up @@ -97,6 +98,7 @@ const render = () => {
Notifications.Provider,
[APIConnector.Provider, { fetch, middleware: [Auth.apiMiddleware] }],
[Auth.Provider, { checkOn: LOCATION_CHANGE, storage }],
GTMLoader,
[
Intercom.Provider,
{
Expand Down
1 change: 1 addition & 0 deletions catalog/app/utils/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export interface ConfigJson {
registryUrl: string
s3Proxy: string

gtmId?: string
intercomAppId?: string
mixpanelToken: string
sentryDSN?: string
Expand Down
40 changes: 40 additions & 0 deletions catalog/app/utils/gtm.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import * as React from 'react'

import cfg from 'constants/config'

function loadScript(src: string) {
const s = window.document.createElement('script')
s.type = 'text/javascript'
s.async = true
fiskus marked this conversation as resolved.
Show resolved Hide resolved
s.src = src

const x = window.document.getElementsByTagName('script')[0]
x.parentNode?.insertBefore(s, x)
fiskus marked this conversation as resolved.
Show resolved Hide resolved
}

function addScript(content: string) {
const s = window.document.createElement('script')
s.type = 'text/javascript'
s.innerText = content
const x = window.document.getElementsByTagName('script')[0]
x.parentNode?.insertBefore(s, x)
}

interface GTMLoaderProps {
children: React.ReactNode
}

export default function GTMLoader({ children }: GTMLoaderProps) {
const gtmId = cfg.gtmId
fiskus marked this conversation as resolved.
Show resolved Hide resolved
React.useEffect(() => {
if (!gtmId) return
loadScript(`https://www.googletagmanager.com/gtag/js?id=${gtmId}`)
addScript(`
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '${gtmId}');
`)
}, [gtmId])
fiskus marked this conversation as resolved.
Show resolved Hide resolved
return <>{children}</>
}
4 changes: 4 additions & 0 deletions catalog/config-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@
"build_version": {
"type": "string",
"description": "Optional"
},
"gtmId": {
"type": "string",
"description": "ID for Google TagManager/Analytics service"
}
},
"required": [
Expand Down