@usereactify/search 3.2.0 → 3.3.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/README.md CHANGED
@@ -38,4 +38,9 @@ npm run storybook
38
38
 
39
39
  # release a version
40
40
  npm run release
41
+
42
+ # release a beta version
43
+ npm run release -- --prerelease beta
44
+
45
+ npm publish --tag beta
41
46
  ```
@@ -5,6 +5,7 @@ export * from "./useConfig";
5
5
  export * from "./useSearch";
6
6
  export * from "./useFilters";
7
7
  export * from "./useCuration";
8
+ export * from "./useAnalytics";
8
9
  export * from "./useCollection";
9
10
  export * from "./useFilterStack";
10
11
  export * from "./useProductPrice";
@@ -17,6 +17,7 @@ __exportStar(require("./useConfig"), exports);
17
17
  __exportStar(require("./useSearch"), exports);
18
18
  __exportStar(require("./useFilters"), exports);
19
19
  __exportStar(require("./useCuration"), exports);
20
+ __exportStar(require("./useAnalytics"), exports);
20
21
  __exportStar(require("./useCollection"), exports);
21
22
  __exportStar(require("./useFilterStack"), exports);
22
23
  __exportStar(require("./useProductPrice"), exports);
@@ -33,8 +33,15 @@ const useReactiveDataSearchProps = (props = {}) => {
33
33
  return;
34
34
  submitSearch();
35
35
  }, [submitSearch]);
36
+ const { track } = (0, __1.useAnalytics)();
37
+ const trackQuery = react_1.default.useCallback(() => {
38
+ if (!searchQuery)
39
+ return;
40
+ track({ eventName: "search", payload: { searchTerm: searchQuery } });
41
+ }, [searchQuery, track]);
36
42
  const { run: runDebouncedTriggerQuery, cancel: cancelDebouncedTriggerQuery } = (0, ahooks_1.useDebounceFn)((triggerQuery) => {
37
43
  triggerQuery();
44
+ trackQuery();
38
45
  }, {
39
46
  wait: debounce !== null && debounce !== void 0 ? debounce : 300,
40
47
  });
@@ -0,0 +1,28 @@
1
+ export declare const useAnalytics: () => {
2
+ track: (event: TrackEvent) => Promise<import("axios").AxiosResponse<any, any> | undefined>;
3
+ };
4
+ export declare type TrackEvent = TrackEvent.SearchEvent | TrackEvent.ClickProductEvent;
5
+ export declare namespace TrackEvent {
6
+ interface SearchEvent {
7
+ eventName: "search";
8
+ payload: SearchEvent.Payload;
9
+ }
10
+ namespace SearchEvent {
11
+ interface Payload {
12
+ searchTerm: string;
13
+ }
14
+ }
15
+ interface ClickProductEvent {
16
+ eventName: "clickProduct";
17
+ payload: ClickProductEvent.Payload;
18
+ }
19
+ namespace ClickProductEvent {
20
+ interface Payload {
21
+ elasticProduct: ElasticProduct;
22
+ }
23
+ }
24
+ interface ElasticProduct {
25
+ id: number;
26
+ title: string;
27
+ }
28
+ }
@@ -0,0 +1,54 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.useAnalytics = void 0;
16
+ const axios_1 = __importDefault(require("axios"));
17
+ const provider_1 = require("../provider");
18
+ const useAnalytics = () => {
19
+ const { shopifyPermanentDomain } = (0, provider_1.useContext)();
20
+ const url = "https://analytics.search.reactify.app/record/";
21
+ const track = (event) => __awaiter(void 0, void 0, void 0, function* () {
22
+ if (!shopifyPermanentDomain) {
23
+ console.warn(new Error('Unable to send tracking event, missing value for "shopifyPermanentDomain".'));
24
+ return;
25
+ }
26
+ const events = getTrackEvents(event);
27
+ return axios_1.default.post(url, { events }, { headers: { "X-Reactify-Tenant": shopifyPermanentDomain } });
28
+ });
29
+ return { track };
30
+ };
31
+ exports.useAnalytics = useAnalytics;
32
+ function getTrackEvents(event) {
33
+ const { eventName } = event;
34
+ let events = [];
35
+ switch (eventName) {
36
+ case "search":
37
+ events = [
38
+ {
39
+ eventName,
40
+ payload: event.payload,
41
+ },
42
+ ];
43
+ break;
44
+ case "clickProduct":
45
+ events = [
46
+ {
47
+ eventName,
48
+ payload: event.payload,
49
+ },
50
+ ];
51
+ break;
52
+ }
53
+ return events;
54
+ }
@@ -3,6 +3,7 @@ import type { Config, ConfigSort, ConfigFilter, ConfigCuration } from "./types/c
3
3
  declare type Context = {
4
4
  index: string;
5
5
  config: Config;
6
+ shopifyPermanentDomain: string;
6
7
  sortOption: ConfigSort;
7
8
  sortOptions: ConfigSort[];
8
9
  searchQuery?: string;
@@ -24,6 +25,7 @@ declare const Context: React.Context<Context | undefined>;
24
25
  declare type Props = {
25
26
  index: string;
26
27
  config: Config;
28
+ shopifyPermanentDomain: string;
27
29
  filterStackId?: string;
28
30
  collection?: Collection;
29
31
  noReactiveBase?: boolean;
package/dist/provider.js CHANGED
@@ -15,7 +15,7 @@ const defaultCredentials = {
15
15
  };
16
16
  const Provider = (props) => {
17
17
  var _a, _b;
18
- const { index, config, children, collection, instantSearch, filterStackId, noReactiveBase, additionalComponentIds, } = props;
18
+ const { index, config, shopifyPermanentDomain, children, collection, instantSearch, filterStackId, noReactiveBase, additionalComponentIds, } = props;
19
19
  const credentials = (_a = props.credentials) !== null && _a !== void 0 ? _a : defaultCredentials;
20
20
  const theme = (_b = props.theme) !== null && _b !== void 0 ? _b : {
21
21
  typography: {
@@ -64,6 +64,7 @@ const Provider = (props) => {
64
64
  const contextValue = react_1.default.useMemo(() => ({
65
65
  index,
66
66
  config,
67
+ shopifyPermanentDomain,
67
68
  collection,
68
69
  credentials,
69
70
  searchQuery,
@@ -83,6 +84,7 @@ const Provider = (props) => {
83
84
  index,
84
85
  config,
85
86
  curation,
87
+ shopifyPermanentDomain,
86
88
  collection,
87
89
  credentials,
88
90
  searchQuery,
@@ -24,8 +24,20 @@ const ResultCard = (_a) => {
24
24
  if (render)
25
25
  return render(Object.assign(Object.assign({}, props), productPrice));
26
26
  const { formattedPrice, formattedCompareAtPrice, onSale } = productPrice;
27
+ const { track } = (0, hooks_1.useAnalytics)();
28
+ const handleClick = react_1.default.useCallback(() => {
29
+ track({
30
+ eventName: "clickProduct",
31
+ payload: {
32
+ elasticProduct: {
33
+ id: product.id,
34
+ title: product.title,
35
+ },
36
+ },
37
+ });
38
+ }, [track, product]);
27
39
  return (react_1.default.createElement("article", null,
28
- react_1.default.createElement("a", { href: `/products/${product.handle}` },
40
+ react_1.default.createElement("a", { onClick: handleClick, href: `/products/${product.handle}` },
29
41
  product.image && react_1.default.createElement("img", { src: product.image, width: "100%" }),
30
42
  product.title),
31
43
  react_1.default.createElement("span", null, formattedPrice),
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@usereactify/search",
3
3
  "description": "React UI library for Reactify Search",
4
- "version": "3.2.0",
4
+ "version": "3.3.0",
5
5
  "license": "MIT",
6
6
  "main": "dist/index.js",
7
7
  "types": "dist/index.d.ts",
@@ -30,6 +30,7 @@
30
30
  },
31
31
  "dependencies": {
32
32
  "ahooks": "2.10.11",
33
+ "axios": "0.26.1",
33
34
  "currency.js": "2.0.4",
34
35
  "debug": "4.3.2"
35
36
  },