@arthurreira/analytics 0.2.2 → 0.3.0

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 (2) hide show
  1. package/dist/client.js +38 -1
  2. package/package.json +1 -1
package/dist/client.js CHANGED
@@ -136,14 +136,51 @@ function useAnalytics(apiUrl, apiKey) {
136
136
 
137
137
  // src/components/Analytics.tsx
138
138
  function Analytics({ apiKey, apiUrl }) {
139
- const { trackPageview: trackPageview2 } = useAnalytics(apiUrl, apiKey);
139
+ const { trackPageview: trackPageview2, trackClick: trackClick2, trackScroll: trackScroll2, trackCopy: trackCopy2, trackError: trackError2 } = useAnalytics(apiUrl, apiKey);
140
140
  const lastTracked = useRef2(null);
141
+ const lastScrollDepth = useRef2(0);
141
142
  const pathname = typeof window !== "undefined" ? window.location.pathname : "";
142
143
  useEffect2(() => {
143
144
  if (lastTracked.current === pathname) return;
144
145
  lastTracked.current = pathname;
145
146
  trackPageview2(pathname);
146
147
  }, [pathname, trackPageview2]);
148
+ useEffect2(() => {
149
+ if (typeof window === "undefined") return;
150
+ const handler = (e) => trackClick2(e, e.target);
151
+ window.addEventListener("click", handler);
152
+ return () => window.removeEventListener("click", handler);
153
+ }, [trackClick2]);
154
+ useEffect2(() => {
155
+ if (typeof window === "undefined") return;
156
+ const handler = () => {
157
+ const scrolled = window.scrollY;
158
+ const total = document.documentElement.scrollHeight - window.innerHeight;
159
+ if (total <= 0) return;
160
+ const depth = Math.round(scrolled / total * 100);
161
+ const milestones = [25, 50, 75, 100];
162
+ for (const m of milestones) {
163
+ if (depth >= m && lastScrollDepth.current < m) {
164
+ lastScrollDepth.current = m;
165
+ trackScroll2(m);
166
+ }
167
+ }
168
+ };
169
+ window.addEventListener("scroll", handler, { passive: true });
170
+ return () => window.removeEventListener("scroll", handler);
171
+ }, [trackScroll2]);
172
+ useEffect2(() => {
173
+ if (typeof window === "undefined") return;
174
+ const handler = () => trackCopy2();
175
+ window.addEventListener("copy", handler);
176
+ return () => window.removeEventListener("copy", handler);
177
+ }, [trackCopy2]);
178
+ useEffect2(() => {
179
+ if (typeof window === "undefined") return;
180
+ const handler = (e) => trackError2(new Error(e.message));
181
+ window.addEventListener("error", handler);
182
+ return () => window.removeEventListener("error", handler);
183
+ }, [trackError2]);
147
184
  return null;
148
185
  }
149
186
  export {
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  { "name": "@arthurreira/analytics",
2
- "version": "0.2.2",
2
+ "version": "0.3.0",
3
3
  "type": "module",
4
4
  "scripts": {
5
5
  "build": "tsup",