@ogcio/analytics-sdk 0.0.1-beta.1 → 0.0.1-beta.3

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 (83) hide show
  1. package/dist/base/base-client.d.ts +2 -7
  2. package/dist/base/base-client.d.ts.map +1 -1
  3. package/dist/base/base-client.js +3 -15
  4. package/dist/base/base-client.js.map +1 -0
  5. package/{types.d.ts → dist/browser-types.d.ts} +1 -1
  6. package/dist/index.d.ts +400 -597
  7. package/dist/index.d.ts.map +1 -1
  8. package/dist/index.js +332 -198
  9. package/dist/index.js.map +1 -0
  10. package/dist/matomo/helpers.d.ts +15 -0
  11. package/dist/matomo/helpers.d.ts.map +1 -1
  12. package/dist/matomo/helpers.js +80 -0
  13. package/dist/matomo/helpers.js.map +1 -0
  14. package/dist/matomo/types.d.ts +6 -2
  15. package/dist/matomo/types.d.ts.map +1 -1
  16. package/dist/matomo/types.js +1 -0
  17. package/dist/matomo/types.js.map +1 -0
  18. package/dist/schema.d.ts +223 -387
  19. package/dist/schema.d.ts.map +1 -1
  20. package/dist/schema.js +1 -0
  21. package/dist/schema.js.map +1 -0
  22. package/dist/support-files/auth/index.js +1 -0
  23. package/dist/support-files/auth/index.js.map +1 -0
  24. package/dist/support-files/auth/types.d.ts +1 -1
  25. package/dist/support-files/auth/types.d.ts.map +1 -1
  26. package/dist/support-files/auth/types.js +1 -0
  27. package/dist/support-files/auth/types.js.map +1 -0
  28. package/dist/support-files/building-blocks-sdk.d.ts +1 -1
  29. package/dist/support-files/building-blocks-sdk.d.ts.map +1 -1
  30. package/dist/support-files/building-blocks-sdk.js +1 -0
  31. package/dist/support-files/building-blocks-sdk.js.map +1 -0
  32. package/dist/utils/index.js +1 -0
  33. package/dist/utils/index.js.map +1 -0
  34. package/package.json +24 -16
  35. package/dist/ogcio-analytics-sdk-0.0.1.tgz +0 -0
  36. package/dist/package/dist/base/base-client.d.ts +0 -36
  37. package/dist/package/dist/base/base-client.d.ts.map +0 -1
  38. package/dist/package/dist/base/base-client.js +0 -96
  39. package/dist/package/dist/index.d.ts +0 -651
  40. package/dist/package/dist/index.d.ts.map +0 -1
  41. package/dist/package/dist/index.js +0 -321
  42. package/dist/package/dist/matomo/helpers.d.ts +0 -9
  43. package/dist/package/dist/matomo/helpers.d.ts.map +0 -1
  44. package/dist/package/dist/matomo/helpers.js +0 -10
  45. package/dist/package/dist/matomo/types.d.ts +0 -33
  46. package/dist/package/dist/matomo/types.d.ts.map +0 -1
  47. package/dist/package/dist/matomo/types.js +0 -1
  48. package/dist/package/dist/schema.d.ts +0 -1295
  49. package/dist/package/dist/schema.d.ts.map +0 -1
  50. package/dist/package/dist/schema.js +0 -5
  51. package/dist/package/dist/support-files/auth/index.d.ts +0 -3
  52. package/dist/package/dist/support-files/auth/index.d.ts.map +0 -1
  53. package/dist/package/dist/support-files/auth/index.js +0 -76
  54. package/dist/package/dist/support-files/auth/types.d.ts +0 -31
  55. package/dist/package/dist/support-files/auth/types.d.ts.map +0 -1
  56. package/dist/package/dist/support-files/auth/types.js +0 -1
  57. package/dist/package/dist/support-files/building-blocks-sdk.d.ts +0 -17
  58. package/dist/package/dist/support-files/building-blocks-sdk.d.ts.map +0 -1
  59. package/dist/package/dist/support-files/building-blocks-sdk.js +0 -11
  60. package/dist/package/dist/utils/index.d.ts +0 -7
  61. package/dist/package/dist/utils/index.d.ts.map +0 -1
  62. package/dist/package/dist/utils/index.js +0 -19
  63. package/dist/package/examples/environment.d.ts +0 -11
  64. package/dist/package/examples/index.ts +0 -201
  65. package/dist/package/examples/tsconfig.json +0 -15
  66. package/dist/package/package.json +0 -56
  67. package/dist/package/tsconfig.json +0 -18
  68. package/dist/package/types.d.ts +0 -19
  69. package/examples/environment.d.ts +0 -11
  70. package/examples/index.ts +0 -201
  71. package/examples/tsconfig.json +0 -15
  72. package/src/base/base-client.ts +0 -172
  73. package/src/index.test.ts +0 -92
  74. package/src/index.ts +0 -449
  75. package/src/matomo/helpers.ts +0 -14
  76. package/src/matomo/types.ts +0 -48
  77. package/src/schema.ts +0 -1295
  78. package/src/support-files/auth/index.ts +0 -116
  79. package/src/support-files/auth/types.ts +0 -38
  80. package/src/support-files/building-blocks-sdk.ts +0 -32
  81. package/src/utils/index.test.ts +0 -46
  82. package/src/utils/index.ts +0 -20
  83. package/tsconfig.json +0 -18
package/examples/index.ts DELETED
@@ -1,201 +0,0 @@
1
- /*
2
- This file is used as temporary examples for the SDK and will be removed in the future.
3
- */
4
-
5
- import getM2MTokenFn from "../src/support-files/auth/index.js";
6
- import getBuildingBlockSDK from "../src/support-files/building-blocks-sdk.js";
7
-
8
- const setup = async () => {
9
- const getTokenFn = await getM2MTokenFn({
10
- services: {
11
- analytics: {
12
- getOrganizationTokenParams: {
13
- applicationId: process.env.AUTH_APP_ID!,
14
- applicationSecret: process.env.AUTH_APP_SECRET!,
15
- logtoOidcEndpoint: process.env.AUTH_URL!,
16
- organizationId: process.env.AUTH_ORGANIZATION_ID!,
17
- scopes: [
18
- "analytics:website:read",
19
- "analytics:website:write",
20
- "analytics:website:tracking",
21
- ],
22
- },
23
- },
24
- },
25
- });
26
-
27
- const sdk = getBuildingBlockSDK({
28
- getTokenFn,
29
- services: {
30
- analytics: {
31
- baseUrl: process.env.ANALYTICS_URL!,
32
- organizationId: process.env.AUTH_ORGANIZATION_ID!,
33
- },
34
- },
35
- });
36
-
37
- // Create a new website
38
- const { data, error } = await sdk.analytics.createWebsite({
39
- name: "My Website",
40
- domain: "mywebsite.com",
41
- tracker_url: "https://mywebsite.com/tracker",
42
- cookieless: true,
43
- bypass_adblockers: true,
44
- track_page_view: true,
45
- track_bots: true,
46
- link_tracking: true,
47
- heart_beat_timer: 10,
48
- browser_feature_detection: true,
49
- custom_dimensions: {
50
- dimension1: "My Custom Dimension",
51
- },
52
- site_ids: [1],
53
- });
54
-
55
- // eslint-disable-next-line @typescript-eslint/no-non-null-asserted-optional-chain
56
- const website = data?.data!;
57
-
58
- console.log("ℹ️ Create a new website", website, error);
59
-
60
- console.log("ℹ️ Get all websites", (await sdk.analytics.getWebsites()).data);
61
-
62
- console.log(
63
- "ℹ️ Get website by id",
64
- (await sdk.analytics.getWebsiteById(website.id)).data,
65
- );
66
-
67
- console.log(
68
- "ℹ️ Update website",
69
- (
70
- await sdk.analytics.updateWebsite(website.id, {
71
- name: "My Website Updated",
72
- })
73
- ).data,
74
- );
75
-
76
- sdk.analytics.setTrackingWebsiteId(website.id);
77
-
78
- console.log(
79
- "ℹ️ Get Tracking Code",
80
- (
81
- await sdk.analytics.generateTrackingCode({
82
- browser_feature_detection: false,
83
- })
84
- ).data,
85
- );
86
-
87
- console.log(
88
- "ℹ️ Generate Track Event Code",
89
- (
90
- await sdk.analytics.generateTrackEventCode({
91
- event: {
92
- category: "My Event",
93
- action: "My Event Action",
94
- name: "My Event Name",
95
- value: 1,
96
- },
97
- })
98
- ).data,
99
- );
100
-
101
- sdk.analytics.setTrackingMetadata({
102
- userId: "My User ID 123",
103
- userAgent: "My User Agent",
104
- referrer: "My Referrer",
105
- language: "My Language",
106
- screenResolution: "My Screen Resolution",
107
- });
108
-
109
- console.log(
110
- "ℹ️ Track event",
111
- (
112
- await sdk.analytics.trackEvent({
113
- event: {
114
- category: "My Event",
115
- action: "My Event Action",
116
- name: "My Event Name",
117
- value: 1,
118
- },
119
- metadata: {
120
- userId: "My Custom User ID 123",
121
- userAgent: "My Custom User Agent",
122
- referrer: "My Custom Referrer",
123
- language: "My Custom Language",
124
- screenResolution: "My Custom Screen Resolution",
125
- },
126
- })
127
- ).data,
128
- );
129
-
130
- console.log(
131
- "ℹ️ Generate Track Page View Code",
132
- (
133
- await sdk.analytics.generateTrackPageViewCode({
134
- event: {
135
- action_name: "My Page View",
136
- },
137
- })
138
- ).data,
139
- );
140
-
141
- console.log(
142
- "ℹ️ Track page view",
143
- (
144
- await sdk.analytics.trackPageView({
145
- event: {
146
- action_name: "My Page View",
147
- },
148
- })
149
- ).data,
150
- );
151
-
152
- console.log(
153
- "ℹ️ Generate Track Site Search Code",
154
- (
155
- await sdk.analytics.generateTrackSiteSearchCode({
156
- event: {
157
- search_keyword: "My Site Search",
158
- search_count: 1,
159
- search_category: "My Search Category",
160
- },
161
- })
162
- ).data,
163
- );
164
-
165
- console.log(
166
- "ℹ️ Track site search",
167
- (
168
- await sdk.analytics.trackSiteSearch({
169
- event: {
170
- search_keyword: "My Site Search",
171
- search_count: 1,
172
- search_category: "My Search Category",
173
- },
174
- })
175
- ).data,
176
- );
177
-
178
- console.log(
179
- "ℹ️ Generate Track Outlink Code",
180
- (
181
- await sdk.analytics.generateTrackOutLinkCode({
182
- event: {
183
- link: "https://example.com",
184
- },
185
- })
186
- ).data,
187
- );
188
-
189
- console.log(
190
- "ℹ️ Track Outlink",
191
- (
192
- await sdk.analytics.trackOutLink({
193
- event: {
194
- link: "https://example.com",
195
- },
196
- })
197
- ).data,
198
- );
199
- };
200
-
201
- setup();
@@ -1,15 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "target": "ESNext",
4
- "module": "NodeNext",
5
- "moduleResolution": "NodeNext",
6
- "esModuleInterop": true,
7
- "forceConsistentCasingInFileNames": true,
8
- "strict": true,
9
- "skipLibCheck": true,
10
- "declaration": true,
11
- "declarationMap": true,
12
- "experimentalDecorators": true,
13
- "types": ["node", "environment.d.ts"]
14
- }
15
- }
@@ -1,172 +0,0 @@
1
- import createClient, {
2
- type Middleware,
3
- type FetchResponse,
4
- ParseAsResponse,
5
- } from "openapi-fetch";
6
- import type {
7
- ErrorStatus,
8
- FilterKeys,
9
- OkStatus,
10
- ResponseContent,
11
- ResponseObjectMap,
12
- } from "openapi-typescript-helpers";
13
-
14
- export const ANALYTICS = "analytics" as const;
15
-
16
- export type SERVICE_NAME = typeof ANALYTICS;
17
-
18
- export type TokenFunction = (
19
- serviceName: SERVICE_NAME,
20
- ) => Promise<string> | string;
21
-
22
- export type SDKClientParams = {
23
- baseUrl?: string;
24
- };
25
-
26
- export type BaseApiClientParams = SDKClientParams & {
27
- getTokenFn?: TokenFunction;
28
- };
29
-
30
- export type DataResponseValue<T, O> = ParseAsResponse<
31
- FilterKeys<
32
- ResponseContent<FilterKeys<ResponseObjectMap<T>, OkStatus>>,
33
- "application/json"
34
- >,
35
- O
36
- > & {
37
- error: ParseAsResponse<
38
- FilterKeys<
39
- ResponseContent<FilterKeys<ResponseObjectMap<T>, ErrorStatus>>,
40
- "application/json"
41
- >,
42
- O
43
- >;
44
- };
45
-
46
- /**
47
- * This decorator is used to check if the baseUrl or token of the client are set.
48
- * If the baseUrl or token are not set, it throws an error
49
- */
50
- export function needsBackend(
51
- target: unknown,
52
- propertyKey: string,
53
- descriptor: PropertyDescriptor,
54
- ) {
55
- const originalMethod = descriptor.value;
56
- descriptor.value = async function (
57
- this: BaseClient<never>,
58
- ...args: unknown[]
59
- ) {
60
- this.checkBackend();
61
- return originalMethod.apply(this, args);
62
- };
63
- return descriptor;
64
- }
65
-
66
- abstract class BaseClient<T extends object> {
67
- private baseUrl?: string;
68
- private initialized;
69
-
70
- protected token?: string;
71
- protected getTokenFn?: TokenFunction;
72
- protected serviceName: SERVICE_NAME | undefined;
73
-
74
- protected client: ReturnType<typeof createClient<T>>;
75
-
76
- constructor({ baseUrl, getTokenFn }: BaseApiClientParams) {
77
- this.initialized = false;
78
- if (baseUrl) {
79
- this.baseUrl = baseUrl;
80
- this.initialized = true;
81
- }
82
-
83
- if (getTokenFn) {
84
- this.getTokenFn = getTokenFn;
85
- }
86
-
87
- this.token = undefined;
88
- this.client = createClient<T>({ baseUrl: this.baseUrl });
89
- const authMiddleware: Middleware = {
90
- onRequest: async ({ request }) => {
91
- if (!this.token && this.getTokenFn) {
92
- this.token = await this.getTokenFn(this.serviceName as SERVICE_NAME);
93
- }
94
-
95
- if (this.token) {
96
- request.headers.set("Authorization", `Bearer ${this.token}`);
97
- }
98
- return request;
99
- },
100
- };
101
-
102
- this.client.use(authMiddleware);
103
- }
104
-
105
- public deleteToken() {
106
- this.token = undefined;
107
- }
108
-
109
- protected async getToken() {
110
- if (this.getTokenFn) {
111
- const token = await this.getTokenFn(this.serviceName as SERVICE_NAME);
112
- this.token = token;
113
- }
114
-
115
- return this.token;
116
- }
117
-
118
- public isInitialized() {
119
- return this.initialized;
120
- }
121
-
122
- protected formatResponse<G extends Record<string, unknown>, O>(
123
- response: FetchResponse<G, O, "application/json">,
124
- ): DataResponseValue<G, O> {
125
- let outputData = undefined;
126
- let outputMetadata = undefined;
127
- if (!response) {
128
- return {} as DataResponseValue<G, O>;
129
- }
130
- if (response.data) {
131
- const dataEntries = Object.entries(response.data);
132
- // by docs the body should contain a "data"
133
- // properties with the response values
134
- const containsData = dataEntries.find((x) => x[0] === "data");
135
- const containsMetadata = dataEntries.find((x) => x[0] === "metadata");
136
-
137
- if (containsMetadata) {
138
- outputMetadata = containsMetadata[1];
139
- }
140
- outputData = containsData ? containsData[1] : response.data;
141
- }
142
-
143
- return {
144
- data: outputData,
145
- metadata: outputMetadata,
146
- error: response.error,
147
- } as unknown as DataResponseValue<G, O>;
148
- }
149
-
150
- protected formatError<G, O>(reason: unknown): DataResponseValue<G, O> {
151
- return { error: reason } as unknown as DataResponseValue<G, O>;
152
- }
153
-
154
- /* checkBackend is a protected method used by needsBackend decorator */
155
- protected async checkBackend() {
156
- const className = this.constructor.name;
157
-
158
- if (!this.baseUrl) {
159
- throw new Error(
160
- `${className} baseUrl is not set. Please provide a valid baseUrl when initializing ${className} SDK methods.`,
161
- );
162
- }
163
-
164
- if (!this.token && !this.getTokenFn) {
165
- throw new Error(
166
- `${className} token is not set. Please provide a valid getTokenFn function when initializing ${className} SDK methods.`,
167
- );
168
- }
169
- }
170
- }
171
-
172
- export default BaseClient;
package/src/index.test.ts DELETED
@@ -1,92 +0,0 @@
1
- import t from "tap";
2
- import { Analytics } from "./index.js";
3
-
4
- t.test("Analytics - Dry Run and Opt Out", async (t) => {
5
- const baseUrl = "http://example.com";
6
- const trackingWebsiteId = "123";
7
-
8
- t.test("Analytics Config", async (t) => {
9
- const analytics = new Analytics({
10
- baseUrl,
11
- trackingWebsiteId,
12
- getTokenFn: () => "token",
13
- });
14
- analytics.setAnalyticsConfig({ dryRun: true, optOut: false });
15
-
16
- let config = analytics.getAnalyticsConfig();
17
- t.equal(config.dryRun, true, "Dry run should be set to true");
18
- t.equal(config.optOut, false, "Opt out should be set to false");
19
-
20
- analytics.setAnalyticsConfig({ dryRun: false, optOut: true });
21
-
22
- config = analytics.getAnalyticsConfig();
23
- t.equal(config.dryRun, false, "Dry run should be set to false");
24
- t.equal(config.optOut, true, "Opt out should be set to true");
25
- });
26
-
27
- t.test("Dry Run", async (t) => {
28
- const analytics = new Analytics({
29
- baseUrl,
30
- trackingWebsiteId,
31
- getTokenFn: () => "token",
32
- });
33
- analytics.setDryRun(true);
34
-
35
- const consoleSpy = t.capture(console, "warn");
36
- const result = await analytics.trackEvent({
37
- event: { category: "test_event", action: "test_event" },
38
- });
39
-
40
- t.equal(
41
- result?.data,
42
- undefined,
43
- "No data should be returned in dry run mode",
44
- );
45
- t.equal(
46
- result?.error,
47
- undefined,
48
- "No error should be returned in dry run mode",
49
- );
50
-
51
- t.match(
52
- consoleSpy.calls[0].args[0],
53
- "Dry run is enabled, skipping request",
54
- "Console warning should be logged",
55
- );
56
-
57
- analytics.setDryRun(false);
58
- });
59
-
60
- t.test("Opt Out", async (t) => {
61
- const analytics = new Analytics({
62
- baseUrl,
63
- trackingWebsiteId,
64
- getTokenFn: () => "token",
65
- });
66
-
67
- analytics.setOptOut(true);
68
-
69
- const consoleSpy = t.capture(console, "warn");
70
- const result = await analytics.trackEvent({
71
- event: { category: "test_event", action: "test_event" },
72
- });
73
-
74
- t.equal(
75
- result?.data,
76
- undefined,
77
- "No data should be returned when opted out",
78
- );
79
- t.equal(
80
- result?.error,
81
- undefined,
82
- "No error should be returned when opted out",
83
- );
84
- t.match(
85
- consoleSpy.calls[0].args[0],
86
- "Opt out is enabled, skipping request",
87
- "Console warning should be logged",
88
- );
89
-
90
- analytics.setOptOut(false);
91
- });
92
- });