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

Unify Google Analytics #1035

Merged
merged 10 commits into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from 6 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: 0 additions & 1 deletion .github/workflows/charterafrica-deploy-prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ jobs:
build-args: |
MONGO_URL=${{ secrets.CHARTERAFRICA_MONGO_URL }}
NEXT_PUBLIC_APP_URL=${{ env.NEXT_PUBLIC_APP_URL }}
NEXT_PUBLIC_GA_MEASUREMENT_ID=${{ secrets.CHARTERAFRICA_GA_MEASUREMENT_ID }}
NEXT_PUBLIC_SENTRY_DSN=${{ secrets.CHARTERAFRICA_SENTRY_DSN }}
NEXT_PUBLIC_SEO_DISABLED=${{ env.NEXT_PUBLIC_SEO_DISABLED }}
PAYLOAD_SECRET_KEY=${{ secrets.CHARTERAFRICA_PAYLOAD_SECRET_KEY }}
Expand Down
13 changes: 1 addition & 12 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ ARG NEXT_TELEMETRY_DISABLED \
NEXT_PUBLIC_APP_URL \
NEXT_PUBLIC_SENTRY_DSN \
NEXT_PUBLIC_SEO_DISABLED \
NEXT_PUBLIC_GOOGLE_ANALYTICS \
NEXT_PUBLIC_GOOGLE_ANALYTICS_ID \
PORT \
SENTRY_ENVIRONMENT

Expand Down Expand Up @@ -298,13 +298,6 @@ ARG NEXT_PUBLIC_APP_LOGO_URL \
PAYLOAD_CONFIG_PATH="dist/payload.config.js" \
PAYLOAD_PUBLIC_APP_URL

# TODO(koech): Standadise naming of GA MEASUREMENT ID. Our options:
# - GA_MEASUREMENT_ID (charterafrica)
# - GOOGLE_ANALYTICS_ID (pesayetu, vpnmanager)
# This is only needed at runtime
# TODO(koech): Completely remove the use of ENV vars for Google Analytics
# for those app that have CMS. Measurement id should be set
# in the Settings part of a site.
ENV NEXT_PUBLIC_APP_LOGO_URL=${NEXT_PUBLIC_APP_LOGO_URL} \
PAYLOAD_PUBLIC_APP_URL=${PAYLOAD_PUBLIC_APP_URL} \
PAYLOAD_CONFIG_PATH=${PAYLOAD_CONFIG_PATH}
Expand Down Expand Up @@ -379,7 +372,6 @@ ARG NEXT_TELEMETRY_DISABLED \
NEXT_PUBLIC_SEO_DISABLED \
NEXT_PUBLIC_IMAGE_DOMAINS="cms.dev.codeforafrica.org,hurumap-v2.s3.amazonaws.com" \
NEXT_PUBLIC_IMAGE_SCALE_FACTOR=2 \
NEXT_PUBLIC_GOOGLE_ANALYTICS \
# Payload (runtime)
MONGO_URL \
PAYLOAD_SECRET \
Expand Down Expand Up @@ -599,7 +591,6 @@ ARG NEXT_TELEMETRY_DISABLED \
NEXT_PUBLIC_IMAGE_SCALE_FACTOR=2 \
NEXT_PUBLIC_OPENAFRICA_DOMAINS="open.africa,openafrica.net,africaopendata.org" \
NEXT_PUBLIC_SOURCEAFRICA_DOMAINS="dc.sourceafrica.net" \
NEXT_PUBLIC_GOOGLE_ANALYTICS \
# Sentry (build time)
SENTRY_AUTH_TOKEN \
SENTRY_ENVIRONMENT \
Expand Down Expand Up @@ -791,7 +782,6 @@ ARG NEXT_TELEMETRY_DISABLED \
NEXT_PUBLIC_APP_URL \
NEXT_PUBLIC_SENTRY_DSN \
NEXT_PUBLIC_SEO_DISABLED \
NEXT_PUBLIC_GOOGLE_ANALYTICS \
# Sentry (build time)
SENTRY_AUTH_TOKEN \
SENTRY_ENVIRONMENT \
Expand Down Expand Up @@ -866,7 +856,6 @@ ARG NEXT_TELEMETRY_DISABLED \
NEXT_PUBLIC_APP_URL \
NEXT_PUBLIC_SENTRY_DSN \
NEXT_PUBLIC_SEO_DISABLED \
NEXT_PUBLIC_GOOGLE_ANALYTICS \
# Sentry (build time)
SENTRY_AUTH_TOKEN \
SENTRY_ENVIRONMENT \
Expand Down
1 change: 1 addition & 0 deletions apps/charterafrica/.env
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ PAYLOAD_PUBLIC_LOCALES="en, fr, pt"
SENTRY_ENVIRONMENT=local
SENTRY_ORG=code-for-africa
SENTRY_PROJECT=charterafrica
NEXT_PUBLIC_GOOGLE_ANALYTICS_ID="G-5ZPEFLT0NJ"
kelvinkipruto marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions apps/charterafrica/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"@mui/material": "catalog:",
"@mui/utils": "catalog:",
"@next/env": "catalog:",
"@next/third-parties": "catalog:",
"@nivo/core": "catalog:",
"@nivo/pie": "catalog:",
"@nivo/tooltip": "catalog:",
Expand Down
4 changes: 4 additions & 0 deletions apps/charterafrica/src/lib/data/common/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,9 @@ export async function getPageProps(api, context) {
{ defaultLocale, locale },
api,
);

const { analytics } = settings;

const seo = getPageSeoFromMeta(processedPage, settings, {
defaultLocale,
locale,
Expand All @@ -189,6 +192,7 @@ export async function getPageProps(api, context) {
return {
...globalProps,
...processedPage,
analytics,
seo,
};
}
Expand Down
25 changes: 0 additions & 25 deletions apps/charterafrica/src/lib/gtag.js

This file was deleted.

41 changes: 5 additions & 36 deletions apps/charterafrica/src/pages/_app.page.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import { CacheProvider } from "@emotion/react";
import { ThemeProvider, CssBaseline } from "@mui/material";
import { deepmerge } from "@mui/utils";
import { GoogleAnalytics } from "@next/third-parties/google";
import App from "next/app";
import { useRouter } from "next/router";
import Script from "next/script";
import { DefaultSeo } from "next-seo";
import PropTypes from "prop-types";
import React, { useEffect } from "react";
import React from "react";

import Layout from "@/charterafrica/components/Layout";
import * as gtag from "@/charterafrica/lib/gtag";
import SEO from "@/charterafrica/next-seo.config";
import "@/charterafrica/theme/fonts.css";
import theme from "@/charterafrica/theme";
Expand All @@ -22,50 +20,21 @@ function getDefaultLayout(page, pageProps) {
}

function MyApp(props) {
const router = useRouter();
const { Component, emotionCache = clientSideEmotionCache, pageProps } = props;
const { analytics } = pageProps;
const { analyticsId: gaId } = analytics || {};
const getLayout = Component.getLayout || getDefaultLayout;
useEffect(() => {
const handleRouteChange = (url) => {
gtag.pageview(url);
};
router.events.on("routeChangeComplete", handleRouteChange);
router.events.on("hashChangeComplete", handleRouteChange);

return () => {
router.events.off("routeChangeComplete", handleRouteChange);
router.events.off("hashChangeComplete", handleRouteChange);
};
}, [router.events]);

return (
<>
<Script
strategy="afterInteractive"
src={`https://www.googletagmanager.com/gtag/js?id=${gtag.GA_MEASUREMENT_ID}`}
/>
<Script
id="gtag-init"
strategy="afterInteractive"
dangerouslySetInnerHTML={{
__html: `
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '${gtag.GA_MEASUREMENT_ID}', {
page_path: window.location.pathname,
});
`,
}}
/>
<DefaultSeo {...SEO} />
<CacheProvider value={emotionCache}>
<ThemeProvider theme={theme}>
<CssBaseline />
{getLayout(<Component {...pageProps} />, pageProps)}
</ThemeProvider>
</CacheProvider>
<GoogleAnalytics gaId={gaId} />
</>
);
}
Expand Down
13 changes: 0 additions & 13 deletions apps/charterafrica/src/pages/_document.page.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import createEmotionServer from "@emotion/server/create-instance";
import Document, { Head, Html, Main, NextScript } from "next/document";
import React from "react";

import * as gtag from "@/charterafrica/lib/gtag";
import createEmotionCache from "@/charterafrica/utils/createEmotionCache";

export default class MyDocument extends Document {
Expand Down Expand Up @@ -55,18 +54,6 @@ export default class MyDocument extends Document {
<meta name="theme-color" content="#ffffff" />
<meta name="emotion-insertion-point" content="" />
{this.props.emotionStyleTags}
{gtag.isSet ? (
<script
// eslint-disable-next-line react/no-danger
dangerouslySetInnerHTML={{
__html: `
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
`,
}}
/>
) : null}
</Head>
<body>
<Main />
Expand Down
23 changes: 23 additions & 0 deletions apps/charterafrica/src/payload/globals/Settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,29 @@ const Settings = {
initCollapsed: true,
},
},
{
name: "analytics",
type: "group",
label: "Site Analytics",
localized: true,
fields: [
{
type: "collapsible",
label: "Google Analytics",
fields: [
{
name: "analyticsId",
type: "text",
defaultValue: "G-5ZPEFLT0NJ", // From .env
},
],
admin: {
description:
"Measurement ID: https://support.google.com/analytics/answer/12270356",
},
},
],
},
],
};

Expand Down
2 changes: 1 addition & 1 deletion apps/charterafrica/turbo.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"NEXT_MANUAL_SIG_HANDLE",
"NEXT_PUBLIC_APP_URL",
"NEXT_PUBLIC_DEFAULT_LOCALE",
"NEXT_PUBLIC_GA_MEASUREMENT_ID",
"NEXT_PUBLIC_GOOGLE_ANALYTICS_ID",
"NEXT_PUBLIC_IMAGE_DOMAINS",
"NEXT_PUBLIC_LOCALES",
"NEXT_PUBLIC_SENTRY_DSN",
Expand Down
1 change: 0 additions & 1 deletion apps/civicsignalblog/turbo.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
"NEXT_PUBLIC_APP_LOGO_URL",
"NEXT_PUBLIC_APP_NAME",
"NEXT_PUBLIC_APP_URL",
"NEXT_PUBLIC_GOOGLE_ANALYTICS_ID",
"NEXT_PUBLIC_IMAGE_DOMAINS",
"NEXT_PUBLIC_IMAGE_UNOPTIMIZED",
"NEXT_PUBLIC_SENTRY_DSN",
Expand Down
3 changes: 0 additions & 3 deletions apps/climatemappedafrica/.env.template
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ NEXT_PUBLIC_OPENAFRICA_DOMAINS=
# based site domain)
NEXT_PUBLIC_SOURCEAFRICA_DOMAINS=

# Google Analytics
NEXT_PUBLIC_GOOGLE_ANALYTICS_ID = "G-xxxxxxxx"

# AWS S3 bucket for storing images
S3_ACCESS_KEY_ID=AAAAAAAAAAAAAAAAAAAA
S3_SECRET_ACCESS_KEY=AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
Expand Down
1 change: 1 addition & 0 deletions apps/climatemappedafrica/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"@mui/utils": "catalog:mui-styles",
"@mui/x-tree-view": "catalog:",
"@next/env": "catalog:",
"@next/third-parties": "catalog:",
"@payloadcms/bundler-webpack": "catalog:",
"@payloadcms/db-mongodb": "catalog:",
"@payloadcms/plugin-cloud-storage": "catalog:",
Expand Down
2 changes: 2 additions & 0 deletions apps/climatemappedafrica/src/lib/data/common/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ export async function getPageProps(api, context) {
let variant = "default";
const settings = {};
settings.site = (await api.findGlobal("settings-site")) || null;
const { analytics } = settings.site;
const hurumapSettings = await api.findGlobal("settings-hurumap");
if (hurumapSettings?.enabled) {
// TODO(koech): Handle cases when fetching profile fails?
Expand Down Expand Up @@ -156,6 +157,7 @@ export async function getPageProps(api, context) {
const menus = await getNavBar(variant, settings);

return {
analytics,
blocks,
footer,
menus,
Expand Down
13 changes: 0 additions & 13 deletions apps/climatemappedafrica/src/lib/ga/index.js

This file was deleted.

24 changes: 5 additions & 19 deletions apps/climatemappedafrica/src/pages/_app.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
import { CssBaseline } from "@mui/material";
import { ThemeProvider, StyledEngineProvider } from "@mui/material/styles";
import { ThemeProvider as StyledThemeProvider } from "@mui/styles";
import { useRouter } from "next/router";
import { GoogleAnalytics } from "@next/third-parties/google";
import { DefaultSeo } from "next-seo";
import PropTypes from "prop-types";
import React from "react";

import * as ga from "@/climatemappedafrica/lib/ga";
import "@/climatemappedafrica/theme/fonts.css";
import SEO from "@/climatemappedafrica/next-seo.config";
import theme from "@/climatemappedafrica/theme";

export default function MyApp(props) {
const { Component, pageProps } = props;

const { analytics } = pageProps;
const { analyticsId: gaId } = analytics || {};

React.useEffect(() => {
// Remove the server-side injected CSS.
/* eslint-env browser */
Expand All @@ -23,23 +25,6 @@ export default function MyApp(props) {
}
}, []);

const router = useRouter();

React.useEffect(() => {
const handleRouteChange = (url) => {
ga.pageview(url);
};
// When the component is mounted, subscribe to router changes
// and log those page views
router.events.on("routeChangeComplete", handleRouteChange);

// If the component is unmounted, unsubscribe
// from the event with the `off` method
return () => {
router.events.off("routeChangeComplete", handleRouteChange);
};
}, [router.events]);

return (
<>
<DefaultSeo {...SEO} />
Expand All @@ -52,6 +37,7 @@ export default function MyApp(props) {
</StyledThemeProvider>
</ThemeProvider>
</StyledEngineProvider>
<GoogleAnalytics gaId={gaId} />
</>
);
}
Expand Down
18 changes: 0 additions & 18 deletions apps/climatemappedafrica/src/pages/_document.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,6 @@ export default class MyDocument extends Document {
<link rel="manifest" href="/site.webmanifest" />
{/* PWA primary color */}
<meta name="theme-color" content={theme.palette.primary.main} />
{/* Global Site Tag (gtag.js) - Google Analytics */}
<script
async
src={`https://www.googletagmanager.com/gtag/js?id=${process.env.NEXT_PUBLIC_GOOGLE_ANALYTICS_ID}`}
/>
<script
// eslint-disable-next-line react/no-danger
dangerouslySetInnerHTML={{
__html: `
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '${process.env.NEXT_PUBLIC_GOOGLE_ANALYTICS_ID}', {
page_path: window.location.pathname,
});
`,
}}
/>
</Head>
<body>
<Main />
Expand Down
Loading
Loading