@arthurreira/analytics 0.2.1 → 0.2.2

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.
package/dist/client.js CHANGED
@@ -1,11 +1,18 @@
1
- import { useRef, useEffect } from 'react';
1
+ "use client";
2
+ "use client";
3
+
4
+ // src/components/Analytics.tsx
5
+ import { useEffect as useEffect2, useRef as useRef2 } from "react";
6
+
7
+ // src/hooks/useAnalytics.ts
8
+ import { useEffect, useRef } from "react";
2
9
 
3
10
  // src/lib/api.ts
4
11
  var BASE_FIELDS = (sessionId, eventType, path) => ({
5
12
  session_id: sessionId,
6
13
  event_type: eventType,
7
14
  path,
8
- page_url: window.location.href
15
+ page_url: typeof window !== "undefined" ? window.location.href : ""
9
16
  });
10
17
  async function createSession(apiUrl, apiKey, visitorId) {
11
18
  const response = await fetch(`${apiUrl}/sessions`, {
@@ -32,9 +39,8 @@ async function sendEvent(apiUrl, apiKey, payload) {
32
39
  async function trackPageview(apiUrl, apiKey, sessionId, path) {
33
40
  await sendEvent(apiUrl, apiKey, {
34
41
  ...BASE_FIELDS(sessionId, "pageview", path),
35
- page_url: `${window.location.origin}${path}`,
36
- // build from path, not window.location.href
37
- referrer: document.referrer || null,
42
+ page_url: `${typeof window !== "undefined" ? window.location.origin : ""}${path}`,
43
+ referrer: typeof document !== "undefined" ? document.referrer || null : null,
38
44
  scroll_depth: 0
39
45
  });
40
46
  }
@@ -57,7 +63,7 @@ async function trackScroll(apiUrl, apiKey, sessionId, path, depth) {
57
63
  async function trackCopy(apiUrl, apiKey, sessionId, path) {
58
64
  await sendEvent(apiUrl, apiKey, {
59
65
  ...BASE_FIELDS(sessionId, "copy", path),
60
- copied_text: window.getSelection()?.toString().slice(0, 200) || null
66
+ copied_text: typeof window !== "undefined" ? window.getSelection()?.toString().slice(0, 200) || null : null
61
67
  });
62
68
  }
63
69
  async function trackSearch(apiUrl, apiKey, sessionId, path, query) {
@@ -131,14 +137,16 @@ function useAnalytics(apiUrl, apiKey) {
131
137
  // src/components/Analytics.tsx
132
138
  function Analytics({ apiKey, apiUrl }) {
133
139
  const { trackPageview: trackPageview2 } = useAnalytics(apiUrl, apiKey);
134
- const lastTracked = useRef(null);
140
+ const lastTracked = useRef2(null);
135
141
  const pathname = typeof window !== "undefined" ? window.location.pathname : "";
136
- useEffect(() => {
142
+ useEffect2(() => {
137
143
  if (lastTracked.current === pathname) return;
138
144
  lastTracked.current = pathname;
139
145
  trackPageview2(pathname);
140
146
  }, [pathname, trackPageview2]);
141
147
  return null;
142
148
  }
143
-
144
- export { Analytics, useAnalytics };
149
+ export {
150
+ Analytics,
151
+ useAnalytics
152
+ };
package/dist/index.js CHANGED
@@ -3,7 +3,7 @@ var BASE_FIELDS = (sessionId, eventType, path) => ({
3
3
  session_id: sessionId,
4
4
  event_type: eventType,
5
5
  path,
6
- page_url: window.location.href
6
+ page_url: typeof window !== "undefined" ? window.location.href : ""
7
7
  });
8
8
  async function createSession(apiUrl, apiKey, visitorId) {
9
9
  const response = await fetch(`${apiUrl}/sessions`, {
@@ -30,9 +30,8 @@ async function sendEvent(apiUrl, apiKey, payload) {
30
30
  async function trackPageview(apiUrl, apiKey, sessionId, path) {
31
31
  await sendEvent(apiUrl, apiKey, {
32
32
  ...BASE_FIELDS(sessionId, "pageview", path),
33
- page_url: `${window.location.origin}${path}`,
34
- // build from path, not window.location.href
35
- referrer: document.referrer || null,
33
+ page_url: `${typeof window !== "undefined" ? window.location.origin : ""}${path}`,
34
+ referrer: typeof document !== "undefined" ? document.referrer || null : null,
36
35
  scroll_depth: 0
37
36
  });
38
37
  }
@@ -55,7 +54,7 @@ async function trackScroll(apiUrl, apiKey, sessionId, path, depth) {
55
54
  async function trackCopy(apiUrl, apiKey, sessionId, path) {
56
55
  await sendEvent(apiUrl, apiKey, {
57
56
  ...BASE_FIELDS(sessionId, "copy", path),
58
- copied_text: window.getSelection()?.toString().slice(0, 200) || null
57
+ copied_text: typeof window !== "undefined" ? window.getSelection()?.toString().slice(0, 200) || null : null
59
58
  });
60
59
  }
61
60
  async function trackSearch(apiUrl, apiKey, sessionId, path, query) {
@@ -78,5 +77,13 @@ async function trackCTA(apiUrl, apiKey, sessionId, path, ctaId, ctaVariant) {
78
77
  cta_variant: ctaVariant || null
79
78
  });
80
79
  }
81
-
82
- export { createSession, trackCTA, trackClick, trackCopy, trackError, trackPageview, trackScroll, trackSearch };
80
+ export {
81
+ createSession,
82
+ trackCTA,
83
+ trackClick,
84
+ trackCopy,
85
+ trackError,
86
+ trackPageview,
87
+ trackScroll,
88
+ trackSearch
89
+ };
package/package.json CHANGED
@@ -1,29 +1,35 @@
1
- {
2
- "name": "@arthurreira/analytics",
3
- "version": "0.2.1",
4
- "type": "module",
5
- "exports": {
6
- ".": "./dist/index.js",
7
- "./client": "./dist/client.js"
8
- },
9
- "types": "./dist/index.d.ts",
10
- "files": [
11
- "dist"
12
- ],
13
- "description": "Analytics SDK for af-analytics (browser client + helpers)",
14
- "license": "MIT",
15
- "dependencies": {},
16
- "devDependencies": {
17
- "tsup": "^8.5.1",
18
- "typescript": "^5.9.2",
19
- "@types/react": "^19.0.0"
20
- },
21
- "peerDependencies": {
22
- "next": "^14.0.0 || ^15.0.0 || ^16.0.0",
23
- "react": "^18.0.0 || ^19.0.0"
24
- },
25
- "scripts": {
26
- "build": "tsup",
27
- "dev": "tsup src/index.ts --format esm --dts --watch"
28
- }
1
+ { "name": "@arthurreira/analytics",
2
+ "version": "0.2.2",
3
+ "type": "module",
4
+ "scripts": {
5
+ "build": "tsup",
6
+ "prepare": "tsup",
7
+ "dev": "tsup src/index.ts --format esm --dts --watch"
8
+ },
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "import": "./dist/index.js"
13
+ },
14
+ "./client": {
15
+ "types": "./dist/client.d.ts",
16
+ "import": "./dist/client.js"
17
+ }
18
+ },
19
+ "sideEffects": false,
20
+ "files": [
21
+ "dist"
22
+ ],
23
+ "description": "Analytics SDK for af-analytics (browser client + helpers)",
24
+ "license": "MIT",
25
+ "dependencies": {},
26
+ "devDependencies": {
27
+ "tsup": "^8.5.1",
28
+ "typescript": "^5.9.2",
29
+ "@types/react": "^19.0.0"
30
+ },
31
+ "peerDependencies": {
32
+ "next": "^14.0.0 || ^15.0.0 || ^16.0.0",
33
+ "react": "^18.0.0 || ^19.0.0"
34
+ }
29
35
  }