@xyo-network/react-advertising 4.1.5

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.
Files changed (36) hide show
  1. package/LICENSE +165 -0
  2. package/README.md +13 -0
  3. package/dist/browser/index.d.ts +2 -0
  4. package/dist/browser/index.d.ts.map +1 -0
  5. package/dist/browser/index.mjs +131 -0
  6. package/dist/browser/index.mjs.map +1 -0
  7. package/dist/browser/utm/hooks/index.d.ts +4 -0
  8. package/dist/browser/utm/hooks/index.d.ts.map +1 -0
  9. package/dist/browser/utm/hooks/useCaptureUtmLocation.d.ts +6 -0
  10. package/dist/browser/utm/hooks/useCaptureUtmLocation.d.ts.map +1 -0
  11. package/dist/browser/utm/hooks/useDefaultPageState.d.ts +10 -0
  12. package/dist/browser/utm/hooks/useDefaultPageState.d.ts.map +1 -0
  13. package/dist/browser/utm/hooks/useDefaultUtmPageState.d.ts +10 -0
  14. package/dist/browser/utm/hooks/useDefaultUtmPageState.d.ts.map +1 -0
  15. package/dist/browser/utm/index.d.ts +3 -0
  16. package/dist/browser/utm/index.d.ts.map +1 -0
  17. package/dist/browser/utm/lib/PageStateKeys.d.ts +2 -0
  18. package/dist/browser/utm/lib/PageStateKeys.d.ts.map +1 -0
  19. package/dist/browser/utm/lib/UtmStorageArchivist.d.ts +30 -0
  20. package/dist/browser/utm/lib/UtmStorageArchivist.d.ts.map +1 -0
  21. package/dist/browser/utm/lib/index.d.ts +3 -0
  22. package/dist/browser/utm/lib/index.d.ts.map +1 -0
  23. package/package.json +69 -0
  24. package/src/index.ts +1 -0
  25. package/src/types/images.d.ts +5 -0
  26. package/src/utm/hooks/UseCaptureUtmLocation.stories.tsx +54 -0
  27. package/src/utm/hooks/index.ts +3 -0
  28. package/src/utm/hooks/useCaptureUtmLocation.ts +38 -0
  29. package/src/utm/hooks/useDefaultPageState.ts +56 -0
  30. package/src/utm/hooks/useDefaultUtmPageState.ts +14 -0
  31. package/src/utm/index.ts +2 -0
  32. package/src/utm/lib/PageStateKeys.ts +1 -0
  33. package/src/utm/lib/UtmStorageArchivist.ts +29 -0
  34. package/src/utm/lib/index.ts +2 -0
  35. package/typedoc.json +5 -0
  36. package/xy.config.ts +10 -0
package/LICENSE ADDED
@@ -0,0 +1,165 @@
1
+ GNU LESSER GENERAL PUBLIC LICENSE
2
+ Version 3, 29 June 2007
3
+
4
+ Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
5
+ Everyone is permitted to copy and distribute verbatim copies
6
+ of this license document, but changing it is not allowed.
7
+
8
+
9
+ This version of the GNU Lesser General Public License incorporates
10
+ the terms and conditions of version 3 of the GNU General Public
11
+ License, supplemented by the additional permissions listed below.
12
+
13
+ 0. Additional Definitions.
14
+
15
+ As used herein, "this License" refers to version 3 of the GNU Lesser
16
+ General Public License, and the "GNU GPL" refers to version 3 of the GNU
17
+ General Public License.
18
+
19
+ "The Library" refers to a covered work governed by this License,
20
+ other than an Application or a Combined Work as defined below.
21
+
22
+ An "Application" is any work that makes use of an interface provided
23
+ by the Library, but which is not otherwise based on the Library.
24
+ Defining a subclass of a class defined by the Library is deemed a mode
25
+ of using an interface provided by the Library.
26
+
27
+ A "Combined Work" is a work produced by combining or linking an
28
+ Application with the Library. The particular version of the Library
29
+ with which the Combined Work was made is also called the "Linked
30
+ Version".
31
+
32
+ The "Minimal Corresponding Source" for a Combined Work means the
33
+ Corresponding Source for the Combined Work, excluding any source code
34
+ for portions of the Combined Work that, considered in isolation, are
35
+ based on the Application, and not on the Linked Version.
36
+
37
+ The "Corresponding Application Code" for a Combined Work means the
38
+ object code and/or source code for the Application, including any data
39
+ and utility programs needed for reproducing the Combined Work from the
40
+ Application, but excluding the System Libraries of the Combined Work.
41
+
42
+ 1. Exception to Section 3 of the GNU GPL.
43
+
44
+ You may convey a covered work under sections 3 and 4 of this License
45
+ without being bound by section 3 of the GNU GPL.
46
+
47
+ 2. Conveying Modified Versions.
48
+
49
+ If you modify a copy of the Library, and, in your modifications, a
50
+ facility refers to a function or data to be supplied by an Application
51
+ that uses the facility (other than as an argument passed when the
52
+ facility is invoked), then you may convey a copy of the modified
53
+ version:
54
+
55
+ a) under this License, provided that you make a good faith effort to
56
+ ensure that, in the event an Application does not supply the
57
+ function or data, the facility still operates, and performs
58
+ whatever part of its purpose remains meaningful, or
59
+
60
+ b) under the GNU GPL, with none of the additional permissions of
61
+ this License applicable to that copy.
62
+
63
+ 3. Object Code Incorporating Material from Library Header Files.
64
+
65
+ The object code form of an Application may incorporate material from
66
+ a header file that is part of the Library. You may convey such object
67
+ code under terms of your choice, provided that, if the incorporated
68
+ material is not limited to numerical parameters, data structure
69
+ layouts and accessors, or small macros, inline functions and templates
70
+ (ten or fewer lines in length), you do both of the following:
71
+
72
+ a) Give prominent notice with each copy of the object code that the
73
+ Library is used in it and that the Library and its use are
74
+ covered by this License.
75
+
76
+ b) Accompany the object code with a copy of the GNU GPL and this license
77
+ document.
78
+
79
+ 4. Combined Works.
80
+
81
+ You may convey a Combined Work under terms of your choice that,
82
+ taken together, effectively do not restrict modification of the
83
+ portions of the Library contained in the Combined Work and reverse
84
+ engineering for debugging such modifications, if you also do each of
85
+ the following:
86
+
87
+ a) Give prominent notice with each copy of the Combined Work that
88
+ the Library is used in it and that the Library and its use are
89
+ covered by this License.
90
+
91
+ b) Accompany the Combined Work with a copy of the GNU GPL and this license
92
+ document.
93
+
94
+ c) For a Combined Work that displays copyright notices during
95
+ execution, include the copyright notice for the Library among
96
+ these notices, as well as a reference directing the user to the
97
+ copies of the GNU GPL and this license document.
98
+
99
+ d) Do one of the following:
100
+
101
+ 0) Convey the Minimal Corresponding Source under the terms of this
102
+ License, and the Corresponding Application Code in a form
103
+ suitable for, and under terms that permit, the user to
104
+ recombine or relink the Application with a modified version of
105
+ the Linked Version to produce a modified Combined Work, in the
106
+ manner specified by section 6 of the GNU GPL for conveying
107
+ Corresponding Source.
108
+
109
+ 1) Use a suitable shared library mechanism for linking with the
110
+ Library. A suitable mechanism is one that (a) uses at run time
111
+ a copy of the Library already present on the user's computer
112
+ system, and (b) will operate properly with a modified version
113
+ of the Library that is interface-compatible with the Linked
114
+ Version.
115
+
116
+ e) Provide Installation Information, but only if you would otherwise
117
+ be required to provide such information under section 6 of the
118
+ GNU GPL, and only to the extent that such information is
119
+ necessary to install and execute a modified version of the
120
+ Combined Work produced by recombining or relinking the
121
+ Application with a modified version of the Linked Version. (If
122
+ you use option 4d0, the Installation Information must accompany
123
+ the Minimal Corresponding Source and Corresponding Application
124
+ Code. If you use option 4d1, you must provide the Installation
125
+ Information in the manner specified by section 6 of the GNU GPL
126
+ for conveying Corresponding Source.)
127
+
128
+ 5. Combined Libraries.
129
+
130
+ You may place library facilities that are a work based on the
131
+ Library side by side in a single library together with other library
132
+ facilities that are not Applications and are not covered by this
133
+ License, and convey such a combined library under terms of your
134
+ choice, if you do both of the following:
135
+
136
+ a) Accompany the combined library with a copy of the same work based
137
+ on the Library, uncombined with any other library facilities,
138
+ conveyed under the terms of this License.
139
+
140
+ b) Give prominent notice with the combined library that part of it
141
+ is a work based on the Library, and explaining where to find the
142
+ accompanying uncombined form of the same work.
143
+
144
+ 6. Revised Versions of the GNU Lesser General Public License.
145
+
146
+ The Free Software Foundation may publish revised and/or new versions
147
+ of the GNU Lesser General Public License from time to time. Such new
148
+ versions will be similar in spirit to the present version, but may
149
+ differ in detail to address new problems or concerns.
150
+
151
+ Each version is given a distinguishing version number. If the
152
+ Library as you received it specifies that a certain numbered version
153
+ of the GNU Lesser General Public License "or any later version"
154
+ applies to it, you have the option of following the terms and
155
+ conditions either of that published version or of any later version
156
+ published by the Free Software Foundation. If the Library as you
157
+ received it does not specify a version number of the GNU Lesser
158
+ General Public License, you may choose any version of the GNU Lesser
159
+ General Public License ever published by the Free Software Foundation.
160
+
161
+ If the Library as you received it specifies that a proxy can decide
162
+ whether future versions of the GNU Lesser General Public License shall
163
+ apply, that proxy's public statement of acceptance of any version is
164
+ permanent authorization for you to choose that version for the
165
+ Library.
package/README.md ADDED
@@ -0,0 +1,13 @@
1
+ [![logo][]](https://xyo.network)
2
+
3
+ Part of [@xyo-network/sdk-xyo-react](https://www.npmjs.com/package/@xyo-network/sdk-xyo-react)
4
+
5
+ ## License
6
+
7
+ > See the [LICENSE](LICENSE) file for license details
8
+
9
+ ## Credits
10
+
11
+ [Made with 🔥 and ❄️ by XYO](https://xyo.network)
12
+
13
+ [logo]: https://cdn.xy.company/img/brand/XYO_full_colored.png
@@ -0,0 +1,2 @@
1
+ export * from './utm/index.ts';
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAA"}
@@ -0,0 +1,131 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
+
4
+ // src/utm/hooks/useCaptureUtmLocation.ts
5
+ import { usePromise } from "@xylabs/react-promise";
6
+ import { UtmSchema } from "@xyo-network/advertising-payload-plugins";
7
+ import { useMemo as useMemo2 } from "react";
8
+
9
+ // src/utm/lib/UtmStorageArchivist.ts
10
+ import { isUtm } from "@xyo-network/advertising-payload-plugins";
11
+ import { StorageArchivist, StorageArchivistConfigSchema } from "@xyo-network/archivist-storage";
12
+ var STORAGE_NAME_SPACE = "utm";
13
+ var archivist;
14
+ var UtmStorageArchivist = /* @__PURE__ */ __name(async () => {
15
+ if (archivist) {
16
+ return archivist;
17
+ } else {
18
+ archivist = await StorageArchivist.create({
19
+ account: "random",
20
+ config: {
21
+ schema: StorageArchivistConfigSchema,
22
+ namespace: STORAGE_NAME_SPACE,
23
+ type: "session"
24
+ }
25
+ });
26
+ return archivist;
27
+ }
28
+ }, "UtmStorageArchivist");
29
+ var LatestUtmPayload = /* @__PURE__ */ __name(async () => {
30
+ const archivist2 = await UtmStorageArchivist();
31
+ return (await archivist2.all()).findLast(isUtm);
32
+ }, "LatestUtmPayload");
33
+
34
+ // src/utm/hooks/useDefaultPageState.ts
35
+ import { useMemo } from "react";
36
+ import { useSearchParams } from "react-router-dom";
37
+ var useDefaultPageState = /* @__PURE__ */ __name(({ queryParams }) => {
38
+ const [searchParams, setSearchParams] = useSearchParams();
39
+ const patchDefaultPageState = /* @__PURE__ */ __name((source, key, value) => {
40
+ switch (source) {
41
+ case "queryParams": {
42
+ setSearchParams((prevSearchParams) => {
43
+ const newSearchParams = new URLSearchParams(prevSearchParams);
44
+ newSearchParams.set(key, value);
45
+ return newSearchParams;
46
+ });
47
+ break;
48
+ }
49
+ default: {
50
+ throw new Error(`Invalid source for default page state: ${source}`);
51
+ }
52
+ }
53
+ }, "patchDefaultPageState");
54
+ const queryParamsState = useMemo(() => {
55
+ if (!queryParams) return {};
56
+ let defaultState = {};
57
+ for (const [key, value] of searchParams.entries()) {
58
+ if (queryParams.includes(key)) {
59
+ defaultState[key] = value;
60
+ }
61
+ }
62
+ return defaultState;
63
+ }, [
64
+ searchParams,
65
+ queryParams
66
+ ]);
67
+ return [
68
+ queryParamsState,
69
+ patchDefaultPageState
70
+ ];
71
+ }, "useDefaultPageState");
72
+
73
+ // src/utm/hooks/useDefaultUtmPageState.ts
74
+ var UtmQueryParamStrings = {
75
+ utmCampaign: "utm_campaign",
76
+ utmContent: "utm_content",
77
+ utmMedium: "utm_medium",
78
+ utmSource: "utm_source",
79
+ utmTerm: "utm_term"
80
+ };
81
+ var useDefaultUtmPageState = /* @__PURE__ */ __name(() => useDefaultPageState({
82
+ queryParams: Object.values(UtmQueryParamStrings)
83
+ }), "useDefaultUtmPageState");
84
+
85
+ // src/utm/hooks/useCaptureUtmLocation.ts
86
+ var useCaptureUtmLocation = /* @__PURE__ */ __name(() => {
87
+ const [{ utm_campaign, utm_content, utm_medium, utm_source, utm_term }] = useDefaultUtmPageState();
88
+ const [sessionStorageArchivist, error] = usePromise(async () => {
89
+ return await UtmStorageArchivist();
90
+ }, []);
91
+ const utmPayload = useMemo2(() => {
92
+ const utm = {
93
+ schema: UtmSchema
94
+ };
95
+ if (utm_campaign) utm.utm_campaign = utm_campaign;
96
+ if (utm_content) utm.utm_content = utm_content;
97
+ if (utm_medium) utm.utm_medium = utm_medium;
98
+ if (utm_source) utm.utm_source = utm_source;
99
+ if (utm_term) utm.utm_term = utm_term;
100
+ return utm;
101
+ }, [
102
+ utm_campaign,
103
+ utm_content,
104
+ utm_medium,
105
+ utm_source,
106
+ utm_term
107
+ ]);
108
+ const [, insertError] = usePromise(async () => {
109
+ if (utmPayload && sessionStorageArchivist) {
110
+ await sessionStorageArchivist.insert([
111
+ utmPayload
112
+ ]);
113
+ }
114
+ }, [
115
+ sessionStorageArchivist,
116
+ utmPayload
117
+ ]);
118
+ return {
119
+ utmPayload,
120
+ error: error ?? insertError
121
+ };
122
+ }, "useCaptureUtmLocation");
123
+ export {
124
+ LatestUtmPayload,
125
+ UtmQueryParamStrings,
126
+ UtmStorageArchivist,
127
+ useCaptureUtmLocation,
128
+ useDefaultPageState,
129
+ useDefaultUtmPageState
130
+ };
131
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/utm/hooks/useCaptureUtmLocation.ts","../../src/utm/lib/UtmStorageArchivist.ts","../../src/utm/hooks/useDefaultPageState.ts","../../src/utm/hooks/useDefaultUtmPageState.ts"],"sourcesContent":["import { usePromise } from '@xylabs/react-promise'\nimport { type Utm, UtmSchema } from '@xyo-network/advertising-payload-plugins'\nimport { useMemo } from 'react'\n\nimport { UtmStorageArchivist } from '../lib/index.ts'\nimport { useDefaultUtmPageState } from './useDefaultUtmPageState.ts'\n\nexport const useCaptureUtmLocation = () => {\n const [{\n utm_campaign, utm_content, utm_medium, utm_source, utm_term,\n }] = useDefaultUtmPageState()\n\n const [sessionStorageArchivist, error] = usePromise(async () => {\n return await UtmStorageArchivist()\n }, [])\n\n const utmPayload = useMemo(() => {\n // Construct the base UTM payload\n const utm: Utm = { schema: UtmSchema }\n\n // Conditionally add all the UTM parameters present\n if (utm_campaign) utm.utm_campaign = utm_campaign\n if (utm_content) utm.utm_content = utm_content\n if (utm_medium) utm.utm_medium = utm_medium\n if (utm_source) utm.utm_source = utm_source\n if (utm_term) utm.utm_term = utm_term\n\n return utm\n }, [utm_campaign, utm_content, utm_medium, utm_source, utm_term])\n\n const [,insertError] = usePromise(async () => {\n if (utmPayload && sessionStorageArchivist) {\n await sessionStorageArchivist.insert([utmPayload])\n }\n }, [sessionStorageArchivist, utmPayload])\n\n return { utmPayload, error: error ?? insertError }\n}\n","import { isUtm } from '@xyo-network/advertising-payload-plugins'\nimport { StorageArchivist, StorageArchivistConfigSchema } from '@xyo-network/archivist-storage'\n\nconst STORAGE_NAME_SPACE = 'utm' as const\n\nlet archivist: StorageArchivist | undefined\n\n/**\n * Find or create a single instance of the UtmStorageArchivist\n * @returns Archivist instance\n */\nexport const UtmStorageArchivist = async () => {\n if (archivist) {\n return archivist\n } else {\n archivist = await StorageArchivist.create({\n account: 'random',\n config: {\n schema: StorageArchivistConfigSchema, namespace: STORAGE_NAME_SPACE, type: 'session',\n },\n })\n return archivist\n }\n}\n\nexport const LatestUtmPayload = async () => {\n const archivist = await UtmStorageArchivist()\n return (await archivist.all()).findLast(isUtm)\n}\n","import { useMemo } from 'react'\nimport { useSearchParams } from 'react-router-dom'\n\nimport type { PageStateKeys } from '../lib/index.ts'\n\nexport interface DefaultPageStateParams<TQueryParams extends PageStateKeys = PageStateKeys> {\n queryParams: TQueryParams\n}\n\nexport type DefaultPageStateReturn<TPageStateKeys extends PageStateKeys = PageStateKeys> = [\n Record<TPageStateKeys[number], string>,\n (source: keyof DefaultPageStateParams, key: TPageStateKeys[number], value: string) => void,\n]\n\n/**\n * Hook to get default page state as key value pairs from various sources. It provides a convenient abstraction over free form\n * page state like query params by narrowing the used params and centralizing their logic.\n *\n * Future work could include adding support for localStorage, sessionStorage, and precedence of sources.\n * @param defaultPageStateParams various sources of default page state (i.e. queryParams, localStorage, etc.)\n * @returns\n */\nexport const useDefaultPageState = <TQueryParams extends PageStateKeys = PageStateKeys>(\n { queryParams }: DefaultPageStateParams<TQueryParams>,\n): DefaultPageStateReturn<TQueryParams> => {\n const [searchParams, setSearchParams] = useSearchParams()\n\n const patchDefaultPageState = (source: keyof DefaultPageStateParams, key: TQueryParams[number], value: string) => {\n switch (source) {\n case 'queryParams': {\n setSearchParams((prevSearchParams) => {\n const newSearchParams = new URLSearchParams(prevSearchParams)\n newSearchParams.set(key, value)\n return newSearchParams\n })\n break\n }\n default: {\n throw new Error(`Invalid source for default page state: ${source}`)\n }\n }\n }\n\n const queryParamsState = useMemo(() => {\n if (!queryParams) return {} as Record<TQueryParams[number], string>\n let defaultState = {} as Record<TQueryParams[number], string>\n for (const [key, value] of searchParams.entries()) {\n if (queryParams.includes(key)) {\n defaultState[key as TQueryParams[number]] = value\n }\n }\n return defaultState\n }, [searchParams, queryParams])\n\n return [queryParamsState, patchDefaultPageState]\n}\n","import { useDefaultPageState } from './useDefaultPageState.ts'\n\nexport const UtmQueryParamStrings = {\n utmCampaign: 'utm_campaign',\n utmContent: 'utm_content',\n utmMedium: 'utm_medium',\n utmSource: 'utm_source',\n utmTerm: 'utm_term',\n} as const\n\nexport type UtmQueryParams = typeof UtmQueryParamStrings[keyof typeof UtmQueryParamStrings]\n\n/** Get the default page state for utm values from specific query params */\nexport const useDefaultUtmPageState = () => useDefaultPageState<UtmQueryParams[]>({ queryParams: Object.values(UtmQueryParamStrings) })\n"],"mappings":";;;;AAAA,SAASA,kBAAkB;AAC3B,SAAmBC,iBAAiB;AACpC,SAASC,WAAAA,gBAAe;;;ACFxB,SAASC,aAAa;AACtB,SAASC,kBAAkBC,oCAAoC;AAE/D,IAAMC,qBAAqB;AAE3B,IAAIC;AAMG,IAAMC,sBAAsB,mCAAA;AACjC,MAAID,WAAW;AACb,WAAOA;EACT,OAAO;AACLA,gBAAY,MAAME,iBAAiBC,OAAO;MACxCC,SAAS;MACTC,QAAQ;QACNC,QAAQC;QAA8BC,WAAWT;QAAoBU,MAAM;MAC7E;IACF,CAAA;AACA,WAAOT;EACT;AACF,GAZmC;AAc5B,IAAMU,mBAAmB,mCAAA;AAC9B,QAAMV,aAAY,MAAMC,oBAAAA;AACxB,UAAQ,MAAMD,WAAUW,IAAG,GAAIC,SAASC,KAAAA;AAC1C,GAHgC;;;ACzBhC,SAASC,eAAe;AACxB,SAASC,uBAAuB;AAqBzB,IAAMC,sBAAsB,wBACjC,EAAEC,YAAW,MAAwC;AAErD,QAAM,CAACC,cAAcC,eAAAA,IAAmBC,gBAAAA;AAExC,QAAMC,wBAAwB,wBAACC,QAAsCC,KAA2BC,UAAAA;AAC9F,YAAQF,QAAAA;MACN,KAAK,eAAe;AAClBH,wBAAgB,CAACM,qBAAAA;AACf,gBAAMC,kBAAkB,IAAIC,gBAAgBF,gBAAAA;AAC5CC,0BAAgBE,IAAIL,KAAKC,KAAAA;AACzB,iBAAOE;QACT,CAAA;AACA;MACF;MACA,SAAS;AACP,cAAM,IAAIG,MAAM,0CAA0CP,MAAAA,EAAQ;MACpE;IACF;EACF,GAd8B;AAgB9B,QAAMQ,mBAAmBC,QAAQ,MAAA;AAC/B,QAAI,CAACd,YAAa,QAAO,CAAC;AAC1B,QAAIe,eAAe,CAAC;AACpB,eAAW,CAACT,KAAKC,KAAAA,KAAUN,aAAae,QAAO,GAAI;AACjD,UAAIhB,YAAYiB,SAASX,GAAAA,GAAM;AAC7BS,qBAAaT,GAAAA,IAA+BC;MAC9C;IACF;AACA,WAAOQ;EACT,GAAG;IAACd;IAAcD;GAAY;AAE9B,SAAO;IAACa;IAAkBT;;AAC5B,GAjCmC;;;ACpB5B,IAAMc,uBAAuB;EAClCC,aAAa;EACbC,YAAY;EACZC,WAAW;EACXC,WAAW;EACXC,SAAS;AACX;AAKO,IAAMC,yBAAyB,6BAAMC,oBAAsC;EAAEC,aAAaC,OAAOC,OAAOV,oBAAAA;AAAsB,CAAA,GAA/F;;;AHN/B,IAAMW,wBAAwB,6BAAA;AACnC,QAAM,CAAC,EACLC,cAAcC,aAAaC,YAAYC,YAAYC,SAAQ,CAC5D,IAAIC,uBAAAA;AAEL,QAAM,CAACC,yBAAyBC,KAAAA,IAASC,WAAW,YAAA;AAClD,WAAO,MAAMC,oBAAAA;EACf,GAAG,CAAA,CAAE;AAEL,QAAMC,aAAaC,SAAQ,MAAA;AAEzB,UAAMC,MAAW;MAAEC,QAAQC;IAAU;AAGrC,QAAId,aAAcY,KAAIZ,eAAeA;AACrC,QAAIC,YAAaW,KAAIX,cAAcA;AACnC,QAAIC,WAAYU,KAAIV,aAAaA;AACjC,QAAIC,WAAYS,KAAIT,aAAaA;AACjC,QAAIC,SAAUQ,KAAIR,WAAWA;AAE7B,WAAOQ;EACT,GAAG;IAACZ;IAAcC;IAAaC;IAAYC;IAAYC;GAAS;AAEhE,QAAM,CAAA,EAAEW,WAAAA,IAAeP,WAAW,YAAA;AAChC,QAAIE,cAAcJ,yBAAyB;AACzC,YAAMA,wBAAwBU,OAAO;QAACN;OAAW;IACnD;EACF,GAAG;IAACJ;IAAyBI;GAAW;AAExC,SAAO;IAAEA;IAAYH,OAAOA,SAASQ;EAAY;AACnD,GA9BqC;","names":["usePromise","UtmSchema","useMemo","isUtm","StorageArchivist","StorageArchivistConfigSchema","STORAGE_NAME_SPACE","archivist","UtmStorageArchivist","StorageArchivist","create","account","config","schema","StorageArchivistConfigSchema","namespace","type","LatestUtmPayload","all","findLast","isUtm","useMemo","useSearchParams","useDefaultPageState","queryParams","searchParams","setSearchParams","useSearchParams","patchDefaultPageState","source","key","value","prevSearchParams","newSearchParams","URLSearchParams","set","Error","queryParamsState","useMemo","defaultState","entries","includes","UtmQueryParamStrings","utmCampaign","utmContent","utmMedium","utmSource","utmTerm","useDefaultUtmPageState","useDefaultPageState","queryParams","Object","values","useCaptureUtmLocation","utm_campaign","utm_content","utm_medium","utm_source","utm_term","useDefaultUtmPageState","sessionStorageArchivist","error","usePromise","UtmStorageArchivist","utmPayload","useMemo","utm","schema","UtmSchema","insertError","insert"]}
@@ -0,0 +1,4 @@
1
+ export * from './useCaptureUtmLocation.ts';
2
+ export * from './useDefaultPageState.ts';
3
+ export * from './useDefaultUtmPageState.ts';
4
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/utm/hooks/index.ts"],"names":[],"mappings":"AAAA,cAAc,4BAA4B,CAAA;AAC1C,cAAc,0BAA0B,CAAA;AACxC,cAAc,6BAA6B,CAAA"}
@@ -0,0 +1,6 @@
1
+ import { type Utm } from '@xyo-network/advertising-payload-plugins';
2
+ export declare const useCaptureUtmLocation: () => {
3
+ utmPayload: Utm;
4
+ error: Error | undefined;
5
+ };
6
+ //# sourceMappingURL=useCaptureUtmLocation.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useCaptureUtmLocation.d.ts","sourceRoot":"","sources":["../../../../src/utm/hooks/useCaptureUtmLocation.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,GAAG,EAAa,MAAM,0CAA0C,CAAA;AAM9E,eAAO,MAAM,qBAAqB;;;CA8BjC,CAAA"}
@@ -0,0 +1,10 @@
1
+ import type { PageStateKeys } from '../lib/index.ts';
2
+ export interface DefaultPageStateParams<TQueryParams extends PageStateKeys = PageStateKeys> {
3
+ queryParams: TQueryParams;
4
+ }
5
+ export type DefaultPageStateReturn<TPageStateKeys extends PageStateKeys = PageStateKeys> = [
6
+ Record<TPageStateKeys[number], string>,
7
+ (source: keyof DefaultPageStateParams, key: TPageStateKeys[number], value: string) => void
8
+ ];
9
+ export declare const useDefaultPageState: <TQueryParams extends PageStateKeys = PageStateKeys>({ queryParams }: DefaultPageStateParams<TQueryParams>) => DefaultPageStateReturn<TQueryParams>;
10
+ //# sourceMappingURL=useDefaultPageState.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useDefaultPageState.d.ts","sourceRoot":"","sources":["../../../../src/utm/hooks/useDefaultPageState.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAA;AAEpD,MAAM,WAAW,sBAAsB,CAAC,YAAY,SAAS,aAAa,GAAG,aAAa;IACxF,WAAW,EAAE,YAAY,CAAA;CAC1B;AAED,MAAM,MAAM,sBAAsB,CAAC,cAAc,SAAS,aAAa,GAAG,aAAa,IAAI;IACzF,MAAM,CAAC,cAAc,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC;IACtC,CAAC,MAAM,EAAE,MAAM,sBAAsB,EAAE,GAAG,EAAE,cAAc,CAAC,MAAM,CAAC,EAAE,KAAK,EAAE,MAAM,KAAK,IAAI;CAC3F,CAAA;AAUD,eAAO,MAAM,mBAAmB,GAAI,YAAY,SAAS,aAAa,mCACnD,sBAAsB,CAAC,YAAY,CAAC,KACpD,sBAAsB,CAAC,YAAY,CA+BrC,CAAA"}
@@ -0,0 +1,10 @@
1
+ export declare const UtmQueryParamStrings: {
2
+ readonly utmCampaign: "utm_campaign";
3
+ readonly utmContent: "utm_content";
4
+ readonly utmMedium: "utm_medium";
5
+ readonly utmSource: "utm_source";
6
+ readonly utmTerm: "utm_term";
7
+ };
8
+ export type UtmQueryParams = typeof UtmQueryParamStrings[keyof typeof UtmQueryParamStrings];
9
+ export declare const useDefaultUtmPageState: () => import("./useDefaultPageState.ts").DefaultPageStateReturn<UtmQueryParams[]>;
10
+ //# sourceMappingURL=useDefaultUtmPageState.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useDefaultUtmPageState.d.ts","sourceRoot":"","sources":["../../../../src/utm/hooks/useDefaultUtmPageState.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,oBAAoB;;;;;;CAMvB,CAAA;AAEV,MAAM,MAAM,cAAc,GAAG,OAAO,oBAAoB,CAAC,MAAM,OAAO,oBAAoB,CAAC,CAAA;AAG3F,eAAO,MAAM,sBAAsB,mFAAoG,CAAA"}
@@ -0,0 +1,3 @@
1
+ export * from './hooks/index.ts';
2
+ export * from './lib/index.ts';
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/utm/index.ts"],"names":[],"mappings":"AAAA,cAAc,kBAAkB,CAAA;AAChC,cAAc,gBAAgB,CAAA"}
@@ -0,0 +1,2 @@
1
+ export type PageStateKeys = ReadonlyArray<string>;
2
+ //# sourceMappingURL=PageStateKeys.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"PageStateKeys.d.ts","sourceRoot":"","sources":["../../../../src/utm/lib/PageStateKeys.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,aAAa,GAAG,aAAa,CAAC,MAAM,CAAC,CAAA"}
@@ -0,0 +1,30 @@
1
+ import { StorageArchivist, StorageArchivistConfigSchema } from '@xyo-network/archivist-storage';
2
+ export declare const UtmStorageArchivist: () => Promise<StorageArchivist<import("@xylabs/object").BaseParamsFields & {
3
+ account?: import("@xyo-network/account-model").AccountInstance | "random";
4
+ addToResolvers?: boolean;
5
+ additionalSigners?: import("@xyo-network/account-model").AccountInstance[];
6
+ allowNameResolution?: boolean;
7
+ config: import("@xyo-network/payload-model").SchemaFields & import("@xyo-network/payload-model").PayloadFields & Omit<import("@xyo-network/module-model").ArchivingModuleConfig & import("@xyo-network/module-model").ModuleConfigFields & import("@xyo-network/payload-model").SchemaFields & import("@xyo-network/payload-model").PayloadFields & Omit<import("@xyo-network/module-model").ArchivingModuleConfig & import("@xyo-network/module-model").ModuleConfigFields & {
8
+ parents?: import("@xyo-network/archivist-model").ArchivistParents;
9
+ requireAllParents?: boolean;
10
+ schema: "network.xyo.archivist.storage.config";
11
+ storage?: import("@xyo-network/archivist-model").ArchivistStorage;
12
+ storeParentReads?: boolean;
13
+ } & {
14
+ maxEntries?: number;
15
+ maxEntrySize?: number;
16
+ namespace?: string;
17
+ schema: StorageArchivistConfigSchema;
18
+ type?: "local" | "session" | "page";
19
+ }, "schema"> & {
20
+ schema: "network.xyo.archivist.storage.config";
21
+ }, "schema"> & {
22
+ schema: string;
23
+ };
24
+ ephemeralQueryAccountEnabled?: boolean;
25
+ moduleIdentifierTransformers?: import("@xyo-network/module-model").ModuleIdentifierTransformer[];
26
+ }, import("@xyo-network/archivist-model").ArchivistModuleEventData>>;
27
+ export declare const LatestUtmPayload: () => Promise<import("@xyo-network/payload-model").WithMeta<{
28
+ schema: string;
29
+ } & import("@xyo-network/payload-model").PayloadFields> | undefined>;
30
+ //# sourceMappingURL=UtmStorageArchivist.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"UtmStorageArchivist.d.ts","sourceRoot":"","sources":["../../../../src/utm/lib/UtmStorageArchivist.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,gBAAgB,EAAE,4BAA4B,EAAE,MAAM,gCAAgC,CAAA;AAU/F,eAAO,MAAM,mBAAmB;;;;;;;;;;;;;;;;;;;;;;;;oEAY/B,CAAA;AAED,eAAO,MAAM,gBAAgB;;oEAG5B,CAAA"}
@@ -0,0 +1,3 @@
1
+ export * from './PageStateKeys.ts';
2
+ export * from './UtmStorageArchivist.ts';
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/utm/lib/index.ts"],"names":[],"mappings":"AAAA,cAAc,oBAAoB,CAAA;AAClC,cAAc,0BAA0B,CAAA"}
package/package.json ADDED
@@ -0,0 +1,69 @@
1
+ {
2
+ "name": "@xyo-network/react-advertising",
3
+ "version": "4.1.5",
4
+ "description": "Common React library for all XYO projects that use React",
5
+ "keywords": [
6
+ "xyo",
7
+ "utility",
8
+ "typescript",
9
+ "react"
10
+ ],
11
+ "homepage": "https://xyo.network",
12
+ "bugs": {
13
+ "url": "git+https://github.com/XYOracleNetwork/sdk-xyo-react-js/issues",
14
+ "email": "support@xyo.network"
15
+ },
16
+ "repository": {
17
+ "type": "git",
18
+ "url": "git+https://github.com/XYOracleNetwork/sdk-xyo-react-js.git"
19
+ },
20
+ "license": "LGPL-3.0-only",
21
+ "author": {
22
+ "name": "XYO Development Team",
23
+ "email": "support@xyo.network",
24
+ "url": "https://xyo.network"
25
+ },
26
+ "sideEffects": false,
27
+ "type": "module",
28
+ "exports": {
29
+ ".": {
30
+ "browser": {
31
+ "types": "./dist/browser/index.d.ts",
32
+ "default": "./dist/browser/index.mjs"
33
+ },
34
+ "types": "./dist/browser/index.d.ts",
35
+ "default": "./dist/browser/index.mjs"
36
+ },
37
+ "./package.json": "./package.json"
38
+ },
39
+ "module": "dist/browser/index.mjs",
40
+ "types": "dist/browser/index.d.ts",
41
+ "scripts": {
42
+ "license": "yarn license-checker --exclude \"MIT, ISC, Apache-2.0, BSD, BSD-2-Clause, CC-BY-4.0, Unlicense, CC-BY-3.0, CC0-1.0\"",
43
+ "lint-pkg": "npmPkgJsonLint ."
44
+ },
45
+ "dependencies": {
46
+ "@xylabs/react-promise": "^5.2.2",
47
+ "@xyo-network/advertising-payload-plugins": "^3.1.1",
48
+ "@xyo-network/archivist-storage": "^3.3.0"
49
+ },
50
+ "devDependencies": {
51
+ "@mui/material": "^6.1.4",
52
+ "@storybook/react": "^8.3.6",
53
+ "@xylabs/ts-scripts-yarn3": "^4.2.1",
54
+ "@xylabs/tsconfig-react": "^4.2.1",
55
+ "react": "^18.3.1",
56
+ "react-dom": "^18.3.1",
57
+ "storybook": "^8.3.6",
58
+ "typescript": "^5.6.3"
59
+ },
60
+ "peerDependencies": {
61
+ "react": "^18",
62
+ "react-dom": "^18",
63
+ "react-router-dom": "^6"
64
+ },
65
+ "publishConfig": {
66
+ "access": "public"
67
+ },
68
+ "docs": "dist/docs.json"
69
+ }
package/src/index.ts ADDED
@@ -0,0 +1 @@
1
+ export * from './utm/index.ts'
@@ -0,0 +1,5 @@
1
+ declare module '*.png'
2
+ declare module '*.jpg'
3
+ declare module '*.svg'
4
+ declare module '*.gif'
5
+ declare module '*.webp'
@@ -0,0 +1,54 @@
1
+ import { Box } from '@mui/material'
2
+ import type { Meta, StoryFn } from '@storybook/react'
3
+ import { usePromise } from '@xylabs/react-promise'
4
+ import type { PropsWithChildren } from 'react'
5
+ import React, { useEffect } from 'react'
6
+ import { BrowserRouter, useSearchParams } from 'react-router-dom'
7
+
8
+ import { UtmStorageArchivist } from '../lib/index.ts'
9
+ import { useCaptureUtmLocation } from './useCaptureUtmLocation.ts'
10
+
11
+ export default { title: 'advertising/UseCaptureUtmLocation' } as Meta
12
+
13
+ const UtmStub: React.FC<PropsWithChildren> = ({ children }) => {
14
+ const [, setParams] = useSearchParams()
15
+
16
+ useEffect(() => {
17
+ setParams(() => {
18
+ const newParams = new URLSearchParams()
19
+ newParams.set('utm_campaign', 'test-campaign')
20
+ newParams.set('utm_content', 'test-content')
21
+ return newParams
22
+ })
23
+ }, [])
24
+
25
+ return children
26
+ }
27
+
28
+ const RouterDecorator = (Story: StoryFn) => (
29
+ <BrowserRouter>
30
+ <UtmStub>
31
+ <Story />
32
+ </UtmStub>
33
+ </BrowserRouter>
34
+ )
35
+
36
+ const Template: StoryFn = () => {
37
+ useCaptureUtmLocation()
38
+
39
+ const [payloads] = usePromise(async () => {
40
+ const archivist = await UtmStorageArchivist()
41
+ return await archivist.all()
42
+ }, [])
43
+
44
+ return (
45
+ <Box>
46
+ {payloads?.map(payload => <Box key={payload.$hash}>{JSON.stringify(payload, null, 2)}</Box>)}
47
+ </Box>
48
+ )
49
+ }
50
+
51
+ const Default = Template.bind({})
52
+ Default.decorators = [RouterDecorator]
53
+
54
+ export { Default }
@@ -0,0 +1,3 @@
1
+ export * from './useCaptureUtmLocation.ts'
2
+ export * from './useDefaultPageState.ts'
3
+ export * from './useDefaultUtmPageState.ts'
@@ -0,0 +1,38 @@
1
+ import { usePromise } from '@xylabs/react-promise'
2
+ import { type Utm, UtmSchema } from '@xyo-network/advertising-payload-plugins'
3
+ import { useMemo } from 'react'
4
+
5
+ import { UtmStorageArchivist } from '../lib/index.ts'
6
+ import { useDefaultUtmPageState } from './useDefaultUtmPageState.ts'
7
+
8
+ export const useCaptureUtmLocation = () => {
9
+ const [{
10
+ utm_campaign, utm_content, utm_medium, utm_source, utm_term,
11
+ }] = useDefaultUtmPageState()
12
+
13
+ const [sessionStorageArchivist, error] = usePromise(async () => {
14
+ return await UtmStorageArchivist()
15
+ }, [])
16
+
17
+ const utmPayload = useMemo(() => {
18
+ // Construct the base UTM payload
19
+ const utm: Utm = { schema: UtmSchema }
20
+
21
+ // Conditionally add all the UTM parameters present
22
+ if (utm_campaign) utm.utm_campaign = utm_campaign
23
+ if (utm_content) utm.utm_content = utm_content
24
+ if (utm_medium) utm.utm_medium = utm_medium
25
+ if (utm_source) utm.utm_source = utm_source
26
+ if (utm_term) utm.utm_term = utm_term
27
+
28
+ return utm
29
+ }, [utm_campaign, utm_content, utm_medium, utm_source, utm_term])
30
+
31
+ const [,insertError] = usePromise(async () => {
32
+ if (utmPayload && sessionStorageArchivist) {
33
+ await sessionStorageArchivist.insert([utmPayload])
34
+ }
35
+ }, [sessionStorageArchivist, utmPayload])
36
+
37
+ return { utmPayload, error: error ?? insertError }
38
+ }
@@ -0,0 +1,56 @@
1
+ import { useMemo } from 'react'
2
+ import { useSearchParams } from 'react-router-dom'
3
+
4
+ import type { PageStateKeys } from '../lib/index.ts'
5
+
6
+ export interface DefaultPageStateParams<TQueryParams extends PageStateKeys = PageStateKeys> {
7
+ queryParams: TQueryParams
8
+ }
9
+
10
+ export type DefaultPageStateReturn<TPageStateKeys extends PageStateKeys = PageStateKeys> = [
11
+ Record<TPageStateKeys[number], string>,
12
+ (source: keyof DefaultPageStateParams, key: TPageStateKeys[number], value: string) => void,
13
+ ]
14
+
15
+ /**
16
+ * Hook to get default page state as key value pairs from various sources. It provides a convenient abstraction over free form
17
+ * page state like query params by narrowing the used params and centralizing their logic.
18
+ *
19
+ * Future work could include adding support for localStorage, sessionStorage, and precedence of sources.
20
+ * @param defaultPageStateParams various sources of default page state (i.e. queryParams, localStorage, etc.)
21
+ * @returns
22
+ */
23
+ export const useDefaultPageState = <TQueryParams extends PageStateKeys = PageStateKeys>(
24
+ { queryParams }: DefaultPageStateParams<TQueryParams>,
25
+ ): DefaultPageStateReturn<TQueryParams> => {
26
+ const [searchParams, setSearchParams] = useSearchParams()
27
+
28
+ const patchDefaultPageState = (source: keyof DefaultPageStateParams, key: TQueryParams[number], value: string) => {
29
+ switch (source) {
30
+ case 'queryParams': {
31
+ setSearchParams((prevSearchParams) => {
32
+ const newSearchParams = new URLSearchParams(prevSearchParams)
33
+ newSearchParams.set(key, value)
34
+ return newSearchParams
35
+ })
36
+ break
37
+ }
38
+ default: {
39
+ throw new Error(`Invalid source for default page state: ${source}`)
40
+ }
41
+ }
42
+ }
43
+
44
+ const queryParamsState = useMemo(() => {
45
+ if (!queryParams) return {} as Record<TQueryParams[number], string>
46
+ let defaultState = {} as Record<TQueryParams[number], string>
47
+ for (const [key, value] of searchParams.entries()) {
48
+ if (queryParams.includes(key)) {
49
+ defaultState[key as TQueryParams[number]] = value
50
+ }
51
+ }
52
+ return defaultState
53
+ }, [searchParams, queryParams])
54
+
55
+ return [queryParamsState, patchDefaultPageState]
56
+ }
@@ -0,0 +1,14 @@
1
+ import { useDefaultPageState } from './useDefaultPageState.ts'
2
+
3
+ export const UtmQueryParamStrings = {
4
+ utmCampaign: 'utm_campaign',
5
+ utmContent: 'utm_content',
6
+ utmMedium: 'utm_medium',
7
+ utmSource: 'utm_source',
8
+ utmTerm: 'utm_term',
9
+ } as const
10
+
11
+ export type UtmQueryParams = typeof UtmQueryParamStrings[keyof typeof UtmQueryParamStrings]
12
+
13
+ /** Get the default page state for utm values from specific query params */
14
+ export const useDefaultUtmPageState = () => useDefaultPageState<UtmQueryParams[]>({ queryParams: Object.values(UtmQueryParamStrings) })
@@ -0,0 +1,2 @@
1
+ export * from './hooks/index.ts'
2
+ export * from './lib/index.ts'
@@ -0,0 +1 @@
1
+ export type PageStateKeys = ReadonlyArray<string>
@@ -0,0 +1,29 @@
1
+ import { isUtm } from '@xyo-network/advertising-payload-plugins'
2
+ import { StorageArchivist, StorageArchivistConfigSchema } from '@xyo-network/archivist-storage'
3
+
4
+ const STORAGE_NAME_SPACE = 'utm' as const
5
+
6
+ let archivist: StorageArchivist | undefined
7
+
8
+ /**
9
+ * Find or create a single instance of the UtmStorageArchivist
10
+ * @returns Archivist instance
11
+ */
12
+ export const UtmStorageArchivist = async () => {
13
+ if (archivist) {
14
+ return archivist
15
+ } else {
16
+ archivist = await StorageArchivist.create({
17
+ account: 'random',
18
+ config: {
19
+ schema: StorageArchivistConfigSchema, namespace: STORAGE_NAME_SPACE, type: 'session',
20
+ },
21
+ })
22
+ return archivist
23
+ }
24
+ }
25
+
26
+ export const LatestUtmPayload = async () => {
27
+ const archivist = await UtmStorageArchivist()
28
+ return (await archivist.all()).findLast(isUtm)
29
+ }
@@ -0,0 +1,2 @@
1
+ export * from './PageStateKeys.ts'
2
+ export * from './UtmStorageArchivist.ts'
package/typedoc.json ADDED
@@ -0,0 +1,5 @@
1
+ {
2
+ "$schema": "https://typedoc.org/schema.json",
3
+ "entryPoints": ["src/index.ts"],
4
+ "tsconfig": "./tsconfig.typedoc.json"
5
+ }
package/xy.config.ts ADDED
@@ -0,0 +1,10 @@
1
+ import type { XyTsupConfig } from '@xylabs/ts-scripts-yarn3'
2
+ const config: XyTsupConfig = {
3
+ compile: {
4
+ browser: { src: true },
5
+ node: {},
6
+ neutral: {},
7
+ },
8
+ }
9
+
10
+ export default config