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

fix(ga integration): add ga scripts #965

Merged
merged 1 commit into from
Apr 20, 2022
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
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
AWS_BUCKET=sputnik-dao
AWS_REGION=eu-central-1
AWS_IDENTITY_POOL_ID=eu-central-1:5887fd21-7b40-4871-a6cb-21097fe079a2
GOOGLE_ANALYTICS_KEY=GTM-TRCM9H7
GOOGLE_ANALYTICS_KEY=G-2SWGMFRYJ8
RELEASE_NOTES=https://github.com/near-daos/astro-ui/releases/latest
I18_RELOAD_ON_PRERENDER=false
STATS_API_URL=https://mainnet.api.daostats.io
Expand Down
2 changes: 1 addition & 1 deletion config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const appConfig = {

AWS_BUCKET: '',
AWS_REGION: '',
GOOGLE_ANALYTICS_KEY: '',
GOOGLE_ANALYTICS_KEY: process.env.GOOGLE_ANALYTICS_KEY,
RELEASE_NOTES: process.env.RELEASE_NOTES,
I18_RELOAD_ON_PRERENDER: false,
TOASTS_NOTIFICATIONS_TIMEOUT: 0,
Expand Down
28 changes: 12 additions & 16 deletions constants/googleTagManager.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
const GOOGLE_ANALYTICS_KEY = process.env.GOOGLE_ANALYTICS_KEY;
export const gtag = (key: string): string => {
return `https://www.googletagmanager.com/gtag/js?id=${key}`;
};

export const GOOGLE_TAG_MANAGER = `
(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','${GOOGLE_ANALYTICS_KEY}');
`;

export const GOOGLE_TAG_MANAGER_NO_SCRIPT = `
<iframe
src="https://www.googletagmanager.com/ns.html?id=${GOOGLE_ANALYTICS_KEY}"
height="0"
width="0"
style="display:none;visibility:hidden"
></iframe>
export const gtagScript = (key: string): string => {
return `
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '${key}', {
page_path: window.location.pathname,
});
`;
};
46 changes: 29 additions & 17 deletions pages/_app.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import React, { FunctionComponent, useEffect } from 'react';
import Head from 'next/head';
import Script from 'next/script';
import { appWithTranslation } from 'next-i18next';
import nextI18NextConfig from 'next-i18next.config';
import type { AppContext, AppProps } from 'next/app';
import { withLDProvider } from 'launchdarkly-react-client-sdk';
import { appConfig as applicationConfig } from 'config';

import { configService } from 'services/ConfigService';
import { useRouter } from 'next/router';
import { ALL_FEED_URL, MY_FEED_URL } from 'constants/routing';

import { AuthWrapper } from 'context/AuthContext';
Expand All @@ -18,16 +20,17 @@ import { SearchResults } from 'features/search/search-results';
import { CookieService } from 'services/CookieService';

import { ACCOUNT_COOKIE, DAO_COOKIE, DEFAULT_OPTIONS } from 'constants/cookies';
import { gtag, gtagScript } from 'constants/googleTagManager';

import { SocketProvider } from 'context/SocketContext';

import { useIntercomAdjust } from 'hooks/useIntercomAdjust';

import 'styles/globals.scss';
import { useRouter } from 'next/router';

function App({ Component, pageProps }: AppProps): JSX.Element | null {
const router = useRouter();
const { appConfig } = configService.get();

useEffect(() => {
CookieService.set(DAO_COOKIE, router.query.dao, DEFAULT_OPTIONS);
Expand All @@ -36,21 +39,30 @@ function App({ Component, pageProps }: AppProps): JSX.Element | null {
useIntercomAdjust();

return (
<ModalProvider>
<AuthWrapper>
<SocketProvider>
<SearchResults>
<Head>
<title>Astro</title>
</Head>
<PageLayout>
<Component {...pageProps} />
</PageLayout>
<MobileNav />
</SearchResults>
</SocketProvider>
</AuthWrapper>
</ModalProvider>
<>
<Script
strategy="lazyOnload"
src={gtag(appConfig.GOOGLE_ANALYTICS_KEY)}
/>
<Script strategy="lazyOnload">
{gtagScript(appConfig.GOOGLE_ANALYTICS_KEY)}
</Script>
<ModalProvider>
<AuthWrapper>
<SocketProvider>
<SearchResults>
<Head>
<title>Astro</title>
</Head>
<PageLayout>
<Component {...pageProps} />
</PageLayout>
<MobileNav />
</SearchResults>
</SocketProvider>
</AuthWrapper>
</ModalProvider>
</>
);
}

Expand Down
14 changes: 0 additions & 14 deletions pages/_document.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
/* eslint-disable react/no-danger */
import Document, { Html, Head, Main, NextScript } from 'next/document';

import {
GOOGLE_TAG_MANAGER,
GOOGLE_TAG_MANAGER_NO_SCRIPT,
} from 'constants/googleTagManager';
import { APP_CONFIG } from 'config/fetchConfig';

export default class MyDocument extends Document {
Expand All @@ -17,18 +13,8 @@ export default class MyDocument extends Document {
__html: APP_CONFIG,
}}
/>
<script
dangerouslySetInnerHTML={{
__html: GOOGLE_TAG_MANAGER,
}}
/>
</Head>
<body>
<noscript
dangerouslySetInnerHTML={{
__html: GOOGLE_TAG_MANAGER_NO_SCRIPT,
}}
/>
<Main />
<NextScript />
</body>
Expand Down