@wapitee/typhoonx-hydrogen 0.5.0 → 0.6.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.
@@ -1 +1 @@
1
- {"version":3,"file":"TyphoonX.d.ts","sourceRoot":"","sources":["../src/TyphoonX.tsx"],"names":[],"mappings":"AAKA,MAAM,WAAW,aAAa;IAC5B,UAAU,EAAE,MAAM,CAAC;IACnB,MAAM,EAAE,MAAM,CAAC;IACf,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB;AA2BD,MAAM,CAAC,OAAO,UAAU,QAAQ,CAAC,KAAK,EAAE,aAAa,sCAYpD"}
1
+ {"version":3,"file":"TyphoonX.d.ts","sourceRoot":"","sources":["../src/TyphoonX.tsx"],"names":[],"mappings":"AAKA,MAAM,WAAW,aAAa;IAC5B,UAAU,EAAE,MAAM,CAAC;IACnB,MAAM,EAAE,MAAM,CAAC;IACf,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB;AAmCD,MAAM,CAAC,OAAO,UAAU,QAAQ,CAAC,KAAK,EAAE,aAAa,sCAqBpD"}
package/dist/TyphoonX.js CHANGED
@@ -1,14 +1,21 @@
1
1
  import { jsx as _jsx } from "react/jsx-runtime";
2
2
  import { flattenConnection, parseGid, useAnalytics } from '@shopify/hydrogen';
3
3
  import { useEffect, useRef, useState } from 'react';
4
- import { getOrCreateClientId } from './client-id.js';
4
+ import { getOrCreateClientId, hasClientId } from './client-id.js';
5
5
  const COLLECT_ENDPOINT = 'https://spell.typhoonx.io/api/v1/receive';
6
6
  export default function TyphoonX(props) {
7
+ const { subscribe } = useAnalytics();
8
+ const isInsideProvider = subscribe.length > 0;
7
9
  const [isClient, setIsClient] = useState(false);
8
10
  useEffect(() => {
9
11
  setIsClient(true);
10
12
  }, []);
11
- if (!isClient) {
13
+ useEffect(() => {
14
+ if (!isInsideProvider) {
15
+ console.error('[TyphoonX] <TyphoonX> must be rendered inside <Analytics.Provider>.');
16
+ }
17
+ }, [isInsideProvider]);
18
+ if (!isClient || !isInsideProvider) {
12
19
  return null;
13
20
  }
14
21
  return _jsx(TyphoonXClient, { ...props });
@@ -18,6 +25,17 @@ function TyphoonXClient({ cookieDomain, merchantId, shopId }) {
18
25
  const { ready } = register('TyphoonX');
19
26
  const currentUrlRef = useRef(null);
20
27
  const previousUrlRef = useRef(null);
28
+ useEffect(() => {
29
+ try {
30
+ if (!hasClientId()) {
31
+ window.localStorage.setItem('__typhoon_first_visit_time', new Date().toISOString());
32
+ window.localStorage.setItem('__typhoon_first_visit_url', window.location.href);
33
+ }
34
+ }
35
+ catch {
36
+ // Do nothing
37
+ }
38
+ }, [hasClientId()]);
21
39
  useEffect(() => {
22
40
  const clientId = getOrCreateClientId(cookieDomain);
23
41
  const referrerFor = (url) => {
@@ -72,10 +90,28 @@ function TyphoonXClient({ cookieDomain, merchantId, shopId }) {
72
90
  });
73
91
  });
74
92
  subscribe('page_viewed', (data) => {
93
+ const payload = shared(data.url);
75
94
  send({
76
- ...shared(data.url),
95
+ ...payload,
77
96
  event: 'page_view',
78
97
  });
98
+ try {
99
+ const visitTime = window.localStorage.getItem('__typhoon_first_visit_time');
100
+ const visitUrl = window.localStorage.getItem('__typhoon_first_visit_url');
101
+ if (!visitTime || !visitUrl)
102
+ return;
103
+ send({
104
+ ...payload,
105
+ request_page_url: visitUrl,
106
+ timestamp: visitTime,
107
+ event: 'first_visit',
108
+ });
109
+ window.localStorage.removeItem('__typhoon_first_visit_time');
110
+ window.localStorage.removeItem('__typhoon_first_visit_url');
111
+ }
112
+ catch {
113
+ // Do nothing
114
+ }
79
115
  });
80
116
  subscribe('product_added_to_cart', (data) => {
81
117
  const { currentLine } = data;
@@ -1,3 +1,5 @@
1
+ /** Whether `__typhoon_client_id` is already set. */
2
+ export declare function hasClientId(): boolean;
1
3
  /** Existing `__typhoon_client_id`, or a new UUID cookie on the apex domain. */
2
4
  export declare function getOrCreateClientId(cookieDomain?: string): string;
3
5
  //# sourceMappingURL=client-id.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"client-id.d.ts","sourceRoot":"","sources":["../src/client-id.ts"],"names":[],"mappings":"AAwBA,+EAA+E;AAC/E,wBAAgB,mBAAmB,CAAC,YAAY,CAAC,EAAE,MAAM,GAAG,MAAM,CAWjE"}
1
+ {"version":3,"file":"client-id.d.ts","sourceRoot":"","sources":["../src/client-id.ts"],"names":[],"mappings":"AAwBA,oDAAoD;AACpD,wBAAgB,WAAW,IAAI,OAAO,CAErC;AAED,+EAA+E;AAC/E,wBAAgB,mBAAmB,CAAC,YAAY,CAAC,EAAE,MAAM,GAAG,MAAM,CAWjE"}
package/dist/client-id.js CHANGED
@@ -8,6 +8,10 @@ const cookies = new Cookies(null, {
8
8
  sameSite: 'lax',
9
9
  maxAge: MAX_AGE,
10
10
  });
11
+ /** Whether `__typhoon_client_id` is already set. */
12
+ export function hasClientId() {
13
+ return cookies.get(COOKIE, { doNotParse: true }) !== undefined;
14
+ }
11
15
  /** Existing `__typhoon_client_id`, or a new UUID cookie on the apex domain. */
12
16
  export function getOrCreateClientId(cookieDomain) {
13
17
  const existing = cookies.get(COOKIE, { doNotParse: true });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wapitee/typhoonx-hydrogen",
3
- "version": "0.5.0",
3
+ "version": "0.6.0",
4
4
  "description": "TyphoonX Hydrogen Analytics subscriber",
5
5
  "keywords": [
6
6
  "typhoonx",