Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/yjl9903/unplugin-analytics

Universal Analytics Engines Integration
https://github.com/yjl9903/unplugin-analytics

analytics astro astro-integration google-analytics plausible umami unplugin vite vite-plugin webpack

Last synced: about 1 month ago
JSON representation

Universal Analytics Engines Integration

Awesome Lists containing this project

README

        

# unplugin-analytics

[![version](https://img.shields.io/npm/v/unplugin-analytics?label=unplugin-analytics)](https://www.npmjs.com/package/unplugin-analytics)
[![GitHub License](https://img.shields.io/github/license/yjl9903/unplugin-analytics)](https://github.com/yjl9903/unplugin-analytics/blob/main/LICENSE)
[![CI](https://github.com/yjl9903/unplugin-analytics/actions/workflows/ci.yml/badge.svg)](https://github.com/yjl9903/unplugin-analytics/actions/workflows/ci.yml)

> 👷‍♂️ Still work in progress.
>
> 🤩 PR Welcome! Hope more bundler / framework / analytics engines to be supported.

Universal Analytics Engines Integration.

Support analytics engines:

- [Umami](https://umami.is/)
- [Plausible](https://plausible.io/)
- [Cloudflare Web Analytics](https://www.cloudflare.com/web-analytics/)
- [Microsoft Clarity](https://clarity.microsoft.com/)

## Installation

```bash
npm i -D unplugin-analytics
```

Vite

```ts
// vite.config.ts

import Analytics from 'unplugin-analytics/vite';

export default defineConfig({
plugins: [
Analytics({
analytics: {
cloudflare: {
beacon: '...'
},
// Your unplugin-analytics options ...
}
})
]
});
```

Full example is located at [examples/vite](https://github.com/yjl9903/unplugin-analytics/blob/main/examples/vite).


VitePress

```ts
// .vitepress/config.ts

import { defineConfig } from 'vitepress';

import { injectScriptTags } from 'unplugin-analytics/vitepress';

export default defineConfig({
async transformHead(context) {
// Add the following code
injectScriptTags({
cloudflare: {
beacon: '...'
},
// Your unplugin-analytics options ...
})(context);
},
});
```


Astro

```ts
// astro.config.mjs

import Analytics from 'unplugin-analytics/astro';

export default defineConfig({
integrations: [
Analytics({
analytics: {
cloudflare: {
beacon: '...'
},
// Your unplugin-analytics options ...
}
})
],
});
```

Then add the astro component made of injected scripts to your layouts.

```astro
---
// src/layouts/Layout.astro

import Analytics from '~analytics/scripts.astro'

// ...
---












```

To make the TypeScript work, you can add `unplugin-analytics/client` to your corresponding `tsconfig.json`.

```json5
{
"compilerOptions": {
// ...
"types": [
"unplugin-analytics/client"
],
},
// ...
}
```

Or you can add TypeScript [triple-slash directives](https://www.typescriptlang.org/docs/handbook/triple-slash-directives.html) to your `.d.ts` (i.e. for projects initialized by Vite, it may be `src/env.d.ts`).

```ts
// Your .d.ts file

///
```

Full example is located at [examples/astro](https://github.com/yjl9903/unplugin-analytics/blob/main/examples/astro).


Nuxt

```ts
// nuxt.config.ts

export default defineNuxtConfig({
modules: ['unplugin-analytics/nuxt'],
analytics: {
cloudflare: {
beacon: '...'
},
// Your unplugin-analytics options ...
}
});
```

Full example is located at [examples/nuxt](https://github.com/yjl9903/unplugin-analytics/blob/main/examples/nuxt).


## Usage

Taking Vite as an example, you can config the analytics engines.

```ts
// vite.config.ts

import Analytics from 'unplugin-analytics/vite';

export default defineConfig({
plugins: [
Analytics({
analytics: {
umami: {
src: `...`,
id: `...`
},
plausible: {
domain: `...`
},
cloudflare: {
beacon: `...`
},
clarity: {
id: `...`
}
}
})
]
});
```

### Umami

**Provider key**: `umami`

**Parameters**:

- `src`: Your umami script url or the host
- `id`: Your umami website id

Generated script:

```html

```

You can use umami tracker function like:

```ts
import { umami } from '~analytics/umami';

document.querySelector('.umami')?.addEventListener('click', () => {
// Track events
umami?.track('click umami button');
});
```

### Plausible

**Provider key**: `plausible`

**Parameters**:

- `src`: Your plausible script url or the host
- `id`: Your website domain

Generated script:

```html

```

### Cloudflare Web Analytics

**Provider key**: `cloudflare`

**Parameters**:

- `beacon`: Your cloudflare web analytics beacon

Generated script:

```html

```

### Microsoft Clarity

**Provider key**: `clarity`

**Parameters**:

- `id`: Your clarity project id

Generated script:

```html
(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", "...");
```

## License

MIT License © 2024 [XLor](https://github.com/yjl9903)