Skip to content

Commit

Permalink
📈 Replace gtag.js with Google tag manager
Browse files Browse the repository at this point in the history
  • Loading branch information
SogoKato committed Feb 29, 2024
1 parent 292ccc7 commit 418a232
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 71 deletions.
10 changes: 2 additions & 8 deletions components/Like.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React, { useEffect, useState } from "react";
import { LikeEvent } from "../types/ga";
import { trackEvent } from "../utils/gtag";
import { sendGTMEvent } from '@next/third-parties/google'

type LikeProps = {
path: string;
Expand Down Expand Up @@ -187,12 +186,7 @@ const getRecords = (): Records => {
};

const trackLikeEvent = (label: string) => {
const event: LikeEvent = {
action: "like",
category: "button",
label: label,
};
trackEvent(event);
sendGTMEvent({ event: 'like', value: label })
};

interface IAPIReaction {
Expand Down
16 changes: 0 additions & 16 deletions hooks/usePageView.ts

This file was deleted.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"clean": "tsc --build --clean"
},
"dependencies": {
"@next/third-parties": "^14.1.0",
"@noroch/like-widget": "^1.0.2",
"googleapis": "^110.0.0",
"gray-matter": "^4.0.3",
Expand Down
5 changes: 3 additions & 2 deletions pages/_app.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import Head from "next/head";
import type { AppProps } from "next/app";
import { GoogleTagManager } from '@next/third-parties/google'
import Layout from "../components/Layout";
import "../styles/globals.css";
import usePageView from "../hooks/usePageView";
import { googleTagManagerId } from "../utils/const";

function MyApp({ Component, pageProps }: AppProps) {
usePageView();
return (
<Layout {...pageProps}>
<Head>
<link rel="icon" href="/favicon.ico" />
</Head>
<Component {...pageProps} />
<GoogleTagManager gtmId={googleTagManagerId} />
</Layout>
);
}
Expand Down
22 changes: 1 addition & 21 deletions pages/_document.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Html, Head, Main, NextScript } from "next/document";
import Script from "next/script";
import { gaId, siteTitle } from "../utils/const";
import { siteTitle } from "../utils/const";

export default function Document() {
return (
Expand All @@ -17,25 +16,6 @@ export default function Document() {
type="application/rss+xml"
title={siteTitle}
/>
<Script
async
src={`https://www.googletagmanager.com/gtag/js?id=${gaId}`}
strategy="afterInteractive"
/>
<Script
id="_gtag"
defer
dangerouslySetInnerHTML={{
__html: `
if (window.location.hostname === "sogo.dev") {
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag("js", new Date());
gtag("config", "${gaId}");
}`,
}}
strategy="afterInteractive"
/>
</Head>
<body>
<Main />
Expand Down
18 changes: 18 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 0 additions & 7 deletions types/ga.ts

This file was deleted.

2 changes: 1 addition & 1 deletion utils/const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ export const baseUrl = "https://sogo.dev";
export const basePostDir = "posts";
export const postsPerPage = 10;

export const gaId = "G-RE7Q7WBFDJ";
export const googleTagManagerId = "GTM-M965SWP";
16 changes: 0 additions & 16 deletions utils/gtag.ts

This file was deleted.

0 comments on commit 418a232

Please sign in to comment.