@fire-spark/analytics-server 0.29.0 → 0.30.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.
package/dist/google.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { A as ServerProvider, R as JsonNumber, T as ResolvedEvent, i as HttpReply, t as AttemptOutcome } from "./retry-BRNUy4Of.js";
1
+ import { A as ServerProvider, R as JsonNumber, T as ResolvedEvent, i as HttpReply, t as AttemptOutcome } from "./retry-D6beZds5.js";
2
2
  //#region src/google.types.d.ts
3
3
  interface GoogleDataManagerOptions {
4
4
  /** Service account e-mail (`client_email` of the JSON key). */
package/dist/google.js CHANGED
@@ -239,9 +239,12 @@ const consentFor = (event) => {
239
239
  ad_personalization: consentOf(personalization)
240
240
  };
241
241
  };
242
- /** Hashed e-mail and E.164 phone, only with `ad_user_data` consent. */
242
+ /** `ad_user_data` (default `ads`); without it Google gets no event. */
243
+ const userDataAllowed = (event) => {
244
+ return (event.consent.ad_user_data ?? event.consent.ads) !== false;
245
+ };
246
+ /** Hashed e-mail and E.164 phone. */
243
247
  const userIdentifiersOf = (event) => {
244
- if ((event.consent.ad_user_data ?? event.consent.ads) === false) return [];
245
248
  const { user } = event;
246
249
  const email = user.email_google_sha256 ?? user.email_sha256;
247
250
  const b1 = email ? [{ email_address: email }] : [];
@@ -536,6 +539,8 @@ const googleDataManager = (options) => {
536
539
  });
537
540
  };
538
541
  for (const event of events) if (!actions.has(event.name)) skip(event, "not_mapped");
542
+ else if (!userDataAllowed(event)) skip(event, "consent_denied");
543
+ else if (event.value !== void 0 && !event.currency) skip(event, "invalid_payload");
539
544
  else if (!hasIdentifier(event)) skip(event, "no_identifier");
540
545
  else ready.push(event);
541
546
  return {
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { A as ServerProvider, B as decimalText, C as RelayRejectedEvent, D as ServerEventInput, E as RetryPolicy, F as TrackingHandlerOptions, I as VerifyEvent, L as VerifyEventContext, M as ServerTracker, N as ServerTrackerOptions, O as ServerIssue, P as ServerUserInput, R as JsonNumber, S as RelayRejectReason, T as ResolvedEvent, V as stringifyJson, _ as ProviderReason, a as backoffDelay, b as RelayErrorBody, c as AnalyticsServerConfigError, d as AppTrackingHandlerOptions, f as DeliveryOptions, g as ProviderOutcome, h as ProviderContext, i as HttpReply, j as ServerTrackResult, k as ServerIssueSource, l as AnalyticsServerConfigErrorCode, m as PreparedEvents, n as DEFAULT_RETRY_POLICY, o as postJson, p as IdempotencyStore, r as FinalOutcome, s as withProviderRetry, t as AttemptOutcome, u as AppAuthentication, v as ProviderResult, w as RequestMeta, x as RelayErrorCode, y as RelayAcceptedBody, z as decimalNumber } from "./retry-BRNUy4Of.js";
1
+ import { A as ServerProvider, B as decimalText, C as RelayRejectedEvent, D as ServerEventInput, E as RetryPolicy, F as TrackingHandlerOptions, I as VerifyEvent, L as VerifyEventContext, M as ServerTracker, N as ServerTrackerOptions, O as ServerIssue, P as ServerUserInput, R as JsonNumber, S as RelayRejectReason, T as ResolvedEvent, V as stringifyJson, _ as ProviderReason, a as backoffDelay, b as RelayErrorBody, c as AnalyticsServerConfigError, d as AppTrackingHandlerOptions, f as DeliveryOptions, g as ProviderOutcome, h as ProviderContext, i as HttpReply, j as ServerTrackResult, k as ServerIssueSource, l as AnalyticsServerConfigErrorCode, m as PreparedEvents, n as DEFAULT_RETRY_POLICY, o as postJson, p as IdempotencyStore, r as FinalOutcome, s as withProviderRetry, t as AttemptOutcome, u as AppAuthentication, v as ProviderResult, w as RequestMeta, x as RelayErrorCode, y as RelayAcceptedBody, z as decimalNumber } from "./retry-D6beZds5.js";
2
2
  import { SUPPORTED_CONTRACT_VERSIONS, TrackingAppContextV1, TrackingEventV1, normalizeEmail, normalizeEmailForGoogle, normalizePhoneE164, phoneDigits, sha256Hex, upgradeEnvelope, validateEnvelope } from "@fire-spark/analytics";
3
3
  //#region src/handler.d.ts
4
4
  declare const DEFAULT_MAX_BODY_BYTES = 65536;
package/dist/index.js CHANGED
@@ -6,10 +6,16 @@ import { HASHED_USER_WIRE_KEYS, MAX_ENVELOPE_EVENTS, SERVER_PROVIDER_IDS, SUPPOR
6
6
  const processEnv = () => {
7
7
  return globalThis.process?.env ?? {};
8
8
  };
9
- /** `VERCEL_ENV` or `NODE_ENV` is `production`. */
9
+ /**
10
+ * `VERCEL_ENV` is `production`; without `VERCEL_ENV`, `NODE_ENV` is. A
11
+ * Vercel Preview builds with `NODE_ENV=production`, so `NODE_ENV` alone
12
+ * would drop its test event codes and send QA traffic to live reports.
13
+ */
10
14
  const detectProduction = () => {
11
15
  const env = processEnv();
12
- return env.VERCEL_ENV === "production" || env.NODE_ENV === "production";
16
+ const vercel = env.VERCEL_ENV?.trim();
17
+ if (vercel) return vercel === "production";
18
+ return env.NODE_ENV === "production";
13
19
  };
14
20
  const defaultSleep = (ms, signal) => {
15
21
  return new Promise((resolve) => {
@@ -248,7 +254,8 @@ const claimOne = async (pipeline, event) => {
248
254
  }
249
255
  };
250
256
  /**
251
- * Consent, verification and idempotency, in that order. Throws
257
+ * Repeats in the batch, consent, verification and idempotency, in that
258
+ * order. A repeated `event_id` keeps its first copy only. Throws
252
259
  * `VerifyFailure` when `verifyEvent` throws, before anything is claimed.
253
260
  */
254
261
  const admitEvents = async (pipeline, events, options) => {
@@ -263,7 +270,15 @@ const admitEvents = async (pipeline, events, options) => {
263
270
  const providerReason = reason;
264
271
  skipped.push(...skippedFor(pipeline, event, providerReason));
265
272
  };
273
+ const seen = /* @__PURE__ */ new Set();
266
274
  const consented = events.filter((event) => {
275
+ if (seen.has(event.event_id)) {
276
+ skipped.push(...skippedFor(pipeline, event, "duplicate"));
277
+ return false;
278
+ }
279
+ seen.add(event.event_id);
280
+ return true;
281
+ }).filter((event) => {
267
282
  if (event.consent.ads) return true;
268
283
  reject(event, "consent_denied");
269
284
  return false;
package/dist/meta.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { A as ServerProvider, R as JsonNumber, T as ResolvedEvent, i as HttpReply, t as AttemptOutcome } from "./retry-BRNUy4Of.js";
1
+ import { A as ServerProvider, R as JsonNumber, T as ResolvedEvent, i as HttpReply, t as AttemptOutcome } from "./retry-D6beZds5.js";
2
2
  import "@fire-spark/analytics";
3
3
  //#region src/meta.types.d.ts
4
4
  interface MetaDataProcessingOptions {
@@ -130,7 +130,7 @@ declare const metaEventOf: (event: ResolvedEvent, options: MetaConversionsApiOpt
130
130
  id: string;
131
131
  quantity: JsonNumber | undefined;
132
132
  item_price: JsonNumber | undefined;
133
- }>[];
133
+ }>[] | undefined;
134
134
  num_items: JsonNumber | undefined;
135
135
  }>;
136
136
  }>>;
package/dist/meta.js CHANGED
@@ -31,6 +31,23 @@ const contentsOf = (event) => {
31
31
  });
32
32
  });
33
33
  };
34
+ /** Whole units; a fractional quantity (for example `"1.5"` kg) is `false`. */
35
+ const isWhole = (quantity) => {
36
+ if (quantity === void 0) return true;
37
+ try {
38
+ return new Big(quantity).mod(1).eq(0);
39
+ } catch {
40
+ return false;
41
+ }
42
+ };
43
+ /**
44
+ * Meta wants integer `contents[].quantity` and `num_items`: true when the
45
+ * event has items and every quantity is whole, so nothing is rounded.
46
+ */
47
+ const hasWholeItems = (event) => {
48
+ const items = event.items ?? [];
49
+ return items.length > 0 && items.every((item) => isWhole(item.quantity));
50
+ };
34
51
  /** Sum of item quantities (an item without one counts as 1), unrounded. */
35
52
  const numItemsOf = (event) => {
36
53
  const items = event.items ?? [];
@@ -250,24 +267,13 @@ const userDataOf$1 = async (event, app) => {
250
267
  ...u8
251
268
  });
252
269
  };
253
- /** Whole units; a fractional quantity (for example `"1.5"` kg) is `false`. */
254
- const isWhole = (quantity) => {
255
- if (quantity === void 0) return true;
256
- try {
257
- return new Big(quantity).mod(1).eq(0);
258
- } catch {
259
- return false;
260
- }
261
- };
262
270
  /**
263
271
  * `contents[].quantity` and `num_items` are integers for Meta, so a
264
272
  * fractional quantity drops both rather than rounding; `value` keeps the
265
273
  * exact total.
266
274
  */
267
275
  const customDataOf$1 = (event) => {
268
- const items = event.items ?? [];
269
- const whole = items.every((item) => isWhole(item.quantity));
270
- const priced = items.length > 0 && whole;
276
+ const priced = hasWholeItems(event);
271
277
  const c1 = {
272
278
  currency: event.currency,
273
279
  value: decimalNumber(event.value)
@@ -379,26 +385,30 @@ const metaAppConversionsApi = (options) => {
379
385
  //#endregion
380
386
  //#region src/meta.ts
381
387
  const PROVIDER = META_PROVIDER;
388
+ /**
389
+ * `content_ids` always name the items; `contents` and `num_items` need
390
+ * whole quantities (Meta rejects a fractional one), so `"1.5"` kg drops
391
+ * both instead of rounding. `value` keeps the exact total.
392
+ */
382
393
  const customDataOf = (event) => {
383
394
  const items = event.items ?? [];
384
- const priced = items.length > 0;
395
+ const whole = hasWholeItems(event);
385
396
  const c1 = {
386
397
  currency: event.currency,
387
398
  value: decimalText(event.value)
388
399
  };
389
400
  const c2 = { order_id: event.order_id };
390
- const c3 = { content_type: priced ? "product" : void 0 };
401
+ const c3 = { content_type: items.length > 0 ? "product" : void 0 };
391
402
  const c4 = { content_ids: items.map((item) => item.id) };
392
- const c5 = {
393
- contents: contentsOf(event),
394
- num_items: numItemsOf(event)
395
- };
403
+ const c5 = { contents: whole ? contentsOf(event) : void 0 };
404
+ const c6 = { num_items: whole ? numItemsOf(event) : void 0 };
396
405
  return compact({
397
406
  ...c1,
398
407
  ...c2,
399
408
  ...c3,
400
409
  ...c4,
401
- ...c5
410
+ ...c5,
411
+ ...c6
402
412
  });
403
413
  };
404
414
  const userDataOf = async (event) => {
@@ -159,7 +159,7 @@ interface DeliveryOptions {
159
159
  onResult?: (results: ProviderResult[]) => void;
160
160
  /** Default: global `fetch`. */
161
161
  fetch?: typeof fetch;
162
- /** Default: `VERCEL_ENV` or `NODE_ENV` is `production`. */
162
+ /** Default: `VERCEL_ENV` is `production` (else `NODE_ENV`). */
163
163
  isProduction?: boolean;
164
164
  /** Epoch ms. Default `Date.now`. */
165
165
  now?: () => number;
package/dist/tiktok.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { A as ServerProvider, R as JsonNumber, T as ResolvedEvent, _ as ProviderReason, i as HttpReply, t as AttemptOutcome } from "./retry-BRNUy4Of.js";
1
+ import { A as ServerProvider, R as JsonNumber, T as ResolvedEvent, _ as ProviderReason, i as HttpReply, t as AttemptOutcome } from "./retry-D6beZds5.js";
2
2
  //#region src/tiktok.types.d.ts
3
3
  interface TikTokEventsApiOptions {
4
4
  /** Pixel code from TikTok Events Manager (`event_source_id`). */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fire-spark/analytics-server",
3
- "version": "0.29.0",
3
+ "version": "0.30.0",
4
4
  "description": "Fire Spark server-side tracking relay (Meta Conversions API, TikTok Events API, Google Data Manager API)",
5
5
  "type": "module",
6
6
  "sideEffects": false,
@@ -35,14 +35,14 @@
35
35
  "big.js": "6.2.1"
36
36
  },
37
37
  "peerDependencies": {
38
- "@fire-spark/analytics": "^0.29.0"
38
+ "@fire-spark/analytics": "^0.30.0"
39
39
  },
40
40
  "devDependencies": {
41
41
  "@types/big.js": "6.2.2",
42
42
  "tsdown": "^0.22.14",
43
43
  "typescript": "^7.0.2",
44
44
  "vitest": "^2.1.8",
45
- "@fire-spark/analytics": "0.29.0"
45
+ "@fire-spark/analytics": "0.30.0"
46
46
  },
47
47
  "publishConfig": {
48
48
  "access": "public"