From acbd4b34d5ed3d35a0bf231964cd19c8ba38268c Mon Sep 17 00:00:00 2001 From: Roman Koshkin Date: Wed, 17 Apr 2024 16:14:40 +0900 Subject: [PATCH] google tracking --- .gitignore | 1 + package.json | 3 ++- public/{analytics.js => analytics_proto.js} | 2 +- scripts/replaceAnalyticsId.js | 16 ++++++++++++++++ yarn.lock | 5 +++++ 5 files changed, 25 insertions(+), 2 deletions(-) rename public/{analytics.js => analytics_proto.js} (86%) create mode 100644 scripts/replaceAnalyticsId.js diff --git a/.gitignore b/.gitignore index ecae0c0..379a448 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ node_modules public/sitemap.xml public/feed.xml .vercel +public/analytics.js \ No newline at end of file diff --git a/package.json b/package.json index bae833d..a149a2c 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,7 @@ }, "scripts": { "dev": "next", - "build": "next build", + "build": "node scripts/replaceAnalyticsId.js && next build", "start": "next start", "lint": "next lint", "serve": "npm run build && npm run start", @@ -28,6 +28,7 @@ "@vercel/analytics": "^1.2.2", "clsx": "^2.1.0", "date-fns": "^2.22.1", + "dotenv": "^16.4.5", "framer-motion": "^11.0.12", "gray-matter": "^4.0.3", "ioredis": "^4.27.6", diff --git a/public/analytics.js b/public/analytics_proto.js similarity index 86% rename from public/analytics.js rename to public/analytics_proto.js index f8a93a1..dbb0ecb 100644 --- a/public/analytics.js +++ b/public/analytics_proto.js @@ -7,4 +7,4 @@ function gtag() { gtag('js', new Date()); // Replace 'YOUR_NEXT_PUBLIC_GTAG_ID' with your actual GTAG ID environment variable or hard code the ID -gtag('config', 'G-E08GE9HLQC'); +gtag('config', '%%GA_TRACKING_ID%%'); diff --git a/scripts/replaceAnalyticsId.js b/scripts/replaceAnalyticsId.js new file mode 100644 index 0000000..c8b7c16 --- /dev/null +++ b/scripts/replaceAnalyticsId.js @@ -0,0 +1,16 @@ +const fs = require('fs'); +const path = require('path'); +require('dotenv').config({ path: path.resolve(process.cwd(), '.env.local') }); + +const protoPath = path.join(__dirname, '../public/analytics_proto.js'); +const outputPath = path.join(__dirname, '../public/analytics.js'); + +// Copy prototype file to analytics.js +let content = fs.readFileSync(protoPath, 'utf8'); + +// Replace the placeholder with the actual Google Analytics Tracking ID from environment variables +const trackingId = process.env.NEXT_PUBLIC_GTAG_ID; +content = content.replace('%%GA_TRACKING_ID%%', trackingId); + +// Write the modified content to analytics.js +fs.writeFileSync(outputPath, content); diff --git a/yarn.lock b/yarn.lock index 4405538..05ea0f0 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1665,6 +1665,11 @@ dom-helpers@^5.0.1, dom-helpers@^5.2.0, dom-helpers@^5.2.1: "@babel/runtime" "^7.8.7" csstype "^3.0.2" +dotenv@^16.4.5: + version "16.4.5" + resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-16.4.5.tgz#cdd3b3b604cb327e286b4762e13502f717cb099f" + integrity sha512-ZmdL2rui+eB2YwhsWzjInR8LldtZHGDoQ1ugH85ppHKwpUHL7j7rN0Ti9NCnGiQbhaZ11FpR+7ao1dNsmduNUg== + electron-to-chromium@^1.4.668: version "1.4.731" resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.4.731.tgz#d3dc19f359045b750a1fb0bc42315a502d950187"