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
17 changes: 14 additions & 3 deletions studio/next.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { withSentryConfig } from "@sentry/nextjs";
import pkg from "./package.json" with { type: "json" };

const isPreview = process.env.VERCEL_ENV === "preview";
const isProduction = process.env.NODE_ENV === "production";
// Allow it only for development once https://github.com/vercel/next.js/issues/23587 is fixed
const allowUnsafeEval = true;
// Report CSP violations to the console instead of blocking them
Expand Down Expand Up @@ -54,19 +55,29 @@ const lightweightCspHeader = `
object-src 'none';
base-uri 'self';
font-src 'self' data:;
frame-src 'self' https://js.stripe.com https://hooks.stripe.com ${
frame-src 'self' https://js.stripe.com https://hooks.stripe.com https://www.googletagmanager.com ${
isPreview ? "https://vercel.live/ https://vercel.com" : ""
};
img-src 'self'${
isPreview
? " https://vercel.live/ https://vercel.com *.pusher.com/ data: blob:"
: ""
} *.ads.linkedin.com;
} *.ads.linkedin.com *.google.com;
script-src 'report-sample' 'self' 'unsafe-inline' ${
allowUnsafeEval ? "'unsafe-eval'" : ""
} https://*.wundergraph.com https://js.stripe.com https://maps.googleapis.com https://plausible.io https://wundergraph.com https://static.reo.dev${
isPreview ? " https://vercel.live https://vercel.com" : ""
} https://www.googletagmanager.com https://snap.licdn.com;
} ${
isProduction
? [
"https://www.googletagmanager.com",
"https://snap.licdn.com",
"https://cmp.osano.com",
"https://googleads.g.doubleclick.net",
"https://*.clarity.ms",
].join(" ")
: ""
};
manifest-src 'self';
media-src 'self';
worker-src 'self' ${isSentryFeatureReplayEnabled ? "blob:" : ""};
Expand Down
29 changes: 19 additions & 10 deletions studio/src/components/layout/analytics/gtm-script.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import Script from "next/script";
export interface GtmScriptProps {
gtmId: string | undefined;
};

export function GtmScript() {
const gtmId = process.env.NEXT_PUBLIC_GOOGLE_TAG_MANAGER_ID;
export function GtmScript({ gtmId }: GtmScriptProps) {
if (!gtmId) {
return null;
}

return (
<>
<Script id="gtm" strategy="afterInteractive">{`
<script
id="gtm"
dangerouslySetInnerHTML={{__html: `
(function(w,d,s,l,i){
w[l]=w[l]||[];
w[l].push({'gtm.start': new Date().getTime(), event:'gtm.js'});
Expand All @@ -14,9 +20,13 @@ export function GtmScript() {
j.async=true; j.src='https://www.googletagmanager.com/gtm.js?id='+i+dl;
f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer',${JSON.stringify(gtmId)});
`}</Script>
`
}}
/>

<Script id="gtm-consent" strategy="afterInteractive">{`
<script
id="gtm-consent"
dangerouslySetInnerHTML={{ __html: `
(function(){
function updateConsentFromOsano() {
const consent = window.Osano && Osano.cm && Osano.cm.getConsent ? Osano.cm.getConsent() : null;
Expand Down Expand Up @@ -45,14 +55,13 @@ export function GtmScript() {
window.addEventListener('osano-cm-initialized', onOsanoReady, { once: true });
}
})();
`}</Script>
`}}/>
</>
);
}

export function GtmNoScript() {
const gtmId = process.env.NEXT_PUBLIC_GOOGLE_TAG_MANAGER_ID;
if (!gtmId || process.env.NODE_ENV !== 'production') {
export function GtmNoScript({ gtmId }: GtmScriptProps) {
if (!gtmId) {
return null;
}

Expand Down
8 changes: 5 additions & 3 deletions studio/src/pages/_document.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ export default function Document() {
)}

{gtmId && (
<Script id="gtm-default" strategy="beforeInteractive">{`window.dataLayer = window.dataLayer || [];
<>
<Script id="gtm-default" strategy="beforeInteractive">{`window.dataLayer = window.dataLayer || [];
function gtag(){ dataLayer.push(arguments); }
gtag('consent', 'default', {
ad_storage: 'denied',
Expand All @@ -45,10 +46,11 @@ gtag('consent', 'default', {
functionality_storage: 'granted', // essentials
security_storage: 'granted'
});`}</Script>
<GtmScript gtmId={gtmId} />
</>
)}
</>
)}
{isProduction && <GtmScript />}

<link
rel="apple-touch-icon"
Expand Down Expand Up @@ -128,7 +130,7 @@ gtag('consent', 'default', {
<meta name="theme-color" content="#ffffff" />
</Head>
<body>
{isProduction && <GtmNoScript />}
{isProduction && <GtmNoScript gtmId={gtmId} />}
<Main />
<NextScript />

Expand Down
Loading