@fire-spark/analytics-server 0.0.0-stage → 0.26.0

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,55 @@
1
+ import { isValidEventId, validateEnvelope } from "@fire-spark/analytics";
2
+ //#region src/app.ts
3
+ const ENVELOPE_SDK = {
4
+ name: "analytics-server",
5
+ version: "1"
6
+ };
7
+ /** `runtimes` for `validateEnvelope` on the app relay. */
8
+ const APP_RUNTIMES = ["web", "react-native"];
9
+ const isRecord = (value) => {
10
+ return !!value && typeof value === "object" && !Array.isArray(value);
11
+ };
12
+ /** `context.runtime` of a validated event; `web` when absent. */
13
+ const runtimeOf = (event) => {
14
+ return event.context.runtime ?? "web";
15
+ };
16
+ /** `context.app` of a validated app event. */
17
+ const appContextOf = (event) => event.context.app;
18
+ const rejectionOf = (raw, reason) => {
19
+ const id = isRecord(raw) ? raw.event_id : void 0;
20
+ return isValidEventId(id) ? {
21
+ reason,
22
+ event_id: id
23
+ } : { reason };
24
+ };
25
+ const envelopeOf = (event, now) => {
26
+ return {
27
+ v: 1,
28
+ sdk: ENVELOPE_SDK,
29
+ sent_at: new Date(now).toISOString(),
30
+ events: [event]
31
+ };
32
+ };
33
+ /**
34
+ * Validates one app event with the contract validator (`react-native`
35
+ * runtime, `context.app` and the ATT policy). Only `app_context_missing`
36
+ * is told apart here; every other reason is the validator's.
37
+ */
38
+ const checkAppEvent = (raw, now, providers) => {
39
+ if (!isRecord(raw) || !isRecord(raw.context)) return rejectionOf(raw, "invalid_event");
40
+ if (raw.context.runtime !== "react-native") return rejectionOf(raw, "invalid_event");
41
+ if (raw.context.app === void 0 || raw.context.app === null) return rejectionOf(raw, "app_context_missing");
42
+ const options = {
43
+ now,
44
+ providers,
45
+ runtimes: APP_RUNTIMES
46
+ };
47
+ const result = validateEnvelope(envelopeOf(raw, now), options);
48
+ if (!result.ok) return rejectionOf(raw, "invalid_event");
49
+ const valid = result.envelope.events[0];
50
+ if (valid) return { event: valid };
51
+ const reason = result.rejected[0]?.reason ?? "invalid_event";
52
+ return rejectionOf(raw, reason);
53
+ };
54
+ //#endregion
55
+ export { runtimeOf as i, appContextOf as n, checkAppEvent as r, APP_RUNTIMES as t };
@@ -0,0 +1,112 @@
1
+ import { A as ServerProvider, R as JsonNumber, T as ResolvedEvent, i as HttpReply, t as AttemptOutcome } from "./retry-BRNUy4Of.js";
2
+ //#region src/google.types.d.ts
3
+ interface GoogleDataManagerOptions {
4
+ /** Service account e-mail (`client_email` of the JSON key). */
5
+ clientEmail: string;
6
+ /**
7
+ * PEM PKCS#8 private key (`private_key` of the JSON key). Escaped `\n`
8
+ * from an environment variable is accepted. Server only, never logged.
9
+ */
10
+ privateKey: string;
11
+ /** Google Ads customer id that owns the conversion actions. */
12
+ customerId: string;
13
+ /** Manager (MCC) account the service account reaches `customerId` by. */
14
+ loginCustomerId?: string;
15
+ /**
16
+ * Fire Spark event name → Google Ads conversion action id, for example
17
+ * `{ purchase: "987654321" }`. Other events are skipped (`not_mapped`).
18
+ */
19
+ conversionActions: Record<string, string>;
20
+ /** Sends `validate_only: true`: Google checks, records nothing. */
21
+ validateOnly?: boolean;
22
+ /** Keeps `validateOnly` in production. */
23
+ allowTestInProduction?: boolean;
24
+ }
25
+ /** Server-only environment variables read by `googleDataManagerFromEnv`. */
26
+ type GoogleDataManagerEnv = Record<string, string | undefined>;
27
+ //#endregion
28
+ //#region src/google-auth.d.ts
29
+ declare const GOOGLE_TOKEN_URL = "https://oauth2.googleapis.com/token";
30
+ /** The only scope requested: Data Manager, nothing else in the account. */
31
+ declare const DATA_MANAGER_SCOPE = "https://www.googleapis.com/auth/datamanager";
32
+ /** A cached token is refreshed this long before Google says it expires. */
33
+ declare const TOKEN_REFRESH_MARGIN_MS = 300000;
34
+ /** Token endpoint reply → token and lifetime, or an attempt outcome. */
35
+ declare const classifyTokenReply: (reply: HttpReply) => {
36
+ token: string;
37
+ expiresInMs: number;
38
+ } | {
39
+ outcome: AttemptOutcome;
40
+ };
41
+ //#endregion
42
+ //#region src/google.d.ts
43
+ /** Server-only variables `googleDataManagerFromEnv` reads. */
44
+ declare const GOOGLE_DATA_MANAGER_ENV_VARS: {
45
+ readonly serviceAccountJson: "FIRESPARK_GOOGLE_DATA_MANAGER_SERVICE_ACCOUNT_JSON";
46
+ readonly clientEmail: "FIRESPARK_GOOGLE_DATA_MANAGER_CLIENT_EMAIL";
47
+ readonly privateKey: "FIRESPARK_GOOGLE_DATA_MANAGER_PRIVATE_KEY";
48
+ readonly customerId: "FIRESPARK_GOOGLE_ADS_CUSTOMER_ID";
49
+ readonly loginCustomerId: "FIRESPARK_GOOGLE_ADS_LOGIN_CUSTOMER_ID";
50
+ readonly conversionActions: "FIRESPARK_GOOGLE_ADS_CONVERSION_ACTIONS";
51
+ readonly validateOnly: "FIRESPARK_GOOGLE_DATA_MANAGER_VALIDATE_ONLY";
52
+ };
53
+ /**
54
+ * One Data Manager event for a conversion action. `transaction_id` is the
55
+ * order id (what the Google tag sends, so Google deduplicates), else the
56
+ * Fire Spark event id.
57
+ */
58
+ declare const googleEventOf: (event: ResolvedEvent, actionId: string) => Partial<{
59
+ destination_references: string[];
60
+ transaction_id: string;
61
+ event_timestamp: string;
62
+ event_source: string;
63
+ currency: string | undefined;
64
+ conversion_value: JsonNumber | undefined;
65
+ user_data: Partial<{
66
+ user_identifiers: ({
67
+ email_address: string;
68
+ } | {
69
+ phone_number: string;
70
+ })[];
71
+ }>;
72
+ ad_identifiers: Partial<{
73
+ gclid: string | undefined;
74
+ gbraid: string | undefined;
75
+ wbraid: string | undefined;
76
+ session_attributes: string | undefined;
77
+ }>;
78
+ event_device_info: Partial<{
79
+ user_agent: string | undefined;
80
+ ip_address: string | undefined;
81
+ }>;
82
+ consent: {
83
+ ad_user_data: string;
84
+ ad_personalization: string;
85
+ };
86
+ cart_data: {
87
+ items: Partial<{
88
+ merchant_product_id: string;
89
+ quantity: JsonNumber | undefined;
90
+ unit_price: JsonNumber | undefined;
91
+ }>[];
92
+ } | undefined;
93
+ }>;
94
+ /** Data Manager response → outcome. Google's error text is never kept. */
95
+ declare const classifyGoogleReply: (reply: HttpReply) => AttemptOutcome;
96
+ /**
97
+ * Google Data Manager destination (`events:ingest`) for Google Ads
98
+ * conversions. Authenticates as a service account with the Data Manager
99
+ * scope only. Invalid options never throw: the provider fails closed (see
100
+ * `disabledProvider`). The key and tokens are never logged.
101
+ */
102
+ declare const googleDataManager: (options: GoogleDataManagerOptions) => ServerProvider;
103
+ /** Options from the server-only variables in `GOOGLE_DATA_MANAGER_ENV_VARS`. */
104
+ declare const googleDataManagerOptionsFromEnv: (env: GoogleDataManagerEnv) => GoogleDataManagerOptions;
105
+ /**
106
+ * `googleDataManager` configured from server-only environment variables
107
+ * (pass `process.env`, or the Worker `env`). Never throws: a missing or
108
+ * invalid variable makes the provider fail closed.
109
+ */
110
+ declare const googleDataManagerFromEnv: (env: GoogleDataManagerEnv) => ServerProvider;
111
+ //#endregion
112
+ export { DATA_MANAGER_SCOPE, GOOGLE_DATA_MANAGER_ENV_VARS, GOOGLE_TOKEN_URL, type GoogleDataManagerEnv, type GoogleDataManagerOptions, TOKEN_REFRESH_MARGIN_MS, classifyGoogleReply, classifyTokenReply, googleDataManager, googleDataManagerFromEnv, googleDataManagerOptionsFromEnv, googleEventOf };