-
Notifications
You must be signed in to change notification settings - Fork 823
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(project): add google analytics to web and electron (#1882)
- Loading branch information
Showing
6 changed files
with
73 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
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: [], | ||
}; | ||
}, | ||
}, | ||
}; | ||
} | ||
|
||
// cspell:disable | ||
const FLAT_ELECTRON_DEV = ` | ||
<!-- Google tag (gtag.js) --> | ||
<script async src="https://www.googletagmanager.com/gtag/js?id=G-8G3P2T2DBF"></script> | ||
<script> | ||
window.dataLayer = window.dataLayer || []; | ||
function gtag(){dataLayer.push(arguments);} | ||
gtag('js', new Date()); | ||
gtag('config', 'G-8G3P2T2DBF'); | ||
</script> | ||
`; | ||
|
||
const FLAT_ELECTRON_PROD = ` | ||
<!-- Google tag (gtag.js) --> | ||
<script async src="https://www.googletagmanager.com/gtag/js?id=G-1VSFBNGGCC"></script> | ||
<script> | ||
window.dataLayer = window.dataLayer || []; | ||
function gtag(){dataLayer.push(arguments);} | ||
gtag('js', new Date()); | ||
gtag('config', 'G-1VSFBNGGCC'); | ||
</script> | ||
`; | ||
// cspell:enable | ||
|
||
const gtag = (isProduction: boolean): string => { | ||
return isProduction ? FLAT_ELECTRON_PROD : FLAT_ELECTRON_DEV; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters