summaryrefslogtreecommitdiff
path: root/packages/integrations/vercel/src/lib/web-analytics.ts
diff options
context:
space:
mode:
authorGravatar Chris <7249920+chriswdmr@users.noreply.github.com> 2023-09-14 14:02:11 +0200
committerGravatar GitHub <noreply@github.com> 2023-09-14 08:02:11 -0400
commit2e8726feec2e0d6ba8bd4db941009986e8e34141 (patch)
treede59fa68c9e8b00b1364bc82024e57f95d955b94 /packages/integrations/vercel/src/lib/web-analytics.ts
parent7522bb4914f2f9e8b8f3c743bc9c941fd3aca644 (diff)
downloadastro-2e8726feec2e0d6ba8bd4db941009986e8e34141.tar.gz
astro-2e8726feec2e0d6ba8bd4db941009986e8e34141.tar.zst
astro-2e8726feec2e0d6ba8bd4db941009986e8e34141.zip
[@astrojs/vercel] Individually enable Speed Insights and Web Analytics (#8021)
* Individually enable Speed Insights and Web Analytics * Update pnpm-lock.yaml * Remove .only on tests * Fix build * Move `beforeSend` out of config * Address feedback from review * Update README.md * Add back the `analytics` property and add deprecation warning when used * Add migration guide for the deprecated `analytics` property * Update packages/integrations/vercel/README.md Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca> * Update README.md * Fix external dependency issue * Simplify plugin and reduce scope * Update .changeset/sixty-teachers-tap.md Co-authored-by: Emanuele Stoppa <my.burning@gmail.com> * Apply feedback from review * Move exposeEnv to speed-insights since it's only used there --------- Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca> Co-authored-by: Matthew Phillips <matthew@skypack.dev> Co-authored-by: Emanuele Stoppa <my.burning@gmail.com>
Diffstat (limited to 'packages/integrations/vercel/src/lib/web-analytics.ts')
-rw-r--r--packages/integrations/vercel/src/lib/web-analytics.ts30
1 files changed, 30 insertions, 0 deletions
diff --git a/packages/integrations/vercel/src/lib/web-analytics.ts b/packages/integrations/vercel/src/lib/web-analytics.ts
new file mode 100644
index 000000000..d6ee4d78d
--- /dev/null
+++ b/packages/integrations/vercel/src/lib/web-analytics.ts
@@ -0,0 +1,30 @@
+export type VercelWebAnalyticsConfig = {
+ enabled: boolean;
+};
+
+export async function getInjectableWebAnalyticsContent({
+ mode,
+}: {
+ mode: 'development' | 'production';
+}) {
+ const base = `window.va = window.va || function () { (window.vaq = window.vaq || []).push(arguments); };`;
+
+ if (mode === 'development') {
+ return `
+ ${base}
+ var script = document.createElement('script');
+ script.defer = true;
+ script.src = 'https://cdn.vercel-insights.com/v1/script.debug.js';
+ var head = document.querySelector('head');
+ head.appendChild(script);
+ `;
+ }
+
+ return `${base}
+ var script = document.createElement('script');
+ script.defer = true;
+ script.src = '/_vercel/insights/script.js';
+ var head = document.querySelector('head');
+ head.appendChild(script);
+ `;
+}