@getpimms/analytics 0.0.27

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 ADDED
@@ -0,0 +1,91 @@
1
+ ## Overview
2
+
3
+ `@getpimms/analytics` allows you to track leads and sales conversions for PIMMS.
4
+
5
+ ## Quick start
6
+
7
+ 1. Enable conversion tracking for your PIMMS link.
8
+ 2. Install the `@getpimms/analytics` package to your project
9
+
10
+ ```bash
11
+ npm install @getpimms/analytics
12
+ ```
13
+
14
+ 3. Inject the Analytics script to your app
15
+
16
+ ```tsx
17
+ import { Analytics as PimmsAnalytics } from '@getpimms/analytics/react';
18
+
19
+ export default function RootLayout({
20
+ children,
21
+ }: Readonly<{
22
+ children: React.ReactNode;
23
+ }>) {
24
+ return (
25
+ <html lang="en">
26
+ <body>{children}</body>
27
+ <PimmsAnalytics />
28
+ </html>
29
+ );
30
+ }
31
+ ```
32
+
33
+ You can all use the `inject()` function to add the tracking script to other frameworks.
34
+
35
+ ## Available Props
36
+
37
+ You can pass the following props to the `Analytics` component to customize the tracking script.
38
+
39
+ ### `apiHost`
40
+
41
+ The API host to use for tracking. This is useful for setting up reverse proxies to avoid adblockers. The default is `https://api.pimms.io`.
42
+
43
+ ### `domainsConfig`
44
+
45
+ This is a JSON object that configures the domains that PIMMS will track.
46
+
47
+ - `refer`: The PIMMS short domain for [referral program client-side click tracking](https://d.to/clicks/refer) (previously `shortDomain`).
48
+ - `site`: The PIMMS short domain for [tracking site visits](https://d.to/clicks/site).
49
+ - `outbound`: An array of domains for cross-domain tracking. When configured, the existing `pimms_id` cookie will be automatically appended to all outbound links targeting these domains to enable cross-domain tracking across different applications.
50
+
51
+ ### `shortDomain`
52
+
53
+ [DEPRECATED: use `domainsConfig.refer` instead] The custom short domain you're using on PIMMS for your short links (for client-side click tracking).
54
+
55
+ ### `attributionModel`
56
+
57
+ Decide the attribution model to use for tracking. The default is `last-click`.
58
+
59
+ - `first-click` - The first click model gives all the credit to the first touchpoint in the customer journey.
60
+ - `last-click` - The last click model gives all the credit to the last touchpoint in the customer journey.
61
+
62
+ ### `cookieOptions`
63
+
64
+ The `cookieOptions` prop accepts the following keys:
65
+
66
+ | Key | Default | Description | Example |
67
+ |----------|---------|-------------|---------|
68
+ | `domain` | `null` | Specifies the value for the `Domain` Set-Cookie attribute. | `example.com` |
69
+ | `expires` | 90 days from now | Specifies the `Date` object to be the value for the `Expires` Set-Cookie attribute. | `new Date('2024-12-31')` |
70
+ | `expiresInDays` | `90` | Specifies the number (in days) to be the value for the `Expires` Set-Cookie attribute. | `90` |
71
+ | `path` | `/` | Specifies the value for the `Path` Set-Cookie attribute. By default, the path is considered the "default path". | `/` |
72
+
73
+ For example, to set a 60-day cookie window, you can use the following code:
74
+
75
+ ```tsx
76
+ import { Analytics as PimmsAnalytics } from "@getpimms/analytics"
77
+
78
+ <PimmsAnalytics
79
+ cookieOptions={{
80
+ expiresInDays: 60,
81
+ }}
82
+ />
83
+ ```
84
+
85
+ ### `queryParam`
86
+
87
+ The query parameter to listen to for client-side click-tracking (e.g. `?via=john`, `?ref=jane`). The default is `via`.
88
+
89
+ ### `scriptProps`
90
+
91
+ Custom properties to pass to the script tag. Refer to [MDN](https://developer.mozilla.org/en-US/docs/Web/API/HTMLScriptElement) for all available options.
package/dist/index.cjs ADDED
@@ -0,0 +1,93 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name2 in all)
8
+ __defProp(target, name2, { get: all[name2], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/generic.ts
21
+ var generic_exports = {};
22
+ __export(generic_exports, {
23
+ default: () => generic_default,
24
+ inject: () => inject
25
+ });
26
+ module.exports = __toCommonJS(generic_exports);
27
+
28
+ // package.json
29
+ var name = "@getpimms/analytics";
30
+ var version = "0.0.27";
31
+
32
+ // src/utils.tsx
33
+ function isBrowser() {
34
+ return typeof window !== "undefined";
35
+ }
36
+
37
+ // src/generic.ts
38
+ function inject(props) {
39
+ var _a, _b, _c, _d;
40
+ if (!isBrowser())
41
+ return;
42
+ const baseUrl = "https://cdn.pimms.io/analytics/script";
43
+ const features = [];
44
+ if ((_a = props.domainsConfig) == null ? void 0 : _a.site)
45
+ features.push("site-visit");
46
+ if ((_b = props.domainsConfig) == null ? void 0 : _b.outbound)
47
+ features.push("outbound-domains");
48
+ const src = ((_c = props.scriptProps) == null ? void 0 : _c.src) || (features.length > 0 ? `${baseUrl}.${features.join(".")}.js` : `${baseUrl}.js`);
49
+ if (document.head.querySelector(`script[src*="${src}"]`))
50
+ return;
51
+ const script = document.createElement("script");
52
+ script.src = src;
53
+ script.defer = ((_d = props.scriptProps) == null ? void 0 : _d.defer) ?? true;
54
+ script.setAttribute("data-sdkn", name);
55
+ script.setAttribute("data-sdkv", version);
56
+ if (props.apiHost) {
57
+ script.setAttribute("data-api-host", props.apiHost);
58
+ }
59
+ if (props.domainsConfig) {
60
+ script.setAttribute("data-domains", JSON.stringify(props.domainsConfig));
61
+ }
62
+ if (props.shortDomain) {
63
+ script.setAttribute("data-short-domain", props.shortDomain);
64
+ }
65
+ if (props.attributionModel) {
66
+ script.setAttribute("data-attribution-model", props.attributionModel);
67
+ }
68
+ if (props.cookieOptions && Object.keys(props.cookieOptions).length > 0) {
69
+ script.setAttribute(
70
+ "data-cookie-options",
71
+ JSON.stringify(props.cookieOptions)
72
+ );
73
+ }
74
+ if (props.queryParam) {
75
+ script.setAttribute("data-query-param", props.queryParam);
76
+ }
77
+ if (props.scriptProps) {
78
+ const { src: _, ...restProps } = props.scriptProps;
79
+ Object.assign(script, restProps);
80
+ }
81
+ script.onerror = () => {
82
+ console.log(`[PIMMS Web Analytics] failed to load script from ${src}.`);
83
+ };
84
+ document.head.appendChild(script);
85
+ }
86
+ var generic_default = {
87
+ inject
88
+ };
89
+ // Annotate the CommonJS export names for ESM import in node:
90
+ 0 && (module.exports = {
91
+ inject
92
+ });
93
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/generic.ts","../package.json","../src/utils.tsx"],"sourcesContent":["import { name, version } from '../package.json';\nimport type { AnalyticsProps } from './types';\nimport { isBrowser } from './utils';\n\n/**\n * Injects the PIMMS Web Analytics script into the page head.\n */\nfunction inject(props: AnalyticsProps): void {\n if (!isBrowser()) return;\n\n // Determine script source based on enabled features\n const baseUrl = 'https://cdn.pimms.io/analytics/script';\n const features = [];\n\n if (props.domainsConfig?.site) features.push('site-visit');\n if (props.domainsConfig?.outbound) features.push('outbound-domains');\n\n const src =\n props.scriptProps?.src ||\n (features.length > 0\n ? `${baseUrl}.${features.join('.')}.js`\n : `${baseUrl}.js`);\n\n if (document.head.querySelector(`script[src*=\"${src}\"]`)) return;\n\n const script = document.createElement('script');\n script.src = src;\n script.defer = props.scriptProps?.defer ?? true;\n script.setAttribute('data-sdkn', name);\n script.setAttribute('data-sdkv', version);\n\n if (props.apiHost) {\n script.setAttribute('data-api-host', props.apiHost);\n }\n\n if (props.domainsConfig) {\n script.setAttribute('data-domains', JSON.stringify(props.domainsConfig));\n }\n\n if (props.shortDomain) {\n script.setAttribute('data-short-domain', props.shortDomain);\n }\n\n if (props.attributionModel) {\n script.setAttribute('data-attribution-model', props.attributionModel);\n }\n\n if (props.cookieOptions && Object.keys(props.cookieOptions).length > 0) {\n script.setAttribute(\n 'data-cookie-options',\n JSON.stringify(props.cookieOptions),\n );\n }\n\n if (props.queryParam) {\n script.setAttribute('data-query-param', props.queryParam);\n }\n\n if (props.scriptProps) {\n const { src: _, ...restProps } = props.scriptProps; // we already set the src above\n Object.assign(script, restProps);\n }\n\n script.onerror = (): void => {\n // eslint-disable-next-line no-console -- Logging to console is intentional\n console.log(`[PIMMS Web Analytics] failed to load script from ${src}.`);\n };\n\n document.head.appendChild(script);\n}\n\nexport { inject };\nexport type { AnalyticsProps };\n\n// eslint-disable-next-line import/no-default-export -- Default export is intentional\nexport default {\n inject,\n};\n","{\n \"name\": \"@getpimms/analytics\",\n \"version\": \"0.0.27\",\n \"description\": \"\",\n \"keywords\": [\n \"analytics\",\n \"pimms\"\n ],\n \"repository\": {\n \"url\": \"github:getpimms/analytics\",\n \"directory\": \"packages/web\"\n },\n \"license\": \"MPL-2.0\",\n \"type\": \"module\",\n \"exports\": {\n \"./package.json\": \"./package.json\",\n \".\": {\n \"browser\": \"./dist/index.js\",\n \"import\": \"./dist/index.js\",\n \"require\": \"./dist/index.cjs\"\n },\n \"./react\": {\n \"browser\": \"./dist/react/index.js\",\n \"import\": \"./dist/react/index.js\",\n \"require\": \"./dist/react/index.cjs\"\n }\n },\n \"main\": \"dist/index.js\",\n \"types\": \"dist/index.d.ts\",\n \"typesVersions\": {\n \"*\": {\n \"*\": [\n \"dist/index.d.ts\"\n ],\n \"react\": [\n \"dist/react/index.d.ts\"\n ]\n }\n },\n \"scripts\": {\n \"build\": \"tsup\",\n \"dev\": \"tsup --watch\",\n \"lint\": \"eslint .\",\n \"lint-fix\": \"eslint . --fix\",\n \"type-check\": \"tsc --noEmit\"\n },\n \"eslintConfig\": {\n \"extends\": [\n \"@dub/eslint-config\"\n ],\n \"rules\": {\n \"tsdoc/syntax\": \"off\"\n }\n },\n \"dependencies\": {\n \"server-only\": \"^0.0.1\"\n },\n \"devDependencies\": {\n \"@dub/eslint-config\": \"workspace:0.0.0\",\n \"@swc/core\": \"^1.3.66\",\n \"@types/node\": \"^20.3.1\",\n \"@types/react\": \"^18.2.14\",\n \"react\": \"^18.2.0\",\n \"react-dom\": \"^18.2.0\",\n \"tsup\": \"7.1.0\"\n },\n \"publishConfig\": {\n \"access\": \"public\"\n }\n}","export function isBrowser() {\n return typeof window !== 'undefined';\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACCE,WAAQ;AACR,cAAW;;;ACFN,SAAS,YAAY;AAC1B,SAAO,OAAO,WAAW;AAC3B;;;AFKA,SAAS,OAAO,OAA6B;AAP7C;AAQE,MAAI,CAAC,UAAU;AAAG;AAGlB,QAAM,UAAU;AAChB,QAAM,WAAW,CAAC;AAElB,OAAI,WAAM,kBAAN,mBAAqB;AAAM,aAAS,KAAK,YAAY;AACzD,OAAI,WAAM,kBAAN,mBAAqB;AAAU,aAAS,KAAK,kBAAkB;AAEnE,QAAM,QACJ,WAAM,gBAAN,mBAAmB,SAClB,SAAS,SAAS,IACf,GAAG,OAAO,IAAI,SAAS,KAAK,GAAG,CAAC,QAChC,GAAG,OAAO;AAEhB,MAAI,SAAS,KAAK,cAAc,gBAAgB,GAAG,IAAI;AAAG;AAE1D,QAAM,SAAS,SAAS,cAAc,QAAQ;AAC9C,SAAO,MAAM;AACb,SAAO,UAAQ,WAAM,gBAAN,mBAAmB,UAAS;AAC3C,SAAO,aAAa,aAAa,IAAI;AACrC,SAAO,aAAa,aAAa,OAAO;AAExC,MAAI,MAAM,SAAS;AACjB,WAAO,aAAa,iBAAiB,MAAM,OAAO;AAAA,EACpD;AAEA,MAAI,MAAM,eAAe;AACvB,WAAO,aAAa,gBAAgB,KAAK,UAAU,MAAM,aAAa,CAAC;AAAA,EACzE;AAEA,MAAI,MAAM,aAAa;AACrB,WAAO,aAAa,qBAAqB,MAAM,WAAW;AAAA,EAC5D;AAEA,MAAI,MAAM,kBAAkB;AAC1B,WAAO,aAAa,0BAA0B,MAAM,gBAAgB;AAAA,EACtE;AAEA,MAAI,MAAM,iBAAiB,OAAO,KAAK,MAAM,aAAa,EAAE,SAAS,GAAG;AACtE,WAAO;AAAA,MACL;AAAA,MACA,KAAK,UAAU,MAAM,aAAa;AAAA,IACpC;AAAA,EACF;AAEA,MAAI,MAAM,YAAY;AACpB,WAAO,aAAa,oBAAoB,MAAM,UAAU;AAAA,EAC1D;AAEA,MAAI,MAAM,aAAa;AACrB,UAAM,EAAE,KAAK,GAAG,GAAG,UAAU,IAAI,MAAM;AACvC,WAAO,OAAO,QAAQ,SAAS;AAAA,EACjC;AAEA,SAAO,UAAU,MAAY;AAE3B,YAAQ,IAAI,oDAAoD,GAAG,GAAG;AAAA,EACxE;AAEA,WAAS,KAAK,YAAY,MAAM;AAClC;AAMA,IAAO,kBAAQ;AAAA,EACb;AACF;","names":[]}
@@ -0,0 +1,146 @@
1
+ interface AnalyticsProps {
2
+ /**
3
+ * The API endpoint to send analytics data to.
4
+ * @default 'https://api.pimms.io'
5
+ */
6
+ apiHost?: string;
7
+ /**
8
+ * This is a JSON object that configures the domains that PIMMS will track.
9
+ *
10
+ * - `refer`: The PIMMS short domain for referral program client-side click tracking (previously `shortDomain`). @see: https://d.to/clicks/refer
11
+ * - `site`: The PIMMS short domain for tracking site visits. @see: https://d.to/clicks/site
12
+ * - `outbound`: An array of domains for cross-domain tracking. When configured, the existing `pimms_id` cookie
13
+ * will be automatically appended to all outbound links targeting these domains to enable
14
+ * cross-domain tracking across different applications.
15
+ *
16
+ * @example {
17
+ * refer: "refer.pimms.io",
18
+ * site: "site.pimms.io",
19
+ * outbound: "pim.ms"
20
+ * }
21
+ */
22
+ domainsConfig?: {
23
+ refer?: string;
24
+ site?: string;
25
+ outbound?: string | string[];
26
+ };
27
+ /**
28
+ * The custom domain you're using on PIMMS for your short links (for client-side click tracking).
29
+ * @example 'go.example.com'
30
+ * @deprecated Use domainsConfig.refer instead
31
+ */
32
+ shortDomain?: string;
33
+ /**
34
+ * The Attribution Model to use for the analytics event.
35
+ *
36
+ * @default 'last-click'
37
+ *
38
+ * - `first-click` - The first click model gives all the credit to the first touchpoint in the customer journey.
39
+ * - `last-click` - The last click model gives all the credit to the last touchpoint in the customer journey.
40
+ */
41
+ attributionModel?: 'first-click' | 'last-click';
42
+ /**
43
+ * The cookie options to use for the analytics event.
44
+ */
45
+ cookieOptions?: {
46
+ /**
47
+ * Specifies the value for the {@link https://tools.ietf.org/html/rfc6265#section-5.2.3|Domain Set-Cookie attribute}. By default, no
48
+ * domain is set, and most clients will consider the cookie to apply to only the current domain.
49
+ * By default, the domain is set to the current hostname (including all subdomains).
50
+ *
51
+ * @default `.` + window.location.hostname
52
+ */
53
+ domain?: string | undefined;
54
+ /**
55
+ * Specifies the `Date` object to be the value for the {@link https://tools.ietf.org/html/rfc6265#section-5.2.1|`Expires` `Set-Cookie` attribute}. By default,
56
+ * no expiration is set, and most clients will consider this a "non-persistent cookie" and will delete
57
+ * it on a condition like exiting a web browser application.
58
+ *
59
+ * *Note* the {@link https://tools.ietf.org/html/rfc6265#section-5.3|cookie storage model specification}
60
+ * states that if both `expires` and `maxAge` are set, then `maxAge` takes precedence, but it is
61
+ * possible not all clients by obey this, so if both are set, they should
62
+ * point to the same date and time.
63
+ */
64
+ expires?: Date | undefined;
65
+ /**
66
+ * Specifies the number of days until the cookie expires.
67
+ *
68
+ * @default 90
69
+ */
70
+ expiresInDays?: number | undefined;
71
+ /**
72
+ * Specifies the boolean value for the {@link https://tools.ietf.org/html/rfc6265#section-5.2.6|`HttpOnly` `Set-Cookie` attribute}.
73
+ * When truthy, the `HttpOnly` attribute is set, otherwise it is not. By
74
+ * default, the `HttpOnly` attribute is not set.
75
+ *
76
+ * *Note* be careful when setting this to true, as compliant clients will
77
+ * not allow client-side JavaScript to see the cookie in `document.cookie`.
78
+ */
79
+ httpOnly?: boolean | undefined;
80
+ /**
81
+ * Specifies the number (in seconds) to be the value for the `Max-Age`
82
+ * `Set-Cookie` attribute. The given number will be converted to an integer
83
+ * by rounding down. By default, no maximum age is set.
84
+ *
85
+ * *Note* the {@link https://tools.ietf.org/html/rfc6265#section-5.3|cookie storage model specification}
86
+ * states that if both `expires` and `maxAge` are set, then `maxAge` takes precedence, but it is
87
+ * possible not all clients by obey this, so if both are set, they should
88
+ * point to the same date and time.
89
+ */
90
+ maxAge?: number | undefined;
91
+ /**
92
+ * Specifies the value for the {@link https://tools.ietf.org/html/rfc6265#section-5.2.4|`Path` `Set-Cookie` attribute}.
93
+ * By default, the path is considered the "default path".
94
+ */
95
+ path?: string | undefined;
96
+ /**
97
+ * Specifies the boolean or string to be the value for the {@link https://tools.ietf.org/html/draft-ietf-httpbis-rfc6265bis-03#section-4.1.2.7|`SameSite` `Set-Cookie` attribute}.
98
+ *
99
+ * - `true` will set the `SameSite` attribute to `Strict` for strict same
100
+ * site enforcement.
101
+ * - `false` will not set the `SameSite` attribute.
102
+ * - `'lax'` will set the `SameSite` attribute to Lax for lax same site
103
+ * enforcement.
104
+ * - `'strict'` will set the `SameSite` attribute to Strict for strict same
105
+ * site enforcement.
106
+ * - `'none'` will set the SameSite attribute to None for an explicit
107
+ * cross-site cookie.
108
+ *
109
+ * More information about the different enforcement levels can be found in {@link https://tools.ietf.org/html/draft-ietf-httpbis-rfc6265bis-03#section-4.1.2.7|the specification}.
110
+ *
111
+ * *note* This is an attribute that has not yet been fully standardized, and may change in the future. This also means many clients may ignore this attribute until they understand it.
112
+ */
113
+ sameSite?: true | false | 'lax' | 'strict' | 'none' | undefined;
114
+ /**
115
+ * Specifies the boolean value for the {@link https://tools.ietf.org/html/rfc6265#section-5.2.5|`Secure` `Set-Cookie` attribute}. When truthy, the
116
+ * `Secure` attribute is set, otherwise it is not. By default, the `Secure` attribute is not set.
117
+ *
118
+ * *Note* be careful when setting this to `true`, as compliant clients will
119
+ * not send the cookie back to the server in the future if the browser does
120
+ * not have an HTTPS connection.
121
+ */
122
+ secure?: boolean | undefined;
123
+ };
124
+ /**
125
+ * The query parameter to listen to for client-side click-tracking (e.g. `?via=john`, `?ref=jane`).
126
+ *
127
+ * @default 'via'
128
+ */
129
+ queryParam?: string;
130
+ /**
131
+ * Custom properties to pass to the script.
132
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/HTMLScriptElement
133
+ */
134
+ scriptProps?: React.DetailedHTMLProps<React.ScriptHTMLAttributes<HTMLScriptElement>, HTMLScriptElement>;
135
+ }
136
+
137
+ /**
138
+ * Injects the PIMMS Web Analytics script into the page head.
139
+ */
140
+ declare function inject(props: AnalyticsProps): void;
141
+
142
+ declare const _default: {
143
+ inject: typeof inject;
144
+ };
145
+
146
+ export { AnalyticsProps, _default as default, inject };
@@ -0,0 +1,146 @@
1
+ interface AnalyticsProps {
2
+ /**
3
+ * The API endpoint to send analytics data to.
4
+ * @default 'https://api.pimms.io'
5
+ */
6
+ apiHost?: string;
7
+ /**
8
+ * This is a JSON object that configures the domains that PIMMS will track.
9
+ *
10
+ * - `refer`: The PIMMS short domain for referral program client-side click tracking (previously `shortDomain`). @see: https://d.to/clicks/refer
11
+ * - `site`: The PIMMS short domain for tracking site visits. @see: https://d.to/clicks/site
12
+ * - `outbound`: An array of domains for cross-domain tracking. When configured, the existing `pimms_id` cookie
13
+ * will be automatically appended to all outbound links targeting these domains to enable
14
+ * cross-domain tracking across different applications.
15
+ *
16
+ * @example {
17
+ * refer: "refer.pimms.io",
18
+ * site: "site.pimms.io",
19
+ * outbound: "pim.ms"
20
+ * }
21
+ */
22
+ domainsConfig?: {
23
+ refer?: string;
24
+ site?: string;
25
+ outbound?: string | string[];
26
+ };
27
+ /**
28
+ * The custom domain you're using on PIMMS for your short links (for client-side click tracking).
29
+ * @example 'go.example.com'
30
+ * @deprecated Use domainsConfig.refer instead
31
+ */
32
+ shortDomain?: string;
33
+ /**
34
+ * The Attribution Model to use for the analytics event.
35
+ *
36
+ * @default 'last-click'
37
+ *
38
+ * - `first-click` - The first click model gives all the credit to the first touchpoint in the customer journey.
39
+ * - `last-click` - The last click model gives all the credit to the last touchpoint in the customer journey.
40
+ */
41
+ attributionModel?: 'first-click' | 'last-click';
42
+ /**
43
+ * The cookie options to use for the analytics event.
44
+ */
45
+ cookieOptions?: {
46
+ /**
47
+ * Specifies the value for the {@link https://tools.ietf.org/html/rfc6265#section-5.2.3|Domain Set-Cookie attribute}. By default, no
48
+ * domain is set, and most clients will consider the cookie to apply to only the current domain.
49
+ * By default, the domain is set to the current hostname (including all subdomains).
50
+ *
51
+ * @default `.` + window.location.hostname
52
+ */
53
+ domain?: string | undefined;
54
+ /**
55
+ * Specifies the `Date` object to be the value for the {@link https://tools.ietf.org/html/rfc6265#section-5.2.1|`Expires` `Set-Cookie` attribute}. By default,
56
+ * no expiration is set, and most clients will consider this a "non-persistent cookie" and will delete
57
+ * it on a condition like exiting a web browser application.
58
+ *
59
+ * *Note* the {@link https://tools.ietf.org/html/rfc6265#section-5.3|cookie storage model specification}
60
+ * states that if both `expires` and `maxAge` are set, then `maxAge` takes precedence, but it is
61
+ * possible not all clients by obey this, so if both are set, they should
62
+ * point to the same date and time.
63
+ */
64
+ expires?: Date | undefined;
65
+ /**
66
+ * Specifies the number of days until the cookie expires.
67
+ *
68
+ * @default 90
69
+ */
70
+ expiresInDays?: number | undefined;
71
+ /**
72
+ * Specifies the boolean value for the {@link https://tools.ietf.org/html/rfc6265#section-5.2.6|`HttpOnly` `Set-Cookie` attribute}.
73
+ * When truthy, the `HttpOnly` attribute is set, otherwise it is not. By
74
+ * default, the `HttpOnly` attribute is not set.
75
+ *
76
+ * *Note* be careful when setting this to true, as compliant clients will
77
+ * not allow client-side JavaScript to see the cookie in `document.cookie`.
78
+ */
79
+ httpOnly?: boolean | undefined;
80
+ /**
81
+ * Specifies the number (in seconds) to be the value for the `Max-Age`
82
+ * `Set-Cookie` attribute. The given number will be converted to an integer
83
+ * by rounding down. By default, no maximum age is set.
84
+ *
85
+ * *Note* the {@link https://tools.ietf.org/html/rfc6265#section-5.3|cookie storage model specification}
86
+ * states that if both `expires` and `maxAge` are set, then `maxAge` takes precedence, but it is
87
+ * possible not all clients by obey this, so if both are set, they should
88
+ * point to the same date and time.
89
+ */
90
+ maxAge?: number | undefined;
91
+ /**
92
+ * Specifies the value for the {@link https://tools.ietf.org/html/rfc6265#section-5.2.4|`Path` `Set-Cookie` attribute}.
93
+ * By default, the path is considered the "default path".
94
+ */
95
+ path?: string | undefined;
96
+ /**
97
+ * Specifies the boolean or string to be the value for the {@link https://tools.ietf.org/html/draft-ietf-httpbis-rfc6265bis-03#section-4.1.2.7|`SameSite` `Set-Cookie` attribute}.
98
+ *
99
+ * - `true` will set the `SameSite` attribute to `Strict` for strict same
100
+ * site enforcement.
101
+ * - `false` will not set the `SameSite` attribute.
102
+ * - `'lax'` will set the `SameSite` attribute to Lax for lax same site
103
+ * enforcement.
104
+ * - `'strict'` will set the `SameSite` attribute to Strict for strict same
105
+ * site enforcement.
106
+ * - `'none'` will set the SameSite attribute to None for an explicit
107
+ * cross-site cookie.
108
+ *
109
+ * More information about the different enforcement levels can be found in {@link https://tools.ietf.org/html/draft-ietf-httpbis-rfc6265bis-03#section-4.1.2.7|the specification}.
110
+ *
111
+ * *note* This is an attribute that has not yet been fully standardized, and may change in the future. This also means many clients may ignore this attribute until they understand it.
112
+ */
113
+ sameSite?: true | false | 'lax' | 'strict' | 'none' | undefined;
114
+ /**
115
+ * Specifies the boolean value for the {@link https://tools.ietf.org/html/rfc6265#section-5.2.5|`Secure` `Set-Cookie` attribute}. When truthy, the
116
+ * `Secure` attribute is set, otherwise it is not. By default, the `Secure` attribute is not set.
117
+ *
118
+ * *Note* be careful when setting this to `true`, as compliant clients will
119
+ * not send the cookie back to the server in the future if the browser does
120
+ * not have an HTTPS connection.
121
+ */
122
+ secure?: boolean | undefined;
123
+ };
124
+ /**
125
+ * The query parameter to listen to for client-side click-tracking (e.g. `?via=john`, `?ref=jane`).
126
+ *
127
+ * @default 'via'
128
+ */
129
+ queryParam?: string;
130
+ /**
131
+ * Custom properties to pass to the script.
132
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/HTMLScriptElement
133
+ */
134
+ scriptProps?: React.DetailedHTMLProps<React.ScriptHTMLAttributes<HTMLScriptElement>, HTMLScriptElement>;
135
+ }
136
+
137
+ /**
138
+ * Injects the PIMMS Web Analytics script into the page head.
139
+ */
140
+ declare function inject(props: AnalyticsProps): void;
141
+
142
+ declare const _default: {
143
+ inject: typeof inject;
144
+ };
145
+
146
+ export { AnalyticsProps, _default as default, inject };
package/dist/index.js ADDED
@@ -0,0 +1,66 @@
1
+ // package.json
2
+ var name = "@getpimms/analytics";
3
+ var version = "0.0.27";
4
+
5
+ // src/utils.tsx
6
+ function isBrowser() {
7
+ return typeof window !== "undefined";
8
+ }
9
+
10
+ // src/generic.ts
11
+ function inject(props) {
12
+ var _a, _b, _c, _d;
13
+ if (!isBrowser())
14
+ return;
15
+ const baseUrl = "https://cdn.pimms.io/analytics/script";
16
+ const features = [];
17
+ if ((_a = props.domainsConfig) == null ? void 0 : _a.site)
18
+ features.push("site-visit");
19
+ if ((_b = props.domainsConfig) == null ? void 0 : _b.outbound)
20
+ features.push("outbound-domains");
21
+ const src = ((_c = props.scriptProps) == null ? void 0 : _c.src) || (features.length > 0 ? `${baseUrl}.${features.join(".")}.js` : `${baseUrl}.js`);
22
+ if (document.head.querySelector(`script[src*="${src}"]`))
23
+ return;
24
+ const script = document.createElement("script");
25
+ script.src = src;
26
+ script.defer = ((_d = props.scriptProps) == null ? void 0 : _d.defer) ?? true;
27
+ script.setAttribute("data-sdkn", name);
28
+ script.setAttribute("data-sdkv", version);
29
+ if (props.apiHost) {
30
+ script.setAttribute("data-api-host", props.apiHost);
31
+ }
32
+ if (props.domainsConfig) {
33
+ script.setAttribute("data-domains", JSON.stringify(props.domainsConfig));
34
+ }
35
+ if (props.shortDomain) {
36
+ script.setAttribute("data-short-domain", props.shortDomain);
37
+ }
38
+ if (props.attributionModel) {
39
+ script.setAttribute("data-attribution-model", props.attributionModel);
40
+ }
41
+ if (props.cookieOptions && Object.keys(props.cookieOptions).length > 0) {
42
+ script.setAttribute(
43
+ "data-cookie-options",
44
+ JSON.stringify(props.cookieOptions)
45
+ );
46
+ }
47
+ if (props.queryParam) {
48
+ script.setAttribute("data-query-param", props.queryParam);
49
+ }
50
+ if (props.scriptProps) {
51
+ const { src: _, ...restProps } = props.scriptProps;
52
+ Object.assign(script, restProps);
53
+ }
54
+ script.onerror = () => {
55
+ console.log(`[PIMMS Web Analytics] failed to load script from ${src}.`);
56
+ };
57
+ document.head.appendChild(script);
58
+ }
59
+ var generic_default = {
60
+ inject
61
+ };
62
+ export {
63
+ generic_default as default,
64
+ inject
65
+ };
66
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../package.json","../src/utils.tsx","../src/generic.ts"],"sourcesContent":["{\n \"name\": \"@getpimms/analytics\",\n \"version\": \"0.0.27\",\n \"description\": \"\",\n \"keywords\": [\n \"analytics\",\n \"pimms\"\n ],\n \"repository\": {\n \"url\": \"github:getpimms/analytics\",\n \"directory\": \"packages/web\"\n },\n \"license\": \"MPL-2.0\",\n \"type\": \"module\",\n \"exports\": {\n \"./package.json\": \"./package.json\",\n \".\": {\n \"browser\": \"./dist/index.js\",\n \"import\": \"./dist/index.js\",\n \"require\": \"./dist/index.cjs\"\n },\n \"./react\": {\n \"browser\": \"./dist/react/index.js\",\n \"import\": \"./dist/react/index.js\",\n \"require\": \"./dist/react/index.cjs\"\n }\n },\n \"main\": \"dist/index.js\",\n \"types\": \"dist/index.d.ts\",\n \"typesVersions\": {\n \"*\": {\n \"*\": [\n \"dist/index.d.ts\"\n ],\n \"react\": [\n \"dist/react/index.d.ts\"\n ]\n }\n },\n \"scripts\": {\n \"build\": \"tsup\",\n \"dev\": \"tsup --watch\",\n \"lint\": \"eslint .\",\n \"lint-fix\": \"eslint . --fix\",\n \"type-check\": \"tsc --noEmit\"\n },\n \"eslintConfig\": {\n \"extends\": [\n \"@dub/eslint-config\"\n ],\n \"rules\": {\n \"tsdoc/syntax\": \"off\"\n }\n },\n \"dependencies\": {\n \"server-only\": \"^0.0.1\"\n },\n \"devDependencies\": {\n \"@dub/eslint-config\": \"workspace:0.0.0\",\n \"@swc/core\": \"^1.3.66\",\n \"@types/node\": \"^20.3.1\",\n \"@types/react\": \"^18.2.14\",\n \"react\": \"^18.2.0\",\n \"react-dom\": \"^18.2.0\",\n \"tsup\": \"7.1.0\"\n },\n \"publishConfig\": {\n \"access\": \"public\"\n }\n}","export function isBrowser() {\n return typeof window !== 'undefined';\n}\n","import { name, version } from '../package.json';\nimport type { AnalyticsProps } from './types';\nimport { isBrowser } from './utils';\n\n/**\n * Injects the PIMMS Web Analytics script into the page head.\n */\nfunction inject(props: AnalyticsProps): void {\n if (!isBrowser()) return;\n\n // Determine script source based on enabled features\n const baseUrl = 'https://cdn.pimms.io/analytics/script';\n const features = [];\n\n if (props.domainsConfig?.site) features.push('site-visit');\n if (props.domainsConfig?.outbound) features.push('outbound-domains');\n\n const src =\n props.scriptProps?.src ||\n (features.length > 0\n ? `${baseUrl}.${features.join('.')}.js`\n : `${baseUrl}.js`);\n\n if (document.head.querySelector(`script[src*=\"${src}\"]`)) return;\n\n const script = document.createElement('script');\n script.src = src;\n script.defer = props.scriptProps?.defer ?? true;\n script.setAttribute('data-sdkn', name);\n script.setAttribute('data-sdkv', version);\n\n if (props.apiHost) {\n script.setAttribute('data-api-host', props.apiHost);\n }\n\n if (props.domainsConfig) {\n script.setAttribute('data-domains', JSON.stringify(props.domainsConfig));\n }\n\n if (props.shortDomain) {\n script.setAttribute('data-short-domain', props.shortDomain);\n }\n\n if (props.attributionModel) {\n script.setAttribute('data-attribution-model', props.attributionModel);\n }\n\n if (props.cookieOptions && Object.keys(props.cookieOptions).length > 0) {\n script.setAttribute(\n 'data-cookie-options',\n JSON.stringify(props.cookieOptions),\n );\n }\n\n if (props.queryParam) {\n script.setAttribute('data-query-param', props.queryParam);\n }\n\n if (props.scriptProps) {\n const { src: _, ...restProps } = props.scriptProps; // we already set the src above\n Object.assign(script, restProps);\n }\n\n script.onerror = (): void => {\n // eslint-disable-next-line no-console -- Logging to console is intentional\n console.log(`[PIMMS Web Analytics] failed to load script from ${src}.`);\n };\n\n document.head.appendChild(script);\n}\n\nexport { inject };\nexport type { AnalyticsProps };\n\n// eslint-disable-next-line import/no-default-export -- Default export is intentional\nexport default {\n inject,\n};\n"],"mappings":";AACE,WAAQ;AACR,cAAW;;;ACFN,SAAS,YAAY;AAC1B,SAAO,OAAO,WAAW;AAC3B;;;ACKA,SAAS,OAAO,OAA6B;AAP7C;AAQE,MAAI,CAAC,UAAU;AAAG;AAGlB,QAAM,UAAU;AAChB,QAAM,WAAW,CAAC;AAElB,OAAI,WAAM,kBAAN,mBAAqB;AAAM,aAAS,KAAK,YAAY;AACzD,OAAI,WAAM,kBAAN,mBAAqB;AAAU,aAAS,KAAK,kBAAkB;AAEnE,QAAM,QACJ,WAAM,gBAAN,mBAAmB,SAClB,SAAS,SAAS,IACf,GAAG,OAAO,IAAI,SAAS,KAAK,GAAG,CAAC,QAChC,GAAG,OAAO;AAEhB,MAAI,SAAS,KAAK,cAAc,gBAAgB,GAAG,IAAI;AAAG;AAE1D,QAAM,SAAS,SAAS,cAAc,QAAQ;AAC9C,SAAO,MAAM;AACb,SAAO,UAAQ,WAAM,gBAAN,mBAAmB,UAAS;AAC3C,SAAO,aAAa,aAAa,IAAI;AACrC,SAAO,aAAa,aAAa,OAAO;AAExC,MAAI,MAAM,SAAS;AACjB,WAAO,aAAa,iBAAiB,MAAM,OAAO;AAAA,EACpD;AAEA,MAAI,MAAM,eAAe;AACvB,WAAO,aAAa,gBAAgB,KAAK,UAAU,MAAM,aAAa,CAAC;AAAA,EACzE;AAEA,MAAI,MAAM,aAAa;AACrB,WAAO,aAAa,qBAAqB,MAAM,WAAW;AAAA,EAC5D;AAEA,MAAI,MAAM,kBAAkB;AAC1B,WAAO,aAAa,0BAA0B,MAAM,gBAAgB;AAAA,EACtE;AAEA,MAAI,MAAM,iBAAiB,OAAO,KAAK,MAAM,aAAa,EAAE,SAAS,GAAG;AACtE,WAAO;AAAA,MACL;AAAA,MACA,KAAK,UAAU,MAAM,aAAa;AAAA,IACpC;AAAA,EACF;AAEA,MAAI,MAAM,YAAY;AACpB,WAAO,aAAa,oBAAoB,MAAM,UAAU;AAAA,EAC1D;AAEA,MAAI,MAAM,aAAa;AACrB,UAAM,EAAE,KAAK,GAAG,GAAG,UAAU,IAAI,MAAM;AACvC,WAAO,OAAO,QAAQ,SAAS;AAAA,EACjC;AAEA,SAAO,UAAU,MAAY;AAE3B,YAAQ,IAAI,oDAAoD,GAAG,GAAG;AAAA,EACxE;AAEA,WAAS,KAAK,YAAY,MAAM;AAClC;AAMA,IAAO,kBAAQ;AAAA,EACb;AACF;","names":[]}
@@ -0,0 +1,99 @@
1
+ "use client";
2
+ "use strict";
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name2 in all)
9
+ __defProp(target, name2, { get: all[name2], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
20
+
21
+ // src/react.tsx
22
+ var react_exports = {};
23
+ __export(react_exports, {
24
+ Analytics: () => Analytics
25
+ });
26
+ module.exports = __toCommonJS(react_exports);
27
+ var import_react = require("react");
28
+
29
+ // package.json
30
+ var name = "@getpimms/analytics";
31
+ var version = "0.0.27";
32
+
33
+ // src/utils.tsx
34
+ function isBrowser() {
35
+ return typeof window !== "undefined";
36
+ }
37
+
38
+ // src/generic.ts
39
+ function inject(props) {
40
+ var _a, _b, _c, _d;
41
+ if (!isBrowser())
42
+ return;
43
+ const baseUrl = "https://cdn.pimms.io/analytics/script";
44
+ const features = [];
45
+ if ((_a = props.domainsConfig) == null ? void 0 : _a.site)
46
+ features.push("site-visit");
47
+ if ((_b = props.domainsConfig) == null ? void 0 : _b.outbound)
48
+ features.push("outbound-domains");
49
+ const src = ((_c = props.scriptProps) == null ? void 0 : _c.src) || (features.length > 0 ? `${baseUrl}.${features.join(".")}.js` : `${baseUrl}.js`);
50
+ if (document.head.querySelector(`script[src*="${src}"]`))
51
+ return;
52
+ const script = document.createElement("script");
53
+ script.src = src;
54
+ script.defer = ((_d = props.scriptProps) == null ? void 0 : _d.defer) ?? true;
55
+ script.setAttribute("data-sdkn", name);
56
+ script.setAttribute("data-sdkv", version);
57
+ if (props.apiHost) {
58
+ script.setAttribute("data-api-host", props.apiHost);
59
+ }
60
+ if (props.domainsConfig) {
61
+ script.setAttribute("data-domains", JSON.stringify(props.domainsConfig));
62
+ }
63
+ if (props.shortDomain) {
64
+ script.setAttribute("data-short-domain", props.shortDomain);
65
+ }
66
+ if (props.attributionModel) {
67
+ script.setAttribute("data-attribution-model", props.attributionModel);
68
+ }
69
+ if (props.cookieOptions && Object.keys(props.cookieOptions).length > 0) {
70
+ script.setAttribute(
71
+ "data-cookie-options",
72
+ JSON.stringify(props.cookieOptions)
73
+ );
74
+ }
75
+ if (props.queryParam) {
76
+ script.setAttribute("data-query-param", props.queryParam);
77
+ }
78
+ if (props.scriptProps) {
79
+ const { src: _, ...restProps } = props.scriptProps;
80
+ Object.assign(script, restProps);
81
+ }
82
+ script.onerror = () => {
83
+ console.log(`[PIMMS Web Analytics] failed to load script from ${src}.`);
84
+ };
85
+ document.head.appendChild(script);
86
+ }
87
+
88
+ // src/react.tsx
89
+ function Analytics(props) {
90
+ (0, import_react.useEffect)(() => {
91
+ inject(props);
92
+ }, [props]);
93
+ return null;
94
+ }
95
+ // Annotate the CommonJS export names for ESM import in node:
96
+ 0 && (module.exports = {
97
+ Analytics
98
+ });
99
+ //# sourceMappingURL=index.cjs.map