@jamwidgets/astro 0.2.3 → 0.3.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jamwidgets/astro",
3
- "version": "0.2.3",
3
+ "version": "0.3.1",
4
4
  "description": "Astro components and content loader for Jamwidgets (forms, comments, reactions, posts)",
5
5
  "repository": {
6
6
  "type": "git",
@@ -26,6 +26,7 @@
26
26
  "./PoweredBy": "./src/PoweredBy.astro",
27
27
  "./Reactions": "./src/Reactions.astro",
28
28
  "./Views": "./src/Views.astro",
29
+ "./Analytics": "./src/Analytics.astro",
29
30
  "./Poll": "./src/Poll.astro",
30
31
  "./Subscribe": "./src/Subscribe.astro",
31
32
  "./SubscribeForm": "./src/SubscribeForm.astro",
@@ -55,7 +56,7 @@
55
56
  ],
56
57
  "license": "MIT",
57
58
  "dependencies": {
58
- "@jamwidgets/core": "^0.2.2"
59
+ "@jamwidgets/core": "^0.3.1"
59
60
  },
60
61
  "peerDependencies": {
61
62
  "astro": "^5.0.0 || ^6.0.0 || ^7.0.0"
@@ -0,0 +1,30 @@
1
+ ---
2
+ interface Props {
3
+ /** Falls back to the `jamwidgets-site-key` meta tag. */
4
+ siteKey?: string;
5
+ /** Defaults to `https://jamwidgets.com`. */
6
+ endpoint?: string;
7
+ }
8
+
9
+ const { siteKey = "", endpoint = "" } = Astro.props;
10
+ ---
11
+
12
+ <div
13
+ data-jamwidgets-analytics
14
+ data-site-key={siteKey}
15
+ data-endpoint={endpoint}
16
+ hidden
17
+ >
18
+ </div>
19
+
20
+ <script>
21
+ import { initAnalytics } from "@jamwidgets/core";
22
+
23
+ const el = document.querySelector("[data-jamwidgets-analytics]") as
24
+ | HTMLElement
25
+ | null;
26
+ initAnalytics({
27
+ siteKey: el?.dataset.siteKey || undefined,
28
+ endpoint: el?.dataset.endpoint || undefined,
29
+ });
30
+ </script>
@@ -92,6 +92,8 @@ import PoweredBy from "./PoweredBy.astro";
92
92
  </div>
93
93
 
94
94
  <script>
95
+ import { getAttributionHeaders, getVisitorId } from "@jamwidgets/core";
96
+
95
97
  interface Comment {
96
98
  id: string;
97
99
  authorName: string;
@@ -100,17 +102,6 @@ import PoweredBy from "./PoweredBy.astro";
100
102
  replies: Comment[];
101
103
  }
102
104
 
103
- // Get or create a visitor ID for anonymous users
104
- const VISITOR_KEY = "jamwidgets_visitor_id";
105
- function getVisitorId(): string {
106
- let id = localStorage.getItem(VISITOR_KEY);
107
- if (!id) {
108
- id = crypto.randomUUID();
109
- localStorage.setItem(VISITOR_KEY, id);
110
- }
111
- return id;
112
- }
113
-
114
105
  // Store form load timestamps for spam detection
115
106
  const formTimestamps = new WeakMap<Element, number>();
116
107
 
@@ -130,6 +121,7 @@ import PoweredBy from "./PoweredBy.astro";
130
121
 
131
122
  const visitorId = getVisitorId();
132
123
  const headers = {
124
+ ...getAttributionHeaders(pageId),
133
125
  "X-JamWidgets-Key": siteKey,
134
126
  "X-JamWidgets-Visitor": visitorId,
135
127
  };
package/src/Form.astro CHANGED
@@ -65,22 +65,13 @@ import PoweredBy from "./PoweredBy.astro";
65
65
  </form>
66
66
 
67
67
  <script>
68
+ import { getAttributionHeaders, getVisitorId } from "@jamwidgets/core";
69
+
68
70
  interface FormSubmitResponse {
69
71
  success: boolean;
70
72
  message: string;
71
73
  }
72
74
 
73
- // Get or create a visitor ID for anonymous users
74
- const VISITOR_KEY = "jamwidgets_visitor_id";
75
- function getVisitorId(): string {
76
- let id = localStorage.getItem(VISITOR_KEY);
77
- if (!id) {
78
- id = crypto.randomUUID();
79
- localStorage.setItem(VISITOR_KEY, id);
80
- }
81
- return id;
82
- }
83
-
84
75
  // Store form load timestamps for spam detection
85
76
  const formTimestamps = new WeakMap<Element, number>();
86
77
 
@@ -134,6 +125,7 @@ import PoweredBy from "./PoweredBy.astro";
134
125
  const response = await fetch(`${endpoint}/forms/${formSlug}/submit`, {
135
126
  method: "POST",
136
127
  headers: {
128
+ ...getAttributionHeaders(),
137
129
  "Content-Type": "application/json",
138
130
  "X-JamWidgets-Key": siteKey,
139
131
  "X-JamWidgets-Visitor": getVisitorId(),
@@ -87,16 +87,7 @@ const baseUrl = endpoint.replace(/\/+$/, "") + API_PATH;
87
87
  </div>
88
88
 
89
89
  <script>
90
- // Get or create a visitor ID for anonymous users
91
- const VISITOR_KEY = "jamwidgets_visitor_id";
92
- function getVisitorId(): string {
93
- let id = localStorage.getItem(VISITOR_KEY);
94
- if (!id) {
95
- id = crypto.randomUUID();
96
- localStorage.setItem(VISITOR_KEY, id);
97
- }
98
- return id;
99
- }
90
+ import { getAttributionHeaders, getVisitorId } from "@jamwidgets/core";
100
91
 
101
92
  document.querySelectorAll("[data-jamwidgets-reactions]").forEach((container) => {
102
93
  const endpoint = (container as HTMLElement).dataset.endpoint;
@@ -107,6 +98,7 @@ const baseUrl = endpoint.replace(/\/+$/, "") + API_PATH;
107
98
 
108
99
  const visitorId = getVisitorId();
109
100
  const headers = {
101
+ ...getAttributionHeaders(pageId),
110
102
  "X-JamWidgets-Key": siteKey,
111
103
  "X-JamWidgets-Visitor": visitorId,
112
104
  };
@@ -71,22 +71,13 @@ const baseUrl = endpoint.replace(/\/+$/, "") + API_PATH;
71
71
  </form>
72
72
 
73
73
  <script>
74
+ import { getAttributionHeaders, getVisitorId } from "@jamwidgets/core";
75
+
74
76
  interface SubscribeResponse {
75
77
  success: boolean;
76
78
  message: string;
77
79
  }
78
80
 
79
- // Get or create a visitor ID for anonymous users
80
- const VISITOR_KEY = "jamwidgets_visitor_id";
81
- function getVisitorId(): string {
82
- let id = localStorage.getItem(VISITOR_KEY);
83
- if (!id) {
84
- id = crypto.randomUUID();
85
- localStorage.setItem(VISITOR_KEY, id);
86
- }
87
- return id;
88
- }
89
-
90
81
  document.querySelectorAll("[data-jamwidgets-subscribe-form]").forEach((form) => {
91
82
  form.addEventListener("submit", async (e) => {
92
83
  e.preventDefault();
@@ -114,6 +105,7 @@ const baseUrl = endpoint.replace(/\/+$/, "") + API_PATH;
114
105
  const response = await fetch(`${endpoint}/subscribe`, {
115
106
  method: "POST",
116
107
  headers: {
108
+ ...getAttributionHeaders(),
117
109
  "Content-Type": "application/json",
118
110
  "X-JamWidgets-Key": siteKey,
119
111
  "X-JamWidgets-Visitor": getVisitorId(),