Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
VITE_GTM_CONTAINER_ID=GTM-XXXXXXX
2 changes: 2 additions & 0 deletions .github/workflows/deploy-s3-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ jobs:

- name: Build
run: npm run build
env:
VITE_GTM_CONTAINER_ID: ${{ secrets.DEV_GTM_CONTAINER_ID }}

- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/deploy-s3-prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ jobs:

- name: Build
run: npm run build
env:
VITE_GTM_CONTAINER_ID: ${{ secrets.GTM_CONTAINER_ID }}

- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
Expand Down
32 changes: 32 additions & 0 deletions src/gtm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
interface GTMEvent {
event: string;
[key: string]: unknown;
}

declare global {
interface Window {
dataLayer: GTMEvent[];
}
}

export const initGTM = (containerId?: string): void => {
if (!containerId) return;

window.dataLayer = window.dataLayer ?? [];
window.dataLayer.push({ "gtm.start": new Date().getTime(), event: "gtm.js" });

const script = document.createElement("script");
script.async = true;
script.src = `https://www.googletagmanager.com/gtm.js?id=${containerId}`;
document.head.insertBefore(script, document.head.firstChild);

const iframe = document.createElement("iframe");
iframe.src = `https://www.googletagmanager.com/ns.html?id=${containerId}`;
iframe.height = "0";
iframe.width = "0";
iframe.style.display = "none";
iframe.style.visibility = "hidden";
const noscript = document.createElement("noscript");
noscript.appendChild(iframe);
document.body.insertBefore(noscript, document.body.firstChild);
};
3 changes: 3 additions & 0 deletions src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import ReactDOM from "react-dom";
import "./index.css";
import "shepherd.js/dist/css/shepherd.css";
import App from "./app";
import { initGTM } from "./gtm";

initGTM(import.meta.env.VITE_GTM_CONTAINER_ID);

ReactDOM.render(
<React.StrictMode>
Expand Down
5 changes: 5 additions & 0 deletions src/vite-env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/// <reference types="vite/client" />

interface ImportMetaEnv {
readonly VITE_GTM_CONTAINER_ID?: string;
}
Loading