astro-microsoft-clarity-integration 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +42 -0
  2. package/index.ts +50 -0
  3. package/package.json +34 -0
package/README.md ADDED
@@ -0,0 +1,42 @@
1
+ # Astro Clarity
2
+
3
+ Astro Clarity is a lightweight wrapper that seamlessly integrates Clarity analytics with Astro, making it effortless to track user interactions and performance metrics on your Astro-powered websites.
4
+
5
+ ## Installation
6
+
7
+ ### npm
8
+ ```bash
9
+ npm install astro-microsoft-clarity-integration
10
+ ```
11
+
12
+ ### Yarn
13
+ ```bash
14
+ yarn add astro-microsoft-clarity-integration
15
+ ```
16
+
17
+ ### pnpm
18
+ ```bash
19
+ pnpm add astro-microsoft-clarity-integration
20
+ ```
21
+
22
+ ## Usage
23
+
24
+ ```js
25
+ // astro.config.mjs (or .ts if you're using TypeScript)
26
+ import { defineConfig } from 'astro/config';
27
+ import clarityIntegration from 'astro-microsoft-clarity-integration'; // Imported from an npm package
28
+
29
+ export default defineConfig({
30
+ integrations: [
31
+ clarityIntegration({
32
+ projectId: 'YOUR_TRACKING_ID', // Required: Replace with your Clarity project ID
33
+ enabled: true, // Optional: Enable the integration (defaults to true)
34
+ scriptStage: 'head-inline', // Optional: Set scriptStage to 'head-inline', 'body-inline'
35
+ debug: false, // Optional: Enable debug (set to true if you want to log script injections)
36
+ async: true, // Optional: Enable async loading
37
+ defer: true, // Optional: Enable defer for script loading
38
+ }),
39
+ ],
40
+ });
41
+
42
+ ```
package/index.ts ADDED
@@ -0,0 +1,50 @@
1
+ import type { AstroIntegration, InjectedScriptStage } from 'astro';
2
+
3
+ type ClarityOptions = {
4
+ projectId: string;
5
+ enabled?: boolean;
6
+ scriptStage?: InjectedScriptStage;
7
+ debug?: boolean;
8
+ async?: boolean;
9
+ defer?: boolean;
10
+ customAttrs?: Record<string, string>;
11
+ };
12
+
13
+ export default function clarityIntegration({
14
+ projectId,
15
+ enabled = true,
16
+ scriptStage = 'head-inline',
17
+ debug = false,
18
+ async = true,
19
+ defer = false,
20
+ customAttrs = {},
21
+ }: ClarityOptions): AstroIntegration {
22
+ if (!projectId) console.error('Clarity Integration requires a valid projectId');
23
+
24
+ return {
25
+ name: 'astro-clarity',
26
+ hooks: {
27
+ 'astro:config:setup': ({ injectScript }) => {
28
+ if (!enabled) return;
29
+
30
+ const scriptContent = `
31
+ (function(c, l, a, r, i, t, y) {
32
+ c[a] = c[a] || function() { (c[a].q = c[a].q || []).push(arguments) };
33
+ t = l.createElement(r);
34
+ t.src = "https://www.clarity.ms/tag/" + i;
35
+ t.async = ${async};
36
+ t.defer = ${defer};
37
+ ${debug ? `console.debug("Clarity script injected:", i);` : ''}
38
+ ${Object.entries(customAttrs)
39
+ .map(([key, value]) => `t.setAttribute("data-${key}", "${value}");`)
40
+ .join('\n')}
41
+ y = l.getElementsByTagName(r)[0];
42
+ y.parentNode.insertBefore(t, y);
43
+ })(window, document, "clarity", "script", "${projectId}");
44
+ `;
45
+
46
+ injectScript(scriptStage, scriptContent);
47
+ },
48
+ },
49
+ };
50
+ }
package/package.json ADDED
@@ -0,0 +1,34 @@
1
+ {
2
+ "name": "astro-microsoft-clarity-integration",
3
+ "description": "Microsoft Clarity analytics in Astro site",
4
+ "author": "Iurii Kalashnikov",
5
+ "version": "0.0.1",
6
+ "homepage": "https://github.com/YKalashnikov#readme",
7
+ "type": "module",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "git+https://github.com/YKalashnikov/astro-microsoft-clarity-integration"
11
+ },
12
+ "exports": {
13
+ ".": "./index.ts",
14
+ "./package.json": "./package.json"
15
+ },
16
+ "files": [
17
+ "index.ts"
18
+ ],
19
+ "keywords": [
20
+ "astro-integration",
21
+ "clarity",
22
+ "analytics",
23
+ "microsoft",
24
+ "astro",
25
+ "astro plugin"
26
+ ],
27
+ "scripts": {},
28
+ "devDependencies": {
29
+ "astro": "^5.5.5"
30
+ },
31
+ "peerDependencies": {
32
+ "astro": "^4.0.0 || ^5.0.0"
33
+ }
34
+ }