Skip to content

Commit 3bdf77d

Browse files
committed
feat: updated to latest plausible tracker
1 parent c7bcb29 commit 3bdf77d

6 files changed

Lines changed: 76 additions & 159 deletions

File tree

README.md

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,19 @@
55

66
Plausible provider for your [SolidJS](https://solidjs.com) app.
77

8-
All credit should go to [barbapapazes](https://github.com/barbapapazes) for
9-
updating the
8+
All credit should go to [barbapapazes](https://github.com/barbapapazes) for updating the
109
[plausible-tracker](https://github.com/barbapapazes/plausible-tracker)
1110

1211
## Usage
1312

14-
Add the `PlausibleProvider` to your app component:
13+
Add the `Plausible.Init` to your app component:
1514

1615
```tsx
1716
export default function App() {
18-
return (
19-
<Plausible.Provider value={{/* ... */}}>
20-
<Plausible.AutoFileDownloadsTracking />
21-
<Plausible.AutoOutboundTracking />
22-
<Plausible.AutoPageviewTracking />
23-
</Plausible.Provider>
24-
);
17+
return <Plausible.Init />;
2518
}
2619
```
2720

2821
## License
2922

30-
Licensed under the [MPL-2.0](LICENSE) license.<br/> Copyright &copy; 2024,
31-
Bastiaan Stroosnijder
23+
Licensed under the [MPL-2.0](LICENSE) license.<br/> Copyright &copy; 2024, Bastiaan Stroosnijder

deno.json

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,29 @@
11
{
22
"name": "@strootje/solid-plausible",
3-
"version": "0.2.12",
3+
"version": "0.3.0",
44
"license": "MPL-2.0",
55
"exports": "./mod.ts",
6+
7+
"compilerOptions": {
8+
"jsx": "react-jsx",
9+
"jsxImportSource": "solid-js"
10+
},
11+
612
"imports": {
7-
"@barbapapazes/plausible-tracker": "npm:@barbapapazes/plausible-tracker@^0.5.6",
8-
"@std/expect": "jsr:@std/expect@^1.0.16",
9-
"solid-js": "npm:solid-js@^1.9.7"
13+
"#/": "./",
14+
"@plausible-analytics/tracker": "npm:@plausible-analytics/tracker@^0.4.4",
15+
"@std/expect": "jsr:@std/expect@^1.0.18",
16+
"solid-js": "npm:solid-js@^1.9.11"
17+
},
18+
19+
"test": {
20+
"include": [
21+
"**/*.test.ts",
22+
"**/*.test.tsx"
23+
]
1024
},
25+
1126
"fmt": {
1227
"lineWidth": 120
13-
},
14-
"compilerOptions": {
15-
"jsx": "react-jsx",
16-
"jsxImportSource": "solid-js"
1728
}
1829
}

deno.lock

Lines changed: 34 additions & 27 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mod.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { Plausible } from "#/mod.ts";
2+
import { expect } from "@std/expect";
3+
4+
Deno.test(function stuff_should_not_be_null() {
5+
expect(Plausible.Init).not.toBeNull();
6+
expect(Plausible.Track).not.toBeNull();
7+
});

mod.test.tsx

Lines changed: 0 additions & 9 deletions
This file was deleted.

mod.ts

Lines changed: 12 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -1,111 +1,20 @@
1-
import {
2-
createPlausibleTracker,
3-
type EventName,
4-
type EventOptions,
5-
type Plausible as PlausibleType,
6-
type PlausibleOptions,
7-
} from "@barbapapazes/plausible-tracker";
8-
import {
9-
defaultFileTypes,
10-
useAutoFileDownloadsTracking as _useAutoFileDownloadsTracking,
11-
useAutoOutboundTracking as _useAutoOutboundTracking,
12-
useAutoPageviews as _useAutoPageviews,
13-
} from "@barbapapazes/plausible-tracker/extensions";
14-
import { type Component, type Context, type ContextProviderComponent, createContext, onCleanup, useContext } from "solid-js";
1+
import { init, type PlausibleConfig, type PlausibleEventOptions, track } from "@plausible-analytics/tracker";
2+
import { onMount } from "solid-js";
153
import { isServer } from "solid-js/web";
164

17-
const PlausibleContext: Context<Partial<PlausibleOptions> | undefined> = createContext<
18-
Partial<PlausibleOptions>
19-
>();
20-
21-
let plausible: PlausibleType;
22-
const usePlausible = () => plausible ??= createPlausibleTracker(useContext(PlausibleContext));
23-
24-
export const useTrackEvent = (
25-
name: EventName,
26-
opts?: Partial<EventOptions>,
27-
) => {
28-
if (isServer) return;
29-
usePlausible().trackEvent(name, opts);
30-
};
31-
32-
export const useTrackPageview = (opts?: Partial<EventOptions>) => {
33-
if (isServer) return;
34-
usePlausible().trackPageview(opts);
35-
};
36-
37-
export const useAutoFileDownloadsTracking = (
38-
opts?: Partial<EventOptions>,
39-
fileTypes: string[] = defaultFileTypes,
40-
) => {
41-
if (isServer) return;
42-
const { cleanup, install } = _useAutoFileDownloadsTracking(usePlausible(), {
43-
fileTypes,
44-
}, opts);
45-
46-
try {
47-
install();
48-
} finally {
49-
onCleanup(cleanup);
50-
}
51-
};
52-
53-
export const useAutoOutboundTracking = (opts?: EventOptions) => {
54-
if (isServer) return;
55-
const { cleanup, install } = _useAutoOutboundTracking(usePlausible(), opts);
56-
57-
try {
58-
install();
59-
} finally {
60-
onCleanup(cleanup);
61-
}
62-
};
63-
64-
export const useAutoPageviewTracking = (opts?: EventOptions) => {
65-
if (isServer) return;
66-
const { cleanup, install } = _useAutoPageviews(usePlausible(), opts);
67-
68-
try {
69-
install();
70-
} finally {
71-
onCleanup(cleanup);
72-
}
5+
type PlausibleTrackProps = {
6+
eventName: string;
7+
opts?: PlausibleEventOptions;
738
};
749

75-
type PlausibleStruct = {
76-
Provider: ContextProviderComponent<Partial<PlausibleOptions>>;
77-
TrackEvent: Component<{ name: EventName } & Partial<EventOptions>>;
78-
TrackPageview: Component<Partial<EventOptions>>;
79-
AutoFileDownloadsTracking: Component<
80-
{ fileTypes: string[] } & Partial<EventOptions>
81-
>;
82-
AutoOutboundTracking: Component<Partial<EventOptions>>;
83-
AutoPageviewTracking: Component<Partial<EventOptions>>;
84-
};
85-
export const Plausible: PlausibleStruct = {
86-
Provider: PlausibleContext.Provider,
87-
TrackEvent: (
88-
{ name, ...opts }: { name: EventName } & Partial<EventOptions>,
89-
) => {
90-
useTrackEvent(name, opts);
10+
export const Plausible = {
11+
Init: (props: PlausibleConfig) => {
12+
onMount(() => !isServer && init(props));
9113
return null;
9214
},
93-
TrackPageview: (opts?: Partial<EventOptions>) => {
94-
useTrackPageview(opts);
95-
return null;
96-
},
97-
AutoFileDownloadsTracking: (
98-
{ fileTypes, ...opts }: { fileTypes: string[] } & Partial<EventOptions>,
99-
) => {
100-
useAutoFileDownloadsTracking(opts, fileTypes);
101-
return null;
102-
},
103-
AutoOutboundTracking: (opts?: Partial<EventOptions>) => {
104-
useAutoOutboundTracking(opts);
105-
return null;
106-
},
107-
AutoPageviewTracking: (opts?: Partial<EventOptions>) => {
108-
useAutoPageviewTracking(opts);
15+
16+
Track: (props: PlausibleTrackProps) => {
17+
onMount(() => !isServer && track(props.eventName, props.opts ?? {}));
10918
return null;
11019
},
111-
};
20+
} as const;

0 commit comments

Comments
 (0)