@reactionary/provider-algolia 0.3.0 → 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/core/initialize.js +4 -0
- package/package.json +2 -2
- package/providers/analytics.provider.js +75 -0
- package/providers/index.js +1 -0
- package/src/providers/analytics.provider.d.ts +12 -0
- package/src/providers/index.d.ts +1 -0
- package/src/providers/product-search.provider.d.ts +3 -69
- package/src/test/test-utils.d.ts +0 -2
- package/test/test-utils.js +0 -32
package/core/initialize.js
CHANGED
|
@@ -1,10 +1,14 @@
|
|
|
1
1
|
import { AlgoliaSearchProvider } from "../providers/product-search.provider.js";
|
|
2
|
+
import { AlgoliaAnalyticsProvider } from "../providers/analytics.provider.js";
|
|
2
3
|
function withAlgoliaCapabilities(configuration, capabilities) {
|
|
3
4
|
return (cache, context) => {
|
|
4
5
|
const client = {};
|
|
5
6
|
if (capabilities.productSearch) {
|
|
6
7
|
client.productSearch = new AlgoliaSearchProvider(configuration, cache, context);
|
|
7
8
|
}
|
|
9
|
+
if (capabilities.analytics) {
|
|
10
|
+
client.analytics = new AlgoliaAnalyticsProvider(cache, context, configuration);
|
|
11
|
+
}
|
|
8
12
|
return client;
|
|
9
13
|
};
|
|
10
14
|
}
|
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@reactionary/provider-algolia",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.1",
|
|
4
4
|
"main": "index.js",
|
|
5
5
|
"types": "src/index.d.ts",
|
|
6
6
|
"dependencies": {
|
|
7
|
-
"@reactionary/core": "0.3.
|
|
7
|
+
"@reactionary/core": "0.3.1",
|
|
8
8
|
"algoliasearch": "^5.23.4",
|
|
9
9
|
"zod": "4.1.9"
|
|
10
10
|
},
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AnalyticsProvider
|
|
3
|
+
} from "@reactionary/core";
|
|
4
|
+
import {
|
|
5
|
+
insightsClient
|
|
6
|
+
} from "algoliasearch";
|
|
7
|
+
class AlgoliaAnalyticsProvider extends AnalyticsProvider {
|
|
8
|
+
constructor(cache, requestContext, config) {
|
|
9
|
+
super(cache, requestContext);
|
|
10
|
+
this.config = config;
|
|
11
|
+
this.client = insightsClient(this.config.appId, this.config.apiKey);
|
|
12
|
+
}
|
|
13
|
+
async processProductAddToCart(event) {
|
|
14
|
+
if (event.source && event.source.type === "search") {
|
|
15
|
+
const algoliaEvent = {
|
|
16
|
+
eventName: "addToCart",
|
|
17
|
+
eventType: "conversion",
|
|
18
|
+
eventSubtype: "addToCart",
|
|
19
|
+
index: this.config.indexName,
|
|
20
|
+
objectIDs: [event.product.key],
|
|
21
|
+
userToken: this.context.session.identityContext.personalizationKey,
|
|
22
|
+
queryID: event.source.identifier.key
|
|
23
|
+
};
|
|
24
|
+
this.client.pushEvents({
|
|
25
|
+
events: [algoliaEvent]
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
async processProductSummaryClick(event) {
|
|
30
|
+
if (event.source && event.source.type === "search") {
|
|
31
|
+
const algoliaEvent = {
|
|
32
|
+
eventName: "click",
|
|
33
|
+
eventType: "click",
|
|
34
|
+
index: this.config.indexName,
|
|
35
|
+
objectIDs: [event.product.key],
|
|
36
|
+
userToken: this.context.session.identityContext.personalizationKey,
|
|
37
|
+
positions: [event.position],
|
|
38
|
+
queryID: event.source.identifier.key
|
|
39
|
+
};
|
|
40
|
+
this.client.pushEvents({
|
|
41
|
+
events: [algoliaEvent]
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
async processProductSummaryView(event) {
|
|
46
|
+
if (event.source && event.source.type === "search") {
|
|
47
|
+
const algoliaEvent = {
|
|
48
|
+
eventName: "view",
|
|
49
|
+
eventType: "view",
|
|
50
|
+
index: this.config.indexName,
|
|
51
|
+
objectIDs: event.products.map((x) => x.key),
|
|
52
|
+
userToken: this.context.session.identityContext.personalizationKey
|
|
53
|
+
};
|
|
54
|
+
this.client.pushEvents({
|
|
55
|
+
events: [algoliaEvent]
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
async processPurchase(event) {
|
|
60
|
+
const algoliaEvent = {
|
|
61
|
+
eventName: "purchase",
|
|
62
|
+
eventType: "conversion",
|
|
63
|
+
eventSubtype: "purchase",
|
|
64
|
+
index: this.config.indexName,
|
|
65
|
+
objectIDs: event.order.items.map((x) => x.identifier.key),
|
|
66
|
+
userToken: this.context.session.identityContext.personalizationKey
|
|
67
|
+
};
|
|
68
|
+
this.client.pushEvents({
|
|
69
|
+
events: [algoliaEvent]
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
export {
|
|
74
|
+
AlgoliaAnalyticsProvider
|
|
75
|
+
};
|
package/providers/index.js
CHANGED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { AnalyticsProvider, type AnalyticsMutationProductAddToCartEvent, type AnalyticsMutationProductSummaryClickEvent, type AnalyticsMutationProductSummaryViewEvent, type AnalyticsMutationPurchaseEvent, type Cache, type RequestContext } from '@reactionary/core';
|
|
2
|
+
import { type InsightsClient } from 'algoliasearch';
|
|
3
|
+
import type { AlgoliaConfiguration } from '../schema/configuration.schema.js';
|
|
4
|
+
export declare class AlgoliaAnalyticsProvider extends AnalyticsProvider {
|
|
5
|
+
protected client: InsightsClient;
|
|
6
|
+
protected config: AlgoliaConfiguration;
|
|
7
|
+
constructor(cache: Cache, requestContext: RequestContext, config: AlgoliaConfiguration);
|
|
8
|
+
protected processProductAddToCart(event: AnalyticsMutationProductAddToCartEvent): Promise<void>;
|
|
9
|
+
protected processProductSummaryClick(event: AnalyticsMutationProductSummaryClickEvent): Promise<void>;
|
|
10
|
+
protected processProductSummaryView(event: AnalyticsMutationProductSummaryViewEvent): Promise<void>;
|
|
11
|
+
protected processPurchase(event: AnalyticsMutationPurchaseEvent): Promise<void>;
|
|
12
|
+
}
|
package/src/providers/index.d.ts
CHANGED
|
@@ -22,32 +22,7 @@ export declare class AlgoliaSearchProvider extends ProductSearchProvider {
|
|
|
22
22
|
};
|
|
23
23
|
name: string;
|
|
24
24
|
slug: string;
|
|
25
|
-
variants:
|
|
26
|
-
variant: {
|
|
27
|
-
sku: string;
|
|
28
|
-
};
|
|
29
|
-
image: {
|
|
30
|
-
sourceUrl: string;
|
|
31
|
-
altText: string;
|
|
32
|
-
width?: number | undefined;
|
|
33
|
-
height?: number | undefined;
|
|
34
|
-
};
|
|
35
|
-
options?: {
|
|
36
|
-
identifier: {
|
|
37
|
-
key: string;
|
|
38
|
-
};
|
|
39
|
-
name: string;
|
|
40
|
-
value: {
|
|
41
|
-
identifier: {
|
|
42
|
-
option: {
|
|
43
|
-
key: string;
|
|
44
|
-
};
|
|
45
|
-
key: string;
|
|
46
|
-
};
|
|
47
|
-
label: string;
|
|
48
|
-
};
|
|
49
|
-
} | undefined;
|
|
50
|
-
}[];
|
|
25
|
+
variants: ProductSearchResultItemVariant[];
|
|
51
26
|
};
|
|
52
27
|
protected parseVariant(variant: AlgoliaNativeVariant, product: AlgoliaNativeRecord): ProductSearchResultItemVariant;
|
|
53
28
|
protected parsePaginatedResult(body: SearchResponse<AlgoliaNativeRecord>, query: ProductSearchQueryByTerm): {
|
|
@@ -75,50 +50,9 @@ export declare class AlgoliaSearchProvider extends ProductSearchProvider {
|
|
|
75
50
|
};
|
|
76
51
|
name: string;
|
|
77
52
|
slug: string;
|
|
78
|
-
variants:
|
|
79
|
-
variant: {
|
|
80
|
-
sku: string;
|
|
81
|
-
};
|
|
82
|
-
image: {
|
|
83
|
-
sourceUrl: string;
|
|
84
|
-
altText: string;
|
|
85
|
-
width?: number | undefined;
|
|
86
|
-
height?: number | undefined;
|
|
87
|
-
};
|
|
88
|
-
options?: {
|
|
89
|
-
identifier: {
|
|
90
|
-
key: string;
|
|
91
|
-
};
|
|
92
|
-
name: string;
|
|
93
|
-
value: {
|
|
94
|
-
identifier: {
|
|
95
|
-
option: {
|
|
96
|
-
key: string;
|
|
97
|
-
};
|
|
98
|
-
key: string;
|
|
99
|
-
};
|
|
100
|
-
label: string;
|
|
101
|
-
};
|
|
102
|
-
} | undefined;
|
|
103
|
-
}[];
|
|
104
|
-
}[];
|
|
105
|
-
facets: {
|
|
106
|
-
identifier: {
|
|
107
|
-
key: string;
|
|
108
|
-
};
|
|
109
|
-
name: string;
|
|
110
|
-
values: {
|
|
111
|
-
identifier: {
|
|
112
|
-
facet: {
|
|
113
|
-
key: string;
|
|
114
|
-
};
|
|
115
|
-
key: string;
|
|
116
|
-
};
|
|
117
|
-
name: string;
|
|
118
|
-
count: number;
|
|
119
|
-
active: boolean;
|
|
120
|
-
}[];
|
|
53
|
+
variants: ProductSearchResultItemVariant[];
|
|
121
54
|
}[];
|
|
55
|
+
facets: ProductSearchResultFacet[];
|
|
122
56
|
};
|
|
123
57
|
protected parseFacet(facetIdentifier: FacetIdentifier, facetValues: Record<string, number>): ProductSearchResultFacet;
|
|
124
58
|
protected parseFacetValue(facetValueIdentifier: FacetValueIdentifier, label: string, count: number): ProductSearchResultFacetValue;
|
package/src/test/test-utils.d.ts
DELETED
package/test/test-utils.js
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
function createAnonymousTestSession() {
|
|
2
|
-
return {
|
|
3
|
-
id: "test-session-id",
|
|
4
|
-
identity: {
|
|
5
|
-
type: "Anonymous",
|
|
6
|
-
meta: {
|
|
7
|
-
cache: { hit: false, key: "" },
|
|
8
|
-
placeholder: false
|
|
9
|
-
},
|
|
10
|
-
id: { userId: "anonymous" },
|
|
11
|
-
token: void 0,
|
|
12
|
-
issued: /* @__PURE__ */ new Date(),
|
|
13
|
-
expiry: new Date((/* @__PURE__ */ new Date()).getTime() + 3600 * 1e3),
|
|
14
|
-
logonId: "",
|
|
15
|
-
createdAt: "",
|
|
16
|
-
updatedAt: "",
|
|
17
|
-
keyring: [],
|
|
18
|
-
currentService: void 0
|
|
19
|
-
},
|
|
20
|
-
languageContext: {
|
|
21
|
-
locale: "en-US",
|
|
22
|
-
currencyCode: "USD",
|
|
23
|
-
countryCode: "US"
|
|
24
|
-
},
|
|
25
|
-
storeIdentifier: {
|
|
26
|
-
key: "the-good-store"
|
|
27
|
-
}
|
|
28
|
-
};
|
|
29
|
-
}
|
|
30
|
-
export {
|
|
31
|
-
createAnonymousTestSession
|
|
32
|
-
};
|