@reactionary/google-analytics 0.3.1

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,11 @@
1
+ # google-analytics
2
+
3
+ This library was generated with [Nx](https://nx.dev).
4
+
5
+ ## Building
6
+
7
+ Run `nx build google-analytics` to build the library.
8
+
9
+ ## Running unit tests
10
+
11
+ Run `nx test google-analytics` to execute the unit tests via [Vitest](https://vitest.dev/).
@@ -0,0 +1,13 @@
1
+ import { GoogleAnalyticsAnalyticsProvider } from "../providers/analytics.provider.js";
2
+ function googleAnalyticsCapabilities(configuration, capabilities) {
3
+ return (cache, context) => {
4
+ const client = {};
5
+ if (capabilities.analytics) {
6
+ client.analytics = new GoogleAnalyticsAnalyticsProvider(cache, context, configuration);
7
+ }
8
+ return client;
9
+ };
10
+ }
11
+ export {
12
+ googleAnalyticsCapabilities
13
+ };
package/index.js ADDED
@@ -0,0 +1,4 @@
1
+ export * from "./core/initialize.js";
2
+ export * from "./providers/analytics.provider.js";
3
+ export * from "./schema/capabilities.schema.js";
4
+ export * from "./schema/configuration.schema.js";
package/package.json ADDED
@@ -0,0 +1,18 @@
1
+ {
2
+ "name": "@reactionary/google-analytics",
3
+ "version": "0.3.1",
4
+ "main": "index.js",
5
+ "types": "src/index.d.ts",
6
+ "dependencies": {
7
+ "@reactionary/core": "0.3.1",
8
+ "zod": "4.1.9"
9
+ },
10
+ "devDependencies": {
11
+ "vitest": "*",
12
+ "@vitest/ui": "*",
13
+ "@vitest/coverage-v8": "*",
14
+ "vite-tsconfig-paths": "*"
15
+ },
16
+ "type": "module",
17
+ "sideEffects": false
18
+ }
@@ -0,0 +1,127 @@
1
+ import {
2
+ AnalyticsProvider
3
+ } from "@reactionary/core";
4
+ class GoogleAnalyticsAnalyticsProvider extends AnalyticsProvider {
5
+ constructor(cache, context, configuration) {
6
+ super(cache, context);
7
+ this.config = configuration;
8
+ }
9
+ async processProductSummaryView(event) {
10
+ const gaEvent = {
11
+ client_id: this.context.session.identityContext.personalizationKey,
12
+ user_id: this.context.session.identityContext.personalizationKey,
13
+ events: [
14
+ {
15
+ name: "view_item_list",
16
+ params: {
17
+ currency: this.context.languageContext.currencyCode,
18
+ items: event.products.map((x) => {
19
+ return {
20
+ item_id: x.key
21
+ };
22
+ })
23
+ }
24
+ }
25
+ ]
26
+ };
27
+ await this.sendEvent(gaEvent);
28
+ }
29
+ async processProductSummaryClick(event) {
30
+ const gaEvent = {
31
+ client_id: this.context.session.identityContext.personalizationKey,
32
+ user_id: this.context.session.identityContext.personalizationKey,
33
+ events: [
34
+ {
35
+ name: "select_item",
36
+ params: {
37
+ currency: this.context.languageContext.currencyCode,
38
+ items: [
39
+ {
40
+ item_id: event.product.key,
41
+ index: event.position
42
+ }
43
+ ]
44
+ }
45
+ }
46
+ ]
47
+ };
48
+ await this.sendEvent(gaEvent);
49
+ }
50
+ async processProductDetailsView(event) {
51
+ const gaEvent = {
52
+ client_id: this.context.session.identityContext.personalizationKey,
53
+ user_id: this.context.session.identityContext.personalizationKey,
54
+ events: [
55
+ {
56
+ name: "view_item",
57
+ params: {
58
+ currency: this.context.languageContext.currencyCode,
59
+ items: [
60
+ {
61
+ item_id: event.product.key
62
+ }
63
+ ]
64
+ }
65
+ }
66
+ ]
67
+ };
68
+ await this.sendEvent(gaEvent);
69
+ }
70
+ async processProductAddToCart(event) {
71
+ const gaEvent = {
72
+ client_id: this.context.session.identityContext.personalizationKey,
73
+ user_id: this.context.session.identityContext.personalizationKey,
74
+ events: [
75
+ {
76
+ name: "add_to_cart",
77
+ params: {
78
+ currency: this.context.languageContext.currencyCode,
79
+ items: [
80
+ {
81
+ item_id: event.product.key
82
+ }
83
+ ]
84
+ }
85
+ }
86
+ ]
87
+ };
88
+ await this.sendEvent(gaEvent);
89
+ }
90
+ async processPurchase(event) {
91
+ const gaEvent = {
92
+ client_id: this.context.session.identityContext.personalizationKey,
93
+ user_id: this.context.session.identityContext.personalizationKey,
94
+ events: [
95
+ {
96
+ name: "purchase",
97
+ params: {
98
+ currency: this.context.languageContext.currencyCode,
99
+ transaction_id: event.order.identifier.key,
100
+ value: event.order.price.grandTotal.value,
101
+ tax: event.order.price.totalTax.value,
102
+ shipping: event.order.price.totalShipping.value,
103
+ items: event.order.items.map((item) => ({
104
+ item_id: item.variant.sku,
105
+ quantity: item.quantity,
106
+ price: item.price.unitPrice.value
107
+ }))
108
+ }
109
+ }
110
+ ]
111
+ };
112
+ await this.sendEvent(gaEvent);
113
+ }
114
+ async sendEvent(event) {
115
+ const url = `${this.config.url}?measurement_id=${this.config.measurementId}&api_secret=${this.config.apiSecret}`;
116
+ await fetch(url, {
117
+ method: "POST",
118
+ headers: {
119
+ "Content-Type": "application/json"
120
+ },
121
+ body: JSON.stringify(event)
122
+ });
123
+ }
124
+ }
125
+ export {
126
+ GoogleAnalyticsAnalyticsProvider
127
+ };
@@ -0,0 +1,7 @@
1
+ import { CapabilitiesSchema } from "@reactionary/core";
2
+ const GoogleAnalyticsCapabilitiesSchema = CapabilitiesSchema.pick({
3
+ analytics: true
4
+ }).partial();
5
+ export {
6
+ GoogleAnalyticsCapabilitiesSchema
7
+ };
@@ -0,0 +1,9 @@
1
+ import { z } from "zod";
2
+ const GoogleAnalyticsConfigurationSchema = z.looseObject({
3
+ url: z.string(),
4
+ measurementId: z.string(),
5
+ apiSecret: z.string()
6
+ });
7
+ export {
8
+ GoogleAnalyticsConfigurationSchema
9
+ };
@@ -0,0 +1,4 @@
1
+ import type { Cache, ClientFromCapabilities, RequestContext } from "@reactionary/core";
2
+ import type { GoogleAnalyticsCapabilities } from "../schema/capabilities.schema.js";
3
+ import type { GoogleAnalyticsConfiguration } from "../schema/configuration.schema.js";
4
+ export declare function googleAnalyticsCapabilities<T extends GoogleAnalyticsCapabilities>(configuration: GoogleAnalyticsConfiguration, capabilities: T): (cache: Cache, context: RequestContext) => ClientFromCapabilities<T>;
package/src/index.d.ts ADDED
@@ -0,0 +1,4 @@
1
+ export * from './core/initialize.js';
2
+ export * from './providers/analytics.provider.js';
3
+ export * from './schema/capabilities.schema.js';
4
+ export * from './schema/configuration.schema.js';
@@ -0,0 +1,12 @@
1
+ import { AnalyticsProvider, type RequestContext, type Cache, type AnalyticsMutationProductSummaryViewEvent, type AnalyticsMutationProductSummaryClickEvent, type AnalyticsMutationProductDetailsViewEvent, type AnalyticsMutationProductAddToCartEvent, type AnalyticsMutationPurchaseEvent } from '@reactionary/core';
2
+ import type { GoogleAnalyticsConfiguration } from '../schema/configuration.schema.js';
3
+ export declare class GoogleAnalyticsAnalyticsProvider extends AnalyticsProvider {
4
+ protected config: GoogleAnalyticsConfiguration;
5
+ constructor(cache: Cache, context: RequestContext, configuration: GoogleAnalyticsConfiguration);
6
+ protected processProductSummaryView(event: AnalyticsMutationProductSummaryViewEvent): Promise<void>;
7
+ protected processProductSummaryClick(event: AnalyticsMutationProductSummaryClickEvent): Promise<void>;
8
+ protected processProductDetailsView(event: AnalyticsMutationProductDetailsViewEvent): Promise<void>;
9
+ protected processProductAddToCart(event: AnalyticsMutationProductAddToCartEvent): Promise<void>;
10
+ protected processPurchase(event: AnalyticsMutationPurchaseEvent): Promise<void>;
11
+ protected sendEvent(event: unknown): Promise<void>;
12
+ }
@@ -0,0 +1,5 @@
1
+ import type { z } from 'zod';
2
+ export declare const GoogleAnalyticsCapabilitiesSchema: z.ZodObject<{
3
+ analytics: z.ZodOptional<z.ZodBoolean>;
4
+ }, z.core.$loose>;
5
+ export type GoogleAnalyticsCapabilities = z.infer<typeof GoogleAnalyticsCapabilitiesSchema>;
@@ -0,0 +1,7 @@
1
+ import { z } from 'zod';
2
+ export declare const GoogleAnalyticsConfigurationSchema: z.ZodObject<{
3
+ url: z.ZodString;
4
+ measurementId: z.ZodString;
5
+ apiSecret: z.ZodString;
6
+ }, z.core.$loose>;
7
+ export type GoogleAnalyticsConfiguration = z.infer<typeof GoogleAnalyticsConfigurationSchema>;