@saasquatch/squatch-js 2.3.1-2 → 2.3.1-6

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/demo/toolbar.tsx CHANGED
@@ -1,249 +1,450 @@
1
- import React, { Component, version } from "react";
2
- import { render } from "react-dom";
3
-
4
- import {
5
- popup,
6
- embed,
7
- embedNew,
8
- embedReferred,
9
- popupNew,
10
- popupReferred,
11
- script,
12
- toURL,
13
- users,
14
- href,
15
- } from "./sandbox";
16
- import { getVersions } from "./versions";
17
- import { delay } from "./util";
18
-
19
- const modes = ["POPUP", "EMBED"];
20
- const widgetTypes = [
21
- "REFERRER_WIDGET",
22
- "CONVERSION_WIDGET",
23
- "p/tuesday-test/w/referrerWidget",
24
- ];
25
- const staticVersions = ["HEAD", "latest", "alpha"];
26
-
27
- /**
28
- * Use the addUrlProps higher-order component to hook-in react-url-query.
29
- */
30
- class App extends Component {
31
- state = {
32
- versions: staticVersions,
33
- };
34
- async componentWillMount() {
35
- const apiVersions = await getVersions();
36
- const versions = [...staticVersions, ...apiVersions];
37
- this.setState({ versions });
38
- }
39
- render() {
40
- return (
41
- <div>
42
- <hr />
43
- <div>
44
- <ParamArea />
45
- <hr />
46
- <h2>Quick pick variables</h2>
47
- <details>
48
- <summary>Tenant / Program</summary>
49
- <a href={href(popup)}>Popup (classic)</a>
50
- <a href={href(embed)}>Embed (classic)</a>
51
- <a href={href(popupNew)}>Popup (new program)</a>
52
- <a href={href(embedNew)}>Embed (new program)</a>
53
- <a href={href(popupReferred)}>Popup (classic referred widget)</a>
54
- <a href={href(embedReferred)}>Embed (classic referred widget)</a>
55
- </details>
56
- <WidgetType />
57
- <ModeList />
58
- <UserList />
59
- <VersionList {...this.state} />
60
- </div>
61
- <hr />
62
-
63
- <button onClick={() => recordPurchase()}>Record Purchase</button>
64
- <hr />
65
-
66
- <button onClick={() => runEventBomb()}>Event Bomb</button>
67
- </div>
68
- );
69
- }
70
- }
71
-
72
- function ParamArea() {
73
- return (
74
- <div>
75
- <h2>Squatch.js Config</h2>
76
- <div>
77
- <textarea id="area1" rows={15} cols={80}>
78
- {JSON.stringify(window["sandbox"], null, 2)}
79
- </textarea>
80
- </div>
81
- <div>
82
- <button
83
- onClick={() => {
84
- let json: Sandbox = JSON.parse(
85
- // @ts-ignore
86
- document.getElementById("area1").value
87
- );
88
- toURL(json);
89
- }}
90
- >
91
- Reload Config
92
- </button>
93
- <button
94
- onClick={() => {
95
- navigator.clipboard.writeText(window.location.toString());
96
- }}
97
- >
98
- Share Current Config
99
- </button>
100
- </div>
101
- </div>
102
- );
103
- }
104
-
105
- async function recordPurchase() {
106
- //@ts-ignore
107
- const { squatch, sandbox } = window;
108
- const {
109
- jwt,
110
- user: { id, accountId },
111
- } = sandbox.initObj;
112
- const fields = {
113
- // Optional
114
- total: 10.0,
115
- revenue: 10.0,
116
- tax: 5.0,
117
- currency: "USD",
118
- };
119
-
120
- await squatch.events().track(
121
- {
122
- userId: id,
123
- accountId: accountId,
124
- events: [
125
- {
126
- key: "purchase",
127
- fields: fields, // Optional
128
- // id: "kjv12kbwktb13t3", // Optional id
129
- // dateTriggered: 1535136384753 // Optional date
130
- },
131
- ],
132
- },
133
- {
134
- jwt,
135
- }
136
- );
137
- // TODO: Eventually we'd like an API like this:
138
- // squatch.events().track("purchase", { ...fields });
139
- }
140
- async function runEventBomb() {
141
- while (true) {
142
- await recordPurchase();
143
- await delay(100);
144
- }
145
- }
146
- function WidgetType(props) {
147
- return (
148
- <details
149
- title={window["sandbox"].initObj.widgetType}
150
- key={0}
151
- id={`dropdown-basic-1`}
152
- >
153
- {widgetTypes.map((widgetType, i) => (
154
- <a
155
- key={i}
156
- href={href({
157
- ...window["sandbox"],
158
- initObj: {
159
- ...window["sandbox"].initObj,
160
- widgetType: widgetType,
161
- },
162
- })}
163
- >
164
- {widgetType}
165
- </a>
166
- ))}
167
- </details>
168
- );
169
- }
170
- function ModeList(props) {
171
- return (
172
- <details
173
- title={window["sandbox"].initObj.engagementMedium}
174
- key={0}
175
- id={`dropdown-basic-1`}
176
- >
177
- <summary>Engagement Medium</summary>
178
- {modes.map((mode, i) => (
179
- <a
180
- key={i}
181
- href={href({
182
- ...window["sandbox"],
183
- initObj: {
184
- ...window["sandbox"].initObj,
185
- engagementMedium: mode,
186
- },
187
- })}
188
- >
189
- {mode}
190
- </a>
191
- ))}
192
- </details>
193
- );
194
- }
195
- function UserList(props) {
196
- return (
197
- <details
198
- title={"User: " + window["sandbox"].initObj.user.firstName}
199
- key={0}
200
- id={`dropdown-basic-1`}
201
- >
202
- <summary>User</summary>
203
- {users.map((user, i) => (
204
- <a
205
- key={i}
206
- href={href({
207
- ...window["sandbox"],
208
- initObj: {
209
- ...window["sandbox"].initObj,
210
- user: user,
211
- },
212
- })}
213
- >
214
- {user["firstName"] || "Empty"}
215
- </a>
216
- ))}
217
- </details>
218
- );
219
- }
220
- function VersionList(props) {
221
- const { versions } = props;
222
- return (
223
- <details
224
- title={"Version: " + window["sandbox"].version || "Head"}
225
- key={0}
226
- id={`dropdown-basic-1`}
227
- >
228
- <summary>Version</summary>
229
- {versions.map((v, i) => (
230
- <a
231
- key={i}
232
- href={href({
233
- ...window["sandbox"],
234
- version: v,
235
- script:
236
- v.toLocaleLowerCase() == "head"
237
- ? script
238
- : `https://unpkg.com/@saasquatch/squatch-js@${v}`,
239
- })}
240
- >
241
- {v}
242
- </a>
243
- ))}
244
- </details>
245
- );
246
- }
247
- const root = document.getElementById("app");
248
- console.log("mount to", root);
249
- render(<App />, root);
1
+ import React, { Component, useState, version } from "react";
2
+ import { render } from "react-dom";
3
+ import squatch from "../dist/squatch";
4
+
5
+ import {
6
+ popup,
7
+ embed,
8
+ embedNew,
9
+ embedReferred,
10
+ popupNew,
11
+ popupReferred,
12
+ script,
13
+ toURL,
14
+ users,
15
+ href,
16
+ } from "./sandbox";
17
+ import { getVersions } from "./versions";
18
+ import { delay } from "./util";
19
+ import { widgets, worker } from "./generate";
20
+ import { rest } from "msw";
21
+
22
+ // 2. Define request handlers and response resolvers.
23
+
24
+ const modes = ["POPUP", "EMBED"];
25
+ const widgetTypes = [
26
+ "REFERRER_WIDGET",
27
+ "CONVERSION_WIDGET",
28
+ "p/tuesday-test/w/referrerWidget",
29
+ ];
30
+ const staticVersions = ["HEAD", "latest", "alpha", "next", "local"];
31
+
32
+ /**
33
+ * Use the addUrlProps higher-order component to hook-in react-url-query.
34
+ */
35
+ class App extends Component {
36
+ constructor(props) {
37
+ super(props);
38
+ worker.start({
39
+ findWorker: (scriptURL, _mockServiceWorkerUrl) =>
40
+ scriptURL.includes("mockServiceWorker"),
41
+ onUnhandledRequest(req) {
42
+ console.error(
43
+ "Found an unhandled %s request to %s",
44
+ req.method,
45
+ req.url.href
46
+ );
47
+ },
48
+ });
49
+ }
50
+ state = {
51
+ versions: staticVersions,
52
+ toolbarOpen: true,
53
+ };
54
+ async componentWillMount() {
55
+ const apiVersions = await getVersions();
56
+ const versions = [...staticVersions, ...apiVersions];
57
+ this.setState({ versions });
58
+ }
59
+ render() {
60
+ return (
61
+ <div>
62
+ <button
63
+ onClick={() =>
64
+ this.setState({ toolbarOpen: !this.state.toolbarOpen })
65
+ }
66
+ style={{ float: "right" }}
67
+ >
68
+ {this.state.toolbarOpen ? `<` : `>`}
69
+ </button>
70
+ <div style={{ display: this.state.toolbarOpen ? "block" : "none" }}>
71
+ <hr />
72
+ <div>
73
+ <ParamArea />
74
+ <hr />
75
+ <h2>Quick pick variables</h2>
76
+ <details>
77
+ <summary>Tenant / Program</summary>
78
+ <ul>
79
+ <li>
80
+ <a href={href(popup)}>Popup (classic)</a>
81
+ </li>
82
+ <li>
83
+ <a href={href(embed)}>Embed (classic)</a>
84
+ </li>
85
+ <li>
86
+ <a href={href(popupNew)}>Popup (new program)</a>
87
+ </li>
88
+ <li>
89
+ <a href={href(embedNew)}>Embed (new program)</a>
90
+ </li>
91
+ <li>
92
+ <a href={href(popupReferred)}>
93
+ Popup (classic referred widget)
94
+ </a>
95
+ </li>
96
+ <li>
97
+ <a href={href(embedReferred)}>
98
+ Embed (classic referred widget)
99
+ </a>
100
+ </li>
101
+ </ul>
102
+ </details>
103
+ <WidgetType />
104
+ <ModeList />
105
+ <UserList />
106
+ <VersionList {...this.state} />
107
+ <MockedWidgets />
108
+ <CustomMockedWidget />
109
+ </div>
110
+ <hr />
111
+
112
+ <button onClick={() => recordPurchase()}>Record Purchase</button>
113
+ <hr />
114
+
115
+ <button onClick={() => runEventBomb()}>Event Bomb</button>
116
+ </div>
117
+ </div>
118
+ );
119
+ }
120
+ }
121
+
122
+ function ParamArea() {
123
+ return (
124
+ <div>
125
+ <h2>Squatch.js Config</h2>
126
+ <div>
127
+ <textarea id="area1" rows={15} cols={70} style={{ maxWidth: "100%" }}>
128
+ {JSON.stringify(window["sandbox"], null, 2)}
129
+ </textarea>
130
+ </div>
131
+ <div>
132
+ <button
133
+ onClick={() => {
134
+ let json: Sandbox = JSON.parse(
135
+ // @ts-ignore
136
+ document.getElementById("area1").value
137
+ );
138
+ toURL(json);
139
+ }}
140
+ >
141
+ Reload Config
142
+ </button>
143
+ <button
144
+ onClick={() => {
145
+ navigator.clipboard.writeText(window.location.toString());
146
+ }}
147
+ >
148
+ Share Current Config
149
+ </button>
150
+ </div>
151
+ </div>
152
+ );
153
+ }
154
+
155
+ async function recordPurchase() {
156
+ //@ts-ignore
157
+ const { squatch, sandbox } = window;
158
+ const {
159
+ jwt,
160
+ user: { id, accountId },
161
+ } = sandbox.initObj;
162
+ const fields = {
163
+ // Optional
164
+ total: 10.0,
165
+ revenue: 10.0,
166
+ tax: 5.0,
167
+ currency: "USD",
168
+ };
169
+
170
+ await squatch.events().track(
171
+ {
172
+ userId: id,
173
+ accountId: accountId,
174
+ events: [
175
+ {
176
+ key: "purchase",
177
+ fields: fields, // Optional
178
+ // id: "kjv12kbwktb13t3", // Optional id
179
+ // dateTriggered: 1535136384753 // Optional date
180
+ },
181
+ ],
182
+ },
183
+ {
184
+ jwt,
185
+ }
186
+ );
187
+ // TODO: Eventually we'd like an API like this:
188
+ // squatch.events().track("purchase", { ...fields });
189
+ }
190
+ async function runEventBomb() {
191
+ while (true) {
192
+ await recordPurchase();
193
+ await delay(100);
194
+ }
195
+ }
196
+ function WidgetType(props) {
197
+ return (
198
+ <details
199
+ title={window["sandbox"].initObj.widgetType}
200
+ key={0}
201
+ id={`dropdown-basic-1`}
202
+ >
203
+ {widgetTypes.map((widgetType, i) => (
204
+ <a
205
+ key={i}
206
+ href={href({
207
+ ...window["sandbox"],
208
+ initObj: {
209
+ ...window["sandbox"].initObj,
210
+ widgetType: widgetType,
211
+ },
212
+ })}
213
+ >
214
+ {widgetType}
215
+ </a>
216
+ ))}
217
+ </details>
218
+ );
219
+ }
220
+ function ModeList(props) {
221
+ return (
222
+ <details
223
+ title={window["sandbox"].initObj.engagementMedium}
224
+ key={0}
225
+ id={`dropdown-basic-1`}
226
+ >
227
+ <summary>Engagement Medium</summary>
228
+ {modes.map((mode, i) => (
229
+ <a
230
+ key={i}
231
+ href={href({
232
+ ...window["sandbox"],
233
+ initObj: {
234
+ ...window["sandbox"].initObj,
235
+ engagementMedium: mode,
236
+ },
237
+ })}
238
+ >
239
+ {mode}
240
+ </a>
241
+ ))}
242
+ </details>
243
+ );
244
+ }
245
+ function UserList(props) {
246
+ return (
247
+ <details
248
+ title={"User: " + window["sandbox"].initObj.user.firstName}
249
+ key={0}
250
+ id={`dropdown-basic-1`}
251
+ >
252
+ <summary>User</summary>
253
+ {users.map((user, i) => (
254
+ <a
255
+ key={i}
256
+ href={href({
257
+ ...window["sandbox"],
258
+ initObj: {
259
+ ...window["sandbox"].initObj,
260
+ user: user,
261
+ },
262
+ })}
263
+ >
264
+ {user["firstName"] || "Empty"}
265
+ </a>
266
+ ))}
267
+ </details>
268
+ );
269
+ }
270
+ function VersionList(props) {
271
+ const { versions } = props;
272
+
273
+ return (
274
+ <details
275
+ title={"Version: " + window["sandbox"].version || "Head"}
276
+ key={0}
277
+ id={`dropdown-basic-1`}
278
+ >
279
+ <summary>Version</summary>
280
+ {versions.map((v, i) => (
281
+ <a
282
+ key={i}
283
+ href={href({
284
+ ...window["sandbox"],
285
+ version: v,
286
+ script:
287
+ v.toLocaleLowerCase() == "head"
288
+ ? script
289
+ : v == "local"
290
+ ? `./squatchjs.min.js`
291
+ : `https://unpkg.com/@saasquatch/squatch-js@${v}`,
292
+ })}
293
+ >
294
+ <button>{v}</button>
295
+ </a>
296
+ ))}
297
+ </details>
298
+ );
299
+ }
300
+
301
+ async function getMockWidget(widget, engagementMedium) {
302
+ window["mockWidget"] = widget;
303
+ window["sandbox"].initObj = {
304
+ ...window["sandbox"].initObj,
305
+ engagementMedium,
306
+ };
307
+
308
+ worker.use(
309
+ rest.put(
310
+ "https://staging.referralsaasquatch.com/api/*",
311
+ (req, res, ctx) => {
312
+ return res(
313
+ ctx.delay(500),
314
+ ctx.status(202, "Mocked status"),
315
+ ctx.json(widgets[window["mockWidget"]])
316
+ );
317
+ }
318
+ )
319
+ );
320
+ document.getElementById("squatchembed").innerHTML = "";
321
+ window["squatch"].widgets().upsertUser(window["sandbox"].initObj);
322
+ // window.location.href = `/?widgetType=${widget}`;
323
+ }
324
+
325
+ async function getCustomWidget() {
326
+ window["sandbox"].initObj = {
327
+ ...window["sandbox"].initObj,
328
+ engagementMedium: "EMBED",
329
+ };
330
+
331
+ const value = document.getElementById("custom-widget")?.value;
332
+ worker.use(
333
+ rest.put(
334
+ "https://staging.referralsaasquatch.com/api/*",
335
+ (req, res, ctx) => {
336
+ return res(
337
+ ctx.delay(500),
338
+ ctx.status(202, "Mocked status"),
339
+ ctx.json({ jsOptions: {}, user: {}, template: value })
340
+ );
341
+ }
342
+ )
343
+ );
344
+ document.getElementById("squatchembed").innerHTML = "";
345
+ window["squatch"].widgets().upsertUser(window["sandbox"].initObj);
346
+ // window.location.href = `/?widgetType=${widget}`;
347
+ }
348
+
349
+ function MockedWidgets(props) {
350
+ const { versions } = props;
351
+
352
+ const [engagementMedium, setEngagementMedium] = useState("EMBED");
353
+ return (
354
+ <details
355
+ title={"Version: " + window["sandbox"].version || "Head"}
356
+ key={0}
357
+ id={`dropdown-basic-1`}
358
+ >
359
+ <summary>Mocked Widgets</summary>
360
+ <label>Embed</label>
361
+ <input
362
+ type="radio"
363
+ name="embed"
364
+ checked={engagementMedium === "EMBED"}
365
+ onClick={() => setEngagementMedium("EMBED")}
366
+ ></input>
367
+
368
+ <label>Popup</label>
369
+ <input
370
+ type="radio"
371
+ name="popup"
372
+ checked={engagementMedium === "POPUP"}
373
+ onClick={() => setEngagementMedium("POPUP")}
374
+ ></input>
375
+ <br />
376
+ <button
377
+ onClick={() => getMockWidget("QuirksVanillaGA", engagementMedium)}
378
+ >
379
+ Quirks mode - Vanilla
380
+ </button>
381
+ <button onClick={() => getMockWidget("QuirksMintGA", engagementMedium)}>
382
+ Quirks mode - Mint
383
+ </button>
384
+ <button onClick={() => getMockWidget("classic", engagementMedium)}>
385
+ Classic
386
+ </button>
387
+ <button onClick={() => getMockWidget("MintGA", engagementMedium)}>
388
+ GA - Mint
389
+ </button>
390
+ <button onClick={() => getMockWidget("VanillaGA", engagementMedium)}>
391
+ GA - Vanilla
392
+ </button>
393
+ <button
394
+ onClick={() => getMockWidget("MintGAContainer", engagementMedium)}
395
+ >
396
+ Mint - With Container
397
+ </button>
398
+ <button
399
+ onClick={() => getMockWidget("QuirksMintGAContainer", engagementMedium)}
400
+ >
401
+ Quirks mode - Mint - With Container
402
+ </button>
403
+ <button
404
+ onClick={() =>
405
+ getMockWidget("MintGAContainerDisplayBlock", engagementMedium)
406
+ }
407
+ >
408
+ Mint - With Container + Display Block
409
+ </button>
410
+ <button
411
+ onClick={() =>
412
+ getMockWidget("QuirksMintGAContainerDisplayBlock", engagementMedium)
413
+ }
414
+ >
415
+ Quirks mode - Mint - With Container + Display Block
416
+ </button>
417
+ <button
418
+ onClick={() => getMockWidget("VanillaGANoContainer", engagementMedium)}
419
+ >
420
+ Vanilla - No Container
421
+ </button>
422
+ </details>
423
+ );
424
+ }
425
+
426
+ function CustomMockedWidget(props) {
427
+ const { versions } = props;
428
+
429
+ return (
430
+ <details
431
+ title={"Version: " + window["sandbox"].version || "Head"}
432
+ key={0}
433
+ id={`dropdown-basic-1`}
434
+ >
435
+ <summary>Custom Mocked Widget</summary>
436
+ <textarea
437
+ id="custom-widget"
438
+ rows={15}
439
+ cols={70}
440
+ style={{ maxWidth: "100%" }}
441
+ ></textarea>
442
+ <div>
443
+ <button onClick={() => getCustomWidget()}>Load Widget</button>
444
+ </div>
445
+ </details>
446
+ );
447
+ }
448
+ const root = document.getElementById("app");
449
+
450
+ render(<App />, root);