-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Integrate Microsoft Clarity and Google Analytics [GA4]
This commit integrates Microsoft Clarity and Google Analytics into our application. - Added Microsoft Clarity and Google Analytics initialization in `utils/msclarity.client.ts` and `utils/gtags.client.ts` respectively. - Utilized the tracking IDs in `root.tsx`. - Updated Privacy Policy and Terms of Service to include disclosures for Microsoft Clarity and Google Analytics. - Added Microsoft Clarity ID and Google Analytics Tracking ID to the .env.example file. - Updated Privacy Policy and Terms of Service to include disclosures for Microsoft Clarity and Google Analytics on main imperfectgamers.org site
- Loading branch information
Showing
5 changed files
with
193 additions
and
6 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
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 @@ | ||
/** | ||
* | ||
* CREDITS TO https://github.com/remix-run/examples/blob/main/google-analytics/app/utils/gtags.client.ts | ||
* | ||
*/ | ||
declare global { | ||
interface Window { | ||
gtag: ( | ||
option: string, | ||
gaTrackingId: string, | ||
options: Record<string, unknown>, | ||
) => void; | ||
} | ||
} | ||
|
||
/** | ||
* @example | ||
* https://developers.google.com/analytics/devguides/collection/gtagjs/pages | ||
*/ | ||
export const pageview = (url: string, trackingId: string) => { | ||
if (!window.gtag) { | ||
console.warn( | ||
"window.gtag is not defined. This could mean your google analytics script has not loaded on the page yet.", | ||
); | ||
return; | ||
} | ||
window.gtag("config", trackingId, { | ||
page_path: url, | ||
}); | ||
}; | ||
|
||
/** | ||
* @example | ||
* https://developers.google.com/analytics/devguides/collection/gtagjs/events | ||
*/ | ||
export const event = ({ | ||
action, | ||
category, | ||
label, | ||
value, | ||
}: Record<string, string>) => { | ||
if (!window.gtag) { | ||
console.warn( | ||
"window.gtag is not defined. This could mean your google analytics script has not loaded on the page yet.", | ||
); | ||
return; | ||
} | ||
window.gtag("event", action, { | ||
event_category: category, | ||
event_label: label, | ||
value: value, | ||
}); | ||
}; |
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,83 @@ | ||
import type { HtmlTagDescriptor, Plugin, ResolvedConfig } from "vite"; | ||
|
||
export interface MsClarityObjectOption { | ||
/** | ||
* The ID of the project Clarity provides to you. | ||
* | ||
* Can be found in the URL of your project. | ||
* | ||
* @example `k4vhy94oj3` | ||
*/ | ||
id?: string; | ||
|
||
/** | ||
* The code to inject in the HTML. | ||
* | ||
* If provided, the `id` option will be ignored. | ||
* | ||
* If not provided, the script provided by Clarity will be used, with the `id` provided. | ||
*/ | ||
script?: string; | ||
|
||
/** | ||
* Whether to inject the script in development mode. | ||
* | ||
* @default false | ||
*/ | ||
enableInDevMode?: boolean; | ||
|
||
/** | ||
* Where to inject the script. | ||
* | ||
* @default "head-prepend" | ||
*/ | ||
injectTo?: HtmlTagDescriptor["injectTo"]; | ||
} | ||
|
||
export default function MsClarity( | ||
idOrOptions: string | MsClarityObjectOption | ||
): Plugin { | ||
let config: ResolvedConfig; | ||
return { | ||
name: "ms-clarity", | ||
enforce: "pre", | ||
configResolved(resolvedConfig) { | ||
config = resolvedConfig; | ||
}, | ||
transformIndexHtml() { | ||
const options: MsClarityObjectOption = | ||
typeof idOrOptions === "string" ? { id: idOrOptions } : idOrOptions; | ||
|
||
if (config.command === "serve" && !options.enableInDevMode) { | ||
return; | ||
} | ||
|
||
if (!options.script && !options.id) { | ||
throw new Error( | ||
"[ms-clarity] No id provided. Please provide a id or a code to inject." | ||
); | ||
} | ||
|
||
const script = options.script ?? toClarityScript(options.id!); | ||
|
||
return [ | ||
{ | ||
tag: "script", | ||
attrs: { | ||
type: "text/javascript", | ||
}, | ||
children: script, | ||
injectTo: options.injectTo, | ||
}, | ||
]; | ||
}, | ||
}; | ||
} | ||
|
||
function toClarityScript(id: string) { | ||
return `(function(c,l,a,r,i,t,y){ | ||
c[a]=c[a]||function(){(c[a].q=c[a].q||[]).push(arguments)}; | ||
t=l.createElement(r);t.async=1;t.src="https://www.clarity.ms/tag/"+i; | ||
y=l.getElementsByTagName(r)[0];y.parentNode.insertBefore(t,y); | ||
})(window, document, "clarity", "script", "${id}");`; | ||
} |