@coveo/shopify 0.0.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/README.md +126 -0
- package/dist/headless.esm.js +73 -0
- package/dist/utilities.esm.js +28 -0
- package/package.json +53 -0
package/README.md
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
# @coveo/shopify
|
|
2
|
+
|
|
3
|
+
The `@coveo/shopify` package provides utilities to integrate Shopify with Coveo's commerce engine.
|
|
4
|
+
|
|
5
|
+
It includes functions to fetch app proxy configurations and build a commerce engine tailored for Shopify.
|
|
6
|
+
|
|
7
|
+
## Benefits
|
|
8
|
+
|
|
9
|
+
Using the `@coveo/shopify` package ensures that the commerce engine will emit events using the same `clientId` both inside and outside Shopify Web Pixels. This provides consistent tracking and analytics across the entire Shopify ecosystem.
|
|
10
|
+
|
|
11
|
+
## Installation
|
|
12
|
+
|
|
13
|
+
Install the package using npm or yarn:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npm install @coveo/shopify
|
|
17
|
+
# or
|
|
18
|
+
yarn add @coveo/shopify
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Features
|
|
22
|
+
|
|
23
|
+
### `fetchAppProxyConfig`
|
|
24
|
+
|
|
25
|
+
Fetches the app proxy configuration for a given Shopify market.
|
|
26
|
+
|
|
27
|
+
#### Parameters
|
|
28
|
+
|
|
29
|
+
- `marketId` (required): The market identifier.
|
|
30
|
+
- `appProxyUrl` (optional): The URL of the app proxy. Defaults to `'/apps/coveo'`. Useful if implementing your own custom proxy.
|
|
31
|
+
|
|
32
|
+
#### Returns
|
|
33
|
+
|
|
34
|
+
A promise that resolves to an object containing:
|
|
35
|
+
|
|
36
|
+
- `accessToken`: The access token for the app proxy.
|
|
37
|
+
- `organizationId`: The organization ID.
|
|
38
|
+
- `environment`: The environment configuration.
|
|
39
|
+
- `trackingId`: The tracking ID.
|
|
40
|
+
|
|
41
|
+
#### Example
|
|
42
|
+
|
|
43
|
+
```typescript
|
|
44
|
+
import {fetchAppProxyConfig} from '@coveo/shopify/headless';
|
|
45
|
+
|
|
46
|
+
const config = await fetchAppProxyConfig({
|
|
47
|
+
marketId: 'market_123432',
|
|
48
|
+
});
|
|
49
|
+
console.log(config);
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### `buildShopifyCommerceEngine`
|
|
53
|
+
|
|
54
|
+
Builds a commerce engine instance configured for Shopify.
|
|
55
|
+
|
|
56
|
+
#### Parameters
|
|
57
|
+
|
|
58
|
+
- `commerceEngineOptions` (required): Options for the commerce engine.
|
|
59
|
+
- `shopifyCookie` (optional): The value of the Shopify `_shopify_y` cookie. If not provided, it will attempt to retrieve it from the browser's cookies.
|
|
60
|
+
- `environment` (optional): A custom environment configuration (useful for testing & SSR).
|
|
61
|
+
|
|
62
|
+
#### Returns
|
|
63
|
+
|
|
64
|
+
A configured commerce engine instance.
|
|
65
|
+
|
|
66
|
+
#### Example
|
|
67
|
+
|
|
68
|
+
```typescript
|
|
69
|
+
<script type="module">
|
|
70
|
+
import {buildShopifyCommerceEngine, fetchAppProxyConfig} from 'https://static.cloud.coveo.com/shopify/v1/headless.esm.js';
|
|
71
|
+
|
|
72
|
+
const config = await fetchAppProxyConfig({marketId: 'market_123432'});
|
|
73
|
+
const engine = buildShopifyCommerceEngine({
|
|
74
|
+
commerceEngineOptions: {
|
|
75
|
+
configuration : {
|
|
76
|
+
accessToken: config.accessToken,
|
|
77
|
+
organizationId: config.organizationId,
|
|
78
|
+
environment: config.environment,
|
|
79
|
+
analytics: {
|
|
80
|
+
enabled: true,
|
|
81
|
+
trackingId: config.trackingId,
|
|
82
|
+
},
|
|
83
|
+
context: {
|
|
84
|
+
country: {{ localization.country.iso_code | json }},
|
|
85
|
+
currency: {{ localization.country.currency.iso_code | json }},
|
|
86
|
+
view: {
|
|
87
|
+
url: {{ canonical_url | json }},
|
|
88
|
+
},
|
|
89
|
+
language: {{ request.locale.iso_code | json }},
|
|
90
|
+
cart: {{ cart.items | json }}.map(function (item) {
|
|
91
|
+
return {
|
|
92
|
+
productId: item.product_id,
|
|
93
|
+
name: item.title,
|
|
94
|
+
price: item.final_price,
|
|
95
|
+
quantity: item.quantity,
|
|
96
|
+
};
|
|
97
|
+
}),
|
|
98
|
+
}
|
|
99
|
+
},
|
|
100
|
+
});
|
|
101
|
+
console.log(engine);
|
|
102
|
+
</script>
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
### `getShopifyCookie`
|
|
106
|
+
|
|
107
|
+
Retrieves the value of a specified Shopify cookie by its name.
|
|
108
|
+
|
|
109
|
+
#### Returns
|
|
110
|
+
|
|
111
|
+
The value of the specified cookie, or `null` if the cookie is not found.
|
|
112
|
+
|
|
113
|
+
#### Notes
|
|
114
|
+
|
|
115
|
+
This function is intended for use in **browser environments only**, as it relies on the `document.cookie` API. Attempting to use this function in non-browser environments will result in an error or undefined behavior.
|
|
116
|
+
|
|
117
|
+
#### Example
|
|
118
|
+
|
|
119
|
+
```typescript
|
|
120
|
+
import {getShopifyCookie} from '@coveo/shopify';
|
|
121
|
+
|
|
122
|
+
const shopifyCookie = getShopifyCookie();
|
|
123
|
+
console.log(shopifyCookie);
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
---
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
*
|
|
4
|
+
* Copyright 2025 Coveo Solutions Inc.
|
|
5
|
+
*
|
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
* you may not use this file except in compliance with the License.
|
|
8
|
+
* You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
* See the License for the specific language governing permissions and
|
|
16
|
+
* limitations under the License.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
// src/headless.ts
|
|
20
|
+
import {
|
|
21
|
+
buildCommerceEngine
|
|
22
|
+
} from "@coveo/headless/commerce";
|
|
23
|
+
import {
|
|
24
|
+
buildBrowserEnvironment,
|
|
25
|
+
clientIdKey
|
|
26
|
+
} from "@coveo/relay";
|
|
27
|
+
|
|
28
|
+
// src/utilities.ts
|
|
29
|
+
import { v5 } from "uuid";
|
|
30
|
+
var UUID_NAMESPACE = "c2db3701-991e-424d-b117-03e30d43b651";
|
|
31
|
+
function getClientId(shopifyCookie) {
|
|
32
|
+
return v5(shopifyCookie, UUID_NAMESPACE);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// src/headless.ts
|
|
36
|
+
export * from "@coveo/headless/commerce";
|
|
37
|
+
var SHOPIFY_COOKIE_KEY = "_shopify_y";
|
|
38
|
+
async function fetchAppProxyConfig({
|
|
39
|
+
appProxyUrl = "/apps/coveo",
|
|
40
|
+
marketId
|
|
41
|
+
}) {
|
|
42
|
+
const response = await fetch(`${appProxyUrl}?marketId=${marketId}]`);
|
|
43
|
+
return response.json();
|
|
44
|
+
}
|
|
45
|
+
function buildShopifyCommerceEngine({
|
|
46
|
+
commerceEngineOptions,
|
|
47
|
+
shopifyCookie,
|
|
48
|
+
environment
|
|
49
|
+
}) {
|
|
50
|
+
const browserEnvironment = environment || buildBrowserEnvironment();
|
|
51
|
+
const cookie = shopifyCookie || getShopifyCookie();
|
|
52
|
+
if (!cookie) {
|
|
53
|
+
throw new Error(
|
|
54
|
+
"Unable to find the _shopify_y cookie. Please ensure you are running this code in a Shopify store."
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
browserEnvironment.storage.setItem(clientIdKey, getClientId(cookie));
|
|
58
|
+
return buildCommerceEngine(commerceEngineOptions);
|
|
59
|
+
}
|
|
60
|
+
function getShopifyCookie(name = SHOPIFY_COOKIE_KEY) {
|
|
61
|
+
const match = document.cookie.match(
|
|
62
|
+
new RegExp("(?:^|;\\s*)" + name + "=([^;]*)")
|
|
63
|
+
);
|
|
64
|
+
return match ? decodeURIComponent(match[1]) : null;
|
|
65
|
+
}
|
|
66
|
+
export {
|
|
67
|
+
SHOPIFY_COOKIE_KEY,
|
|
68
|
+
UUID_NAMESPACE,
|
|
69
|
+
buildShopifyCommerceEngine,
|
|
70
|
+
fetchAppProxyConfig,
|
|
71
|
+
getClientId,
|
|
72
|
+
getShopifyCookie
|
|
73
|
+
};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
*
|
|
4
|
+
* Copyright 2025 Coveo Solutions Inc.
|
|
5
|
+
*
|
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
* you may not use this file except in compliance with the License.
|
|
8
|
+
* You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
* See the License for the specific language governing permissions and
|
|
16
|
+
* limitations under the License.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
// src/utilities.ts
|
|
20
|
+
import { v5 } from "uuid";
|
|
21
|
+
var UUID_NAMESPACE = "c2db3701-991e-424d-b117-03e30d43b651";
|
|
22
|
+
function getClientId(shopifyCookie) {
|
|
23
|
+
return v5(shopifyCookie, UUID_NAMESPACE);
|
|
24
|
+
}
|
|
25
|
+
export {
|
|
26
|
+
UUID_NAMESPACE,
|
|
27
|
+
getClientId
|
|
28
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@coveo/shopify",
|
|
3
|
+
"repository": {
|
|
4
|
+
"type": "git",
|
|
5
|
+
"url": "git+https://github.com/coveo/ui-kit.git",
|
|
6
|
+
"directory": "packages/shopify"
|
|
7
|
+
},
|
|
8
|
+
"main": "./dist/headless.esm.js",
|
|
9
|
+
"module": "./dist/headless.esm.js",
|
|
10
|
+
"types": "./dist/definitions/index.d.ts",
|
|
11
|
+
"license": "Apache-2.0",
|
|
12
|
+
"version": "0.0.1",
|
|
13
|
+
"files": [
|
|
14
|
+
"dist/"
|
|
15
|
+
],
|
|
16
|
+
"type": "module",
|
|
17
|
+
"exports": {
|
|
18
|
+
".": {
|
|
19
|
+
"import": "./dist/headless.esm.js",
|
|
20
|
+
"default": "./dist/headless.esm.js"
|
|
21
|
+
},
|
|
22
|
+
"./headless": {
|
|
23
|
+
"import": "./dist/headless.esm.js",
|
|
24
|
+
"default": "./dist/headless.esm.js"
|
|
25
|
+
},
|
|
26
|
+
"./utilities": {
|
|
27
|
+
"import": "./dist/utilities.esm.js",
|
|
28
|
+
"default": "./dist/utilities.esm.js"
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
"scripts": {
|
|
32
|
+
"dev": "npm run build:bundles -- dev",
|
|
33
|
+
"build": "nx build",
|
|
34
|
+
"build:bundles": "node esbuild.mjs",
|
|
35
|
+
"test": "vitest run",
|
|
36
|
+
"clean": "rimraf -rf dist/* && rimraf -rf cdn/",
|
|
37
|
+
"publish:npm": "npm run-script -w=@coveo/ci npm-publish",
|
|
38
|
+
"publish:bump": "npm run-script -w=@coveo/ci bump",
|
|
39
|
+
"promote:npm:latest": "npm run-script -w=@coveo/ci promote-npm-prod"
|
|
40
|
+
},
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"@coveo/ci": "1.0.0",
|
|
43
|
+
"vitest": "3.1.2"
|
|
44
|
+
},
|
|
45
|
+
"dependencies": {
|
|
46
|
+
"@coveo/headless": "3.22.4",
|
|
47
|
+
"@coveo/relay": "1.1.3",
|
|
48
|
+
"uuid": "^11.0.0"
|
|
49
|
+
},
|
|
50
|
+
"engines": {
|
|
51
|
+
"node": "^20.9.0 || ^22.11.0"
|
|
52
|
+
}
|
|
53
|
+
}
|