|
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"; |
15 | 3 | import { isServer } from "solid-js/web"; |
16 | 4 |
|
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; |
73 | 8 | }; |
74 | 9 |
|
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)); |
91 | 13 | return null; |
92 | 14 | }, |
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 ?? {})); |
109 | 18 | return null; |
110 | 19 | }, |
111 | | -}; |
| 20 | +} as const; |
0 commit comments