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

chore(web): add google analytics to web dev site #1878

Merged
merged 1 commit into from
Mar 23, 2023
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 web/flat-web/index.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<!DOCTYPE html>
<html lang="en">
<head>
<head><!-- FLAG_GTAG -->
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
Expand Down
39 changes: 39 additions & 0 deletions web/flat-web/scripts/vite-plugin-html-gtag.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import type { Plugin } from "vite";

export function injectGtag(): Plugin {
let isProduction = false;
return {
name: "flat:gtag",
enforce: "pre",
configResolved(config) {
isProduction = config.isProduction;
},
transformIndexHtml: {
enforce: "pre",
transform(originalHTML) {
const html = originalHTML.replace("<!-- FLAG_GTAG -->", gtag(isProduction));

return {
html,
tags: [],
};
},
},
};
}

const FLAT_WEB_DEV = `
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-PYZ1K39HMD"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());

gtag('config', 'G-PYZ1K39HMD');
</script>
`;

const gtag = (isProduction: boolean): string => {
return isProduction ? "" : FLAT_WEB_DEV;
};
2 changes: 2 additions & 0 deletions web/flat-web/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { inlineAssets } from "./scripts/vite-plugin-inline-assets";
import { mainPackageJSONPath } from "../../scripts/constants";
import { autoChooseConfig } from "../../scripts/utils/auto-choose-config";
import viteCompression from "vite-plugin-compression";
import { injectGtag } from "./scripts/vite-plugin-html-gtag";

// HACK: disable dedupe in the react plugin
// We need to do this because Flat is not a typical react project,
Expand All @@ -28,6 +29,7 @@ export default defineConfig({
plugins: [
reactPlugin,
dotenv(autoChooseConfig()),
injectGtag(),
injectHtmlHash(),
version(mainPackageJSONPath),
inlineAssets(),
Expand Down