@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.
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/react.tsx","../../package.json","../../src/utils.tsx","../../src/generic.ts"],"sourcesContent":["import { useEffect } from 'react';\nimport { inject } from './generic';\nimport type { AnalyticsProps } from './types';\n\n/**\n * Injects the PIMMS Web Analytics script into the page head.\n * @param props - Analytics options.\n * ```js\n * import { Analytics as PimmsAnalytics } from '@getpimms/analytics/react';\n *\n * export default function App() {\n * return (\n * <div>\n * <PimmsAnalytics />\n * <h1>My App</h1>\n * </div>\n * );\n * }\n * ```\n */\nfunction Analytics(props: AnalyticsProps): null {\n useEffect(() => {\n inject(props);\n }, [props]);\n\n return null;\n}\n\nexport { Analytics };\nexport type { AnalyticsProps };\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","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":";;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,mBAA0B;;;ACCxB,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;;;AHjDA,SAAS,UAAU,OAA6B;AAC9C,8BAAU,MAAM;AACd,WAAO,KAAK;AAAA,EACd,GAAG,CAAC,KAAK,CAAC;AAEV,SAAO;AACT;","names":[]}
@@ -0,0 +1,155 @@
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
+ * @param props - Analytics options.
140
+ * ```js
141
+ * import { Analytics as PimmsAnalytics } from '@getpimms/analytics/react';
142
+ *
143
+ * export default function App() {
144
+ * return (
145
+ * <div>
146
+ * <PimmsAnalytics />
147
+ * <h1>My App</h1>
148
+ * </div>
149
+ * );
150
+ * }
151
+ * ```
152
+ */
153
+ declare function Analytics(props: AnalyticsProps): null;
154
+
155
+ export { Analytics, AnalyticsProps };
@@ -0,0 +1,155 @@
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
+ * @param props - Analytics options.
140
+ * ```js
141
+ * import { Analytics as PimmsAnalytics } from '@getpimms/analytics/react';
142
+ *
143
+ * export default function App() {
144
+ * return (
145
+ * <div>
146
+ * <PimmsAnalytics />
147
+ * <h1>My App</h1>
148
+ * </div>
149
+ * );
150
+ * }
151
+ * ```
152
+ */
153
+ declare function Analytics(props: AnalyticsProps): null;
154
+
155
+ export { Analytics, AnalyticsProps };
@@ -0,0 +1,75 @@
1
+ "use client";
2
+
3
+ // src/react.tsx
4
+ import { useEffect } from "react";
5
+
6
+ // package.json
7
+ var name = "@getpimms/analytics";
8
+ var version = "0.0.27";
9
+
10
+ // src/utils.tsx
11
+ function isBrowser() {
12
+ return typeof window !== "undefined";
13
+ }
14
+
15
+ // src/generic.ts
16
+ function inject(props) {
17
+ var _a, _b, _c, _d;
18
+ if (!isBrowser())
19
+ return;
20
+ const baseUrl = "https://cdn.pimms.io/analytics/script";
21
+ const features = [];
22
+ if ((_a = props.domainsConfig) == null ? void 0 : _a.site)
23
+ features.push("site-visit");
24
+ if ((_b = props.domainsConfig) == null ? void 0 : _b.outbound)
25
+ features.push("outbound-domains");
26
+ const src = ((_c = props.scriptProps) == null ? void 0 : _c.src) || (features.length > 0 ? `${baseUrl}.${features.join(".")}.js` : `${baseUrl}.js`);
27
+ if (document.head.querySelector(`script[src*="${src}"]`))
28
+ return;
29
+ const script = document.createElement("script");
30
+ script.src = src;
31
+ script.defer = ((_d = props.scriptProps) == null ? void 0 : _d.defer) ?? true;
32
+ script.setAttribute("data-sdkn", name);
33
+ script.setAttribute("data-sdkv", version);
34
+ if (props.apiHost) {
35
+ script.setAttribute("data-api-host", props.apiHost);
36
+ }
37
+ if (props.domainsConfig) {
38
+ script.setAttribute("data-domains", JSON.stringify(props.domainsConfig));
39
+ }
40
+ if (props.shortDomain) {
41
+ script.setAttribute("data-short-domain", props.shortDomain);
42
+ }
43
+ if (props.attributionModel) {
44
+ script.setAttribute("data-attribution-model", props.attributionModel);
45
+ }
46
+ if (props.cookieOptions && Object.keys(props.cookieOptions).length > 0) {
47
+ script.setAttribute(
48
+ "data-cookie-options",
49
+ JSON.stringify(props.cookieOptions)
50
+ );
51
+ }
52
+ if (props.queryParam) {
53
+ script.setAttribute("data-query-param", props.queryParam);
54
+ }
55
+ if (props.scriptProps) {
56
+ const { src: _, ...restProps } = props.scriptProps;
57
+ Object.assign(script, restProps);
58
+ }
59
+ script.onerror = () => {
60
+ console.log(`[PIMMS Web Analytics] failed to load script from ${src}.`);
61
+ };
62
+ document.head.appendChild(script);
63
+ }
64
+
65
+ // src/react.tsx
66
+ function Analytics(props) {
67
+ useEffect(() => {
68
+ inject(props);
69
+ }, [props]);
70
+ return null;
71
+ }
72
+ export {
73
+ Analytics
74
+ };
75
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/react.tsx","../../package.json","../../src/utils.tsx","../../src/generic.ts"],"sourcesContent":["import { useEffect } from 'react';\nimport { inject } from './generic';\nimport type { AnalyticsProps } from './types';\n\n/**\n * Injects the PIMMS Web Analytics script into the page head.\n * @param props - Analytics options.\n * ```js\n * import { Analytics as PimmsAnalytics } from '@getpimms/analytics/react';\n *\n * export default function App() {\n * return (\n * <div>\n * <PimmsAnalytics />\n * <h1>My App</h1>\n * </div>\n * );\n * }\n * ```\n */\nfunction Analytics(props: AnalyticsProps): null {\n useEffect(() => {\n inject(props);\n }, [props]);\n\n return null;\n}\n\nexport { Analytics };\nexport type { AnalyticsProps };\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","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":";;;AAAA,SAAS,iBAAiB;;;ACCxB,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;;;AHjDA,SAAS,UAAU,OAA6B;AAC9C,YAAU,MAAM;AACd,WAAO,KAAK;AAAA,EACd,GAAG,CAAC,KAAK,CAAC;AAEV,SAAO;AACT;","names":[]}
package/package.json ADDED
@@ -0,0 +1,70 @@
1
+ {
2
+ "name": "@getpimms/analytics",
3
+ "version": "0.0.27",
4
+ "description": "",
5
+ "keywords": [
6
+ "analytics",
7
+ "pimms"
8
+ ],
9
+ "repository": {
10
+ "url": "github:getpimms/analytics",
11
+ "directory": "packages/web"
12
+ },
13
+ "license": "MPL-2.0",
14
+ "type": "module",
15
+ "exports": {
16
+ "./package.json": "./package.json",
17
+ ".": {
18
+ "browser": "./dist/index.js",
19
+ "import": "./dist/index.js",
20
+ "require": "./dist/index.cjs"
21
+ },
22
+ "./react": {
23
+ "browser": "./dist/react/index.js",
24
+ "import": "./dist/react/index.js",
25
+ "require": "./dist/react/index.cjs"
26
+ }
27
+ },
28
+ "main": "dist/index.js",
29
+ "types": "dist/index.d.ts",
30
+ "typesVersions": {
31
+ "*": {
32
+ "*": [
33
+ "dist/index.d.ts"
34
+ ],
35
+ "react": [
36
+ "dist/react/index.d.ts"
37
+ ]
38
+ }
39
+ },
40
+ "eslintConfig": {
41
+ "extends": [
42
+ "@dub/eslint-config"
43
+ ],
44
+ "rules": {
45
+ "tsdoc/syntax": "off"
46
+ }
47
+ },
48
+ "dependencies": {
49
+ "server-only": "^0.0.1"
50
+ },
51
+ "devDependencies": {
52
+ "@dub/eslint-config": "0.0.0",
53
+ "@swc/core": "^1.3.66",
54
+ "@types/node": "^20.3.1",
55
+ "@types/react": "^18.2.14",
56
+ "react": "^18.2.0",
57
+ "react-dom": "^18.2.0",
58
+ "tsup": "7.1.0"
59
+ },
60
+ "publishConfig": {
61
+ "access": "public"
62
+ },
63
+ "scripts": {
64
+ "build": "tsup",
65
+ "dev": "tsup --watch",
66
+ "lint": "eslint .",
67
+ "lint-fix": "eslint . --fix",
68
+ "type-check": "tsc --noEmit"
69
+ }
70
+ }
package/src/generic.ts ADDED
@@ -0,0 +1,78 @@
1
+ import { name, version } from '../package.json';
2
+ import type { AnalyticsProps } from './types';
3
+ import { isBrowser } from './utils';
4
+
5
+ /**
6
+ * Injects the PIMMS Web Analytics script into the page head.
7
+ */
8
+ function inject(props: AnalyticsProps): void {
9
+ if (!isBrowser()) return;
10
+
11
+ // Determine script source based on enabled features
12
+ const baseUrl = 'https://cdn.pimms.io/analytics/script';
13
+ const features = [];
14
+
15
+ if (props.domainsConfig?.site) features.push('site-visit');
16
+ if (props.domainsConfig?.outbound) features.push('outbound-domains');
17
+
18
+ const src =
19
+ props.scriptProps?.src ||
20
+ (features.length > 0
21
+ ? `${baseUrl}.${features.join('.')}.js`
22
+ : `${baseUrl}.js`);
23
+
24
+ if (document.head.querySelector(`script[src*="${src}"]`)) return;
25
+
26
+ const script = document.createElement('script');
27
+ script.src = src;
28
+ script.defer = props.scriptProps?.defer ?? true;
29
+ script.setAttribute('data-sdkn', name);
30
+ script.setAttribute('data-sdkv', version);
31
+
32
+ if (props.apiHost) {
33
+ script.setAttribute('data-api-host', props.apiHost);
34
+ }
35
+
36
+ if (props.domainsConfig) {
37
+ script.setAttribute('data-domains', JSON.stringify(props.domainsConfig));
38
+ }
39
+
40
+ if (props.shortDomain) {
41
+ script.setAttribute('data-short-domain', props.shortDomain);
42
+ }
43
+
44
+ if (props.attributionModel) {
45
+ script.setAttribute('data-attribution-model', props.attributionModel);
46
+ }
47
+
48
+ if (props.cookieOptions && Object.keys(props.cookieOptions).length > 0) {
49
+ script.setAttribute(
50
+ 'data-cookie-options',
51
+ JSON.stringify(props.cookieOptions),
52
+ );
53
+ }
54
+
55
+ if (props.queryParam) {
56
+ script.setAttribute('data-query-param', props.queryParam);
57
+ }
58
+
59
+ if (props.scriptProps) {
60
+ const { src: _, ...restProps } = props.scriptProps; // we already set the src above
61
+ Object.assign(script, restProps);
62
+ }
63
+
64
+ script.onerror = (): void => {
65
+ // eslint-disable-next-line no-console -- Logging to console is intentional
66
+ console.log(`[PIMMS Web Analytics] failed to load script from ${src}.`);
67
+ };
68
+
69
+ document.head.appendChild(script);
70
+ }
71
+
72
+ export { inject };
73
+ export type { AnalyticsProps };
74
+
75
+ // eslint-disable-next-line import/no-default-export -- Default export is intentional
76
+ export default {
77
+ inject,
78
+ };
package/src/react.tsx ADDED
@@ -0,0 +1,30 @@
1
+ import { useEffect } from 'react';
2
+ import { inject } from './generic';
3
+ import type { AnalyticsProps } from './types';
4
+
5
+ /**
6
+ * Injects the PIMMS Web Analytics script into the page head.
7
+ * @param props - Analytics options.
8
+ * ```js
9
+ * import { Analytics as PimmsAnalytics } from '@getpimms/analytics/react';
10
+ *
11
+ * export default function App() {
12
+ * return (
13
+ * <div>
14
+ * <PimmsAnalytics />
15
+ * <h1>My App</h1>
16
+ * </div>
17
+ * );
18
+ * }
19
+ * ```
20
+ */
21
+ function Analytics(props: AnalyticsProps): null {
22
+ useEffect(() => {
23
+ inject(props);
24
+ }, [props]);
25
+
26
+ return null;
27
+ }
28
+
29
+ export { Analytics };
30
+ export type { AnalyticsProps };