@wapitee/typhoonx-hydrogen 0.1.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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Wapitee Interactive
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,59 @@
1
+ # @wapitee/typhoonx-hydrogen
2
+
3
+ Hydrogen `useAnalytics` subscriber that sends TyphoonX storefront events.
4
+
5
+ This is not a Shopify Web Pixel. Place it in a Hydrogen storefront, inside `Analytics.Provider`.
6
+
7
+ ## Install
8
+
9
+ ```bash
10
+ pnpm add @wapitee/typhoonx-hydrogen
11
+ ```
12
+
13
+ Peer dependencies: `react` ^18.3.1 and `@shopify/hydrogen` >=2025.5.1. Node.js 20 or later.
14
+
15
+ ## Usage
16
+
17
+ ```tsx
18
+ import {Analytics} from '@shopify/hydrogen';
19
+ import TyphoonX from '@wapitee/typhoonx-hydrogen';
20
+
21
+ export function App() {
22
+ return (
23
+ <Analytics.Provider cart={cart} consent={consent} shop={shop}>
24
+ <TyphoonX merchantId="TPX-XXXXXX" shopId="123456789" />
25
+ {/* storefront */}
26
+ </Analytics.Provider>
27
+ );
28
+ }
29
+ ```
30
+
31
+ The component renders `null`.
32
+
33
+ ### Props
34
+
35
+ | Prop | Type | Required | Description |
36
+ | -------------- | -------- | -------- | ----------------------------------------------------------------------------------------------------------- |
37
+ | `merchantId` | `string` | yes | TyphoonX account key (`TPX-…`) |
38
+ | `shopId` | `string` | yes | Shopify shop numeric id |
39
+ | `cookieDomain` | `string` | no | Cookie `Domain` for a first-party client id. Omit for a host-only cookie (recommended on `*.myshopify.com`) |
40
+
41
+ ```ts
42
+ import type {TyphoonXProps} from '@wapitee/typhoonx-hydrogen';
43
+ ```
44
+
45
+ ### Events
46
+
47
+ | Hydrogen | TyphoonX |
48
+ | ----------------------- | ------------- |
49
+ | `page_viewed` | `page_view` |
50
+ | `product_viewed` | `view_item` |
51
+ | `product_added_to_cart` | `add_to_cart` |
52
+
53
+ Beacons go to `https://spell.typhoonx.io/api/v1/receive`. Checkout and purchase are out of scope.
54
+
55
+ Sends are gated by Hydrogen `canTrack()` (Customer Privacy). The anonymous visitor id lives in `__typhoon_client_id` (one year, `SameSite=Lax`, `Secure` on HTTPS).
56
+
57
+ ## License
58
+
59
+ [MIT](./LICENSE) © Wapitee Interactive
@@ -0,0 +1,7 @@
1
+ export interface TyphoonXProps {
2
+ merchantId: string;
3
+ shopId: string;
4
+ cookieDomain?: string;
5
+ }
6
+ export default function TyphoonX(props: TyphoonXProps): import("react").JSX.Element | null;
7
+ //# sourceMappingURL=TyphoonX.d.ts.map
@@ -0,0 +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;AAwBD,MAAM,CAAC,OAAO,UAAU,QAAQ,CAAC,KAAK,EAAE,aAAa,sCAYpD"}
@@ -0,0 +1,99 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ import { parseGid, useAnalytics } from '@shopify/hydrogen';
3
+ import { useEffect, useRef, useState } from 'react';
4
+ import { getOrCreateClientId } from './client-id.js';
5
+ const COLLECT_ENDPOINT = 'https://spell.typhoonx.io/api/v1/receive';
6
+ export default function TyphoonX(props) {
7
+ const [isClient, setIsClient] = useState(false);
8
+ useEffect(() => {
9
+ setIsClient(true);
10
+ }, []);
11
+ if (!isClient) {
12
+ return null;
13
+ }
14
+ return _jsx(TyphoonXClient, { ...props });
15
+ }
16
+ function TyphoonXClient({ cookieDomain, merchantId, shopId }) {
17
+ const { canTrack, register, subscribe } = useAnalytics();
18
+ const { ready } = register('TyphoonX');
19
+ const currentUrlRef = useRef(null);
20
+ const previousUrlRef = useRef(null);
21
+ useEffect(() => {
22
+ let cancelled = false;
23
+ const clientId = getOrCreateClientId(cookieDomain);
24
+ const referrerFor = (url) => {
25
+ if (currentUrlRef.current !== url) {
26
+ previousUrlRef.current = currentUrlRef.current;
27
+ currentUrlRef.current = url;
28
+ }
29
+ return previousUrlRef.current ?? document.referrer;
30
+ };
31
+ const send = (payload) => {
32
+ if (cancelled || !canTrack()) {
33
+ return;
34
+ }
35
+ navigator.sendBeacon(COLLECT_ENDPOINT, new Blob([JSON.stringify(payload)], { type: 'application/json' }));
36
+ };
37
+ const shared = (url, currency) => ({
38
+ client_id: clientId,
39
+ merchant_id: merchantId,
40
+ referrer: referrerFor(url),
41
+ request_page_url: url,
42
+ shop_id: shopId,
43
+ timestamp: new Date().toISOString(),
44
+ user_agent: window.navigator.userAgent,
45
+ ...(currency === undefined ? {} : { currency }),
46
+ });
47
+ subscribe('page_viewed', (data) => {
48
+ send({
49
+ ...shared(data.url),
50
+ event: 'page_view',
51
+ });
52
+ });
53
+ subscribe('product_viewed', (data) => {
54
+ const product = data.products[0];
55
+ if (product === undefined) {
56
+ return;
57
+ }
58
+ send({
59
+ ...shared(data.url, data.shop?.currency),
60
+ event: 'view_item',
61
+ items: [
62
+ {
63
+ item_brand: product.vendor,
64
+ item_id: parseGid(product.id).id,
65
+ item_name: product.title,
66
+ price: product.price,
67
+ quantity: 1,
68
+ },
69
+ ],
70
+ value: product.price,
71
+ });
72
+ });
73
+ subscribe('product_added_to_cart', (data) => {
74
+ const { currentLine } = data;
75
+ if (currentLine === undefined) {
76
+ return;
77
+ }
78
+ send({
79
+ ...shared(window.location.href, data.shop?.currency),
80
+ event: 'add_to_cart',
81
+ items: [
82
+ {
83
+ item_brand: currentLine.merchandise.product.vendor,
84
+ item_id: parseGid(currentLine.merchandise.product.id).id,
85
+ item_name: currentLine.merchandise.product.title,
86
+ price: currentLine.merchandise.price.amount,
87
+ quantity: currentLine.quantity,
88
+ },
89
+ ],
90
+ value: currentLine.cost.totalAmount.amount,
91
+ });
92
+ });
93
+ ready();
94
+ return () => {
95
+ cancelled = true;
96
+ };
97
+ }, [canTrack, cookieDomain, merchantId, ready, shopId, subscribe]);
98
+ return null;
99
+ }
@@ -0,0 +1,2 @@
1
+ export declare function getOrCreateClientId(cookieDomain?: string): string;
2
+ //# sourceMappingURL=client-id.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client-id.d.ts","sourceRoot":"","sources":["../src/client-id.ts"],"names":[],"mappings":"AAGA,wBAAgB,mBAAmB,CAAC,YAAY,CAAC,EAAE,MAAM,GAAG,MAAM,CA8BjE"}
@@ -0,0 +1,28 @@
1
+ const COOKIE = '__typhoon_client_id';
2
+ const MAX_AGE = 31_536_000;
3
+ export function getOrCreateClientId(cookieDomain) {
4
+ const prefix = `${COOKIE}=`;
5
+ const existing = document.cookie
6
+ .split(';')
7
+ .map((part) => part.trim())
8
+ .find((part) => part.startsWith(prefix))
9
+ ?.slice(prefix.length);
10
+ if (existing) {
11
+ return decodeURIComponent(existing);
12
+ }
13
+ const id = crypto.randomUUID();
14
+ const segments = [
15
+ `${COOKIE}=${id}`,
16
+ 'Path=/',
17
+ 'SameSite=Lax',
18
+ `Max-Age=${String(MAX_AGE)}`,
19
+ ];
20
+ if (cookieDomain) {
21
+ segments.push(`Domain=${cookieDomain}`);
22
+ }
23
+ if (window.location.protocol === 'https:') {
24
+ segments.push('Secure');
25
+ }
26
+ document.cookie = segments.join('; ');
27
+ return id;
28
+ }
@@ -0,0 +1,3 @@
1
+ export type { TyphoonXProps } from './TyphoonX.js';
2
+ export { default } from './TyphoonX.js';
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EAAC,aAAa,EAAC,MAAM,eAAe,CAAC;AACjD,OAAO,EAAC,OAAO,EAAC,MAAM,eAAe,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ export { default } from './TyphoonX.js';
package/package.json ADDED
@@ -0,0 +1,52 @@
1
+ {
2
+ "name": "@wapitee/typhoonx-hydrogen",
3
+ "version": "0.1.0",
4
+ "description": "TyphoonX Hydrogen Analytics subscriber",
5
+ "homepage": "https://github.com/Wapitee-Interactive-Marketing-Limited/typhoonx-js/tree/main/packages/typhoonx-hydrogen",
6
+ "bugs": {
7
+ "url": "https://github.com/Wapitee-Interactive-Marketing-Limited/typhoonx-js/issues"
8
+ },
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/Wapitee-Interactive-Marketing-Limited/typhoonx-js.git",
12
+ "directory": "packages/typhoonx-hydrogen"
13
+ },
14
+ "license": "MIT",
15
+ "author": "jay@wapitee.io",
16
+ "sideEffects": false,
17
+ "type": "module",
18
+ "exports": {
19
+ ".": {
20
+ "types": "./dist/index.d.ts",
21
+ "import": "./dist/index.js"
22
+ }
23
+ },
24
+ "main": "./dist/index.js",
25
+ "types": "./dist/index.d.ts",
26
+ "files": [
27
+ "LICENSE",
28
+ "README.md",
29
+ "dist"
30
+ ],
31
+ "devDependencies": {
32
+ "@shopify/hydrogen": "^2026.4.5",
33
+ "@types/react": "^18.3.31",
34
+ "react": "^18.3.1",
35
+ "typescript": "^6.0.3"
36
+ },
37
+ "peerDependencies": {
38
+ "@shopify/hydrogen": ">=2025.5.1",
39
+ "react": "^18.3.1"
40
+ },
41
+ "engines": {
42
+ "node": ">=20"
43
+ },
44
+ "publishConfig": {
45
+ "access": "public",
46
+ "provenance": true,
47
+ "registry": "https://registry.npmjs.org"
48
+ },
49
+ "scripts": {
50
+ "build": "tsc -p tsconfig.json"
51
+ }
52
+ }