Skip to content
Merged
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
64 changes: 64 additions & 0 deletions app/src/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,60 @@
import type { Metadata } from "next";
import Script from "next/script";
import "./globals.css";

const GA_MEASUREMENT_ID = "G-2YHG89FY0N";
const TOOL_NAME = "policybench";

const ANALYTICS_INLINE = `
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '${GA_MEASUREMENT_ID}', { tool_name: '${TOOL_NAME}' });
`;

const SCROLL_INLINE = `
(function() {
var TOOL_NAME = '${TOOL_NAME}';
if (typeof window === 'undefined' || !window.gtag) return;

var scrollFired = {};
window.addEventListener('scroll', function() {
var docHeight = document.documentElement.scrollHeight - window.innerHeight;
if (docHeight <= 0) return;
var pct = Math.floor((window.scrollY / docHeight) * 100);
[25, 50, 75, 100].forEach(function(m) {
if (pct >= m && !scrollFired[m]) {
scrollFired[m] = true;
window.gtag('event', 'scroll_depth', { percent: m, tool_name: TOOL_NAME });
}
});
}, { passive: true });

[30, 60, 120, 300].forEach(function(sec) {
setTimeout(function() {
if (document.visibilityState !== 'hidden') {
window.gtag('event', 'time_on_tool', { seconds: sec, tool_name: TOOL_NAME });
}
}, sec * 1000);
});

document.addEventListener('click', function(e) {
var link = e.target && e.target.closest ? e.target.closest('a') : null;
if (!link || !link.href) return;
try {
var url = new URL(link.href, window.location.origin);
if (url.hostname && url.hostname !== window.location.hostname) {
window.gtag('event', 'outbound_click', {
url: link.href,
target_hostname: url.hostname,
tool_name: TOOL_NAME
});
}
} catch (err) {}
});
})();
`;

const SITE_URL = "https://policybench.org";
const SITE_TITLE = "PolicyBench, by PolicyEngine";
const SITE_DESCRIPTION =
Expand Down Expand Up @@ -60,6 +114,16 @@ export default function RootLayout({
Skip to main content
</a>
{children}
<Script
src={`https://www.googletagmanager.com/gtag/js?id=${GA_MEASUREMENT_ID}`}
strategy="afterInteractive"
/>
<Script id="ga-init" strategy="afterInteractive">
{ANALYTICS_INLINE}
</Script>
<Script id="scroll-tracking" strategy="afterInteractive">
{SCROLL_INLINE}
</Script>
</body>
</html>
);
Expand Down