vercel analytics script on both pages; disable project SSO protection (was 404-ing visitors)
This commit is contained in:
1 parent
cc6a660729
commit
632d053de9
68 files changed
+5662
No files matched your search
+7
@@ -0,0 +1,7 @@
|
||||
export type {
|
||||
AnalyticsProps,
|
||||
BeforeSend,
|
||||
BeforeSendEvent,
|
||||
// @ts-expect-error this filed is copied to dist, so it's ok to reference the generated index.d.ts
|
||||
} from '../index.d.ts';
|
||||
export { default } from './index.astro';
|
||||
+69
@@ -0,0 +1,69 @@
|
||||
---
|
||||
// Since this file will not be bundled by Tsup, it is referencing bundled files relative to dist/astro/
|
||||
import type { AnalyticsProps } from '../index.d.ts';
|
||||
|
||||
type Props = Omit<AnalyticsProps, 'framework' | 'beforeSend'>;
|
||||
|
||||
const propsStr = JSON.stringify(Astro.props);
|
||||
const paramsStr = JSON.stringify(Astro.params);
|
||||
---
|
||||
|
||||
<vercel-analytics
|
||||
data-props={propsStr}
|
||||
data-params={paramsStr}
|
||||
data-pathname={Astro.url.pathname}></vercel-analytics>
|
||||
|
||||
<script>
|
||||
import { inject, pageview, computeRoute } from '../index.mjs';
|
||||
|
||||
// !! important !!
|
||||
// do not access env variables using import.meta.env[varname]
|
||||
// some bundles won't replace the value at build time.
|
||||
|
||||
function getBasePath(): string | undefined {
|
||||
try {
|
||||
return import.meta.env.PUBLIC_VERCEL_OBSERVABILITY_BASEPATH as
|
||||
| string
|
||||
| undefined;
|
||||
} catch {
|
||||
// do nothing
|
||||
}
|
||||
}
|
||||
|
||||
function getConfigString(): string | undefined {
|
||||
// !! important !!
|
||||
// do not access env variables using import.meta.env[varname]
|
||||
// some bundles won't replace the value at build time.
|
||||
try {
|
||||
return import.meta.env.PUBLIC_VERCEL_OBSERVABILITY_CLIENT_CONFIG as
|
||||
| string
|
||||
| undefined;
|
||||
} catch {
|
||||
// do nothing
|
||||
}
|
||||
}
|
||||
|
||||
customElements.define(
|
||||
'vercel-analytics',
|
||||
class VercelAnalytics extends HTMLElement {
|
||||
constructor() {
|
||||
super();
|
||||
try {
|
||||
const props = JSON.parse(this.dataset.props ?? '{}');
|
||||
const params = JSON.parse(this.dataset.params ?? '{}');
|
||||
inject({
|
||||
...props,
|
||||
disableAutoTrack: true,
|
||||
framework: 'astro',
|
||||
basePath: getBasePath(),
|
||||
beforeSend: window.webAnalyticsBeforeSend,
|
||||
}, getConfigString());
|
||||
const path = this.dataset.pathname;
|
||||
pageview({ route: computeRoute(path ?? '', params), path });
|
||||
} catch (err) {
|
||||
throw new Error(`Failed to parse WebAnalytics properties: ${err}`);
|
||||
}
|
||||
}
|
||||
},
|
||||
);
|
||||
</script>
|
||||
Reference in new issue
Block a user