mytart 0.6.5 → 0.6.7

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 CHANGED
@@ -271,7 +271,7 @@ When enabled:
271
271
 
272
272
  - The official `https://www.clarity.ms/tag/{projectId}` script is loaded once on the first `track()`, `identify()`, or `page()` call
273
273
  - `track()` fires `clarity('event', eventName)` and sets each property as a custom tag via `clarity('set', key, value)`
274
- - `identify()` calls `clarity('identify', userId)` with an optional friendly name from `traits.name`, and sets remaining traits as custom tags
274
+ - `identify()` calls `clarity('identify', userId, sessionId, undefined, friendlyName)` with session ID as the native `custom-session-id` parameter, and sets remaining traits as custom tags
275
275
  - `page()` fires a `PageView` event and sets `pageUrl`, `pageName`, and `referrer` as custom tags
276
276
  - SSR-safe: silently succeeds when `window` is undefined
277
277
 
@@ -419,13 +419,15 @@ await analytics.identify({ userId: 'user-789', traits: { email: 'alice@example.c
419
419
 
420
420
  All providers receive the current state in their `track()`, `identify()`, and `page()` calls. Each provider maps state to its API correctly:
421
421
 
422
- - **GA4** — `user_id` and `session_id` in Measurement Protocol
422
+ - **GA4** — `user_id` and `session_id` in Measurement Protocol and gtag.js event params
423
423
  - **Segment** — `userId`, `anonymousId`, `sessionId` in track/identify/page
424
424
  - **Amplitude** — `user_id`, `device_id`, `session_id` in events
425
425
  - **PostHog** — `distinct_id` and `$session_id` in properties
426
- - **Mixpanel** — `distinct_id` in events
426
+ - **Mixpanel** — `distinct_id` and `$session_id` in events
427
427
  - **Meta Pixel (server)** — `external_id` in user_data, `xpl_anonymous_id` and `xpl_session_id` in custom_data
428
- - **Clarity** — `userId` passed to `clarity('identify')`
428
+ - **Meta Pixel (browser)** — `xpl_session_id` in event properties
429
+ - **Clarity** — `userId` passed to `clarity('identify', userId, sessionId, undefined, friendlyName)`, `sessionId` also set as custom tag
430
+ - **Plausible** — does not support user identification (privacy-first, by design)
429
431
 
430
432
  ## API Reference
431
433
 
@@ -549,12 +551,14 @@ All types are exported:
549
551
 
550
552
  ```typescript
551
553
  import type {
552
- MytartConfig, BaseProviderConfig, ProviderConfig, TrackOptions, IdentifyOptions, PageOptions,
554
+ MytartConfig, MytartState, BaseProviderConfig, ProviderConfig, TrackOptions, IdentifyOptions, PageOptions,
553
555
  TrackResult, MytartError, EventContext, ProviderName, GoogleAnalyticsAppType,
554
556
  GoogleAnalyticsConfig, ConsentSettings, ConsentState, MixpanelConfig, SegmentConfig,
555
557
  AmplitudeConfig, PlausibleConfig, PostHogConfig, MetaPixelConfig, MetaPixelAppType,
556
558
  MetaPixelAdvancedMatching, ClarityConfig,
557
559
  } from 'mytart';
560
+
561
+ import type { MytartLike } from 'mytart'; // interface for custom providers
558
562
  ```
559
563
 
560
564
  ## Custom Providers
package/dist/index.d.mts CHANGED
@@ -791,9 +791,9 @@ declare class MixpanelProvider extends BaseProvider {
791
791
  private readonly http;
792
792
  constructor(config: MixpanelConfig, mytart: MytartLike);
793
793
  private encodeData;
794
- track({ event, properties, userId, anonymousId, timestamp }: TrackOptions): Promise<TrackResult>;
795
- identify({ userId, traits }: IdentifyOptions): Promise<TrackResult>;
796
- page({ name, url, userId, anonymousId, properties }: PageOptions): Promise<TrackResult>;
794
+ track({ event, properties, userId, anonymousId, sessionId, timestamp }: TrackOptions): Promise<TrackResult>;
795
+ identify({ userId, traits, sessionId }: IdentifyOptions): Promise<TrackResult>;
796
+ page({ name, url, userId, anonymousId, sessionId, properties }: PageOptions): Promise<TrackResult>;
797
797
  }
798
798
 
799
799
  declare class SegmentProvider extends BaseProvider {
@@ -828,7 +828,7 @@ declare class PlausibleProvider extends BaseProvider {
828
828
  private buildHeaders;
829
829
  track({ event, properties, context }: TrackOptions): Promise<TrackResult>;
830
830
  identify(_options: IdentifyOptions): Promise<TrackResult>;
831
- page({ name, url, properties }: PageOptions): Promise<TrackResult>;
831
+ page({ name, url, referrer, properties }: PageOptions): Promise<TrackResult>;
832
832
  }
833
833
 
834
834
  declare class PostHogProvider extends BaseProvider {
@@ -945,4 +945,4 @@ declare class ClarityProvider extends BaseProvider {
945
945
  page(options: PageOptions): Promise<TrackResult>;
946
946
  }
947
947
 
948
- export { type AmplitudeConfig, AmplitudeProvider, BaseProvider, type BaseProviderConfig, type CapturedIds, type ClarityConfig, ClarityProvider, type ConsentSettings, type ConsentState, type EventContext, type GoogleAnalyticsAppType, type GoogleAnalyticsConfig, GoogleAnalyticsProvider, type IdentifyOptions, type MetaPixelAdvancedMatching, type MetaPixelAppType, type MetaPixelConfig, MetaPixelProvider, type MixpanelConfig, MixpanelProvider, Mytart, type MytartConfig, type MytartError, type PageOptions, type PlausibleConfig, PlausibleProvider, type PostHogConfig, PostHogProvider, type ProviderConfig, type ProviderName, type SegmentConfig, SegmentProvider, type TrackOptions, type TrackResult };
948
+ export { type AmplitudeConfig, AmplitudeProvider, BaseProvider, type BaseProviderConfig, type CapturedIds, type ClarityConfig, ClarityProvider, type ConsentSettings, type ConsentState, type EventContext, type GoogleAnalyticsAppType, type GoogleAnalyticsConfig, GoogleAnalyticsProvider, type IdentifyOptions, type MetaPixelAdvancedMatching, type MetaPixelAppType, type MetaPixelConfig, MetaPixelProvider, type MixpanelConfig, MixpanelProvider, Mytart, type MytartConfig, type MytartError, type MytartLike, type MytartState, type PageOptions, type PlausibleConfig, PlausibleProvider, type PostHogConfig, PostHogProvider, type ProviderConfig, type ProviderName, type SegmentConfig, SegmentProvider, type TrackOptions, type TrackResult };
package/dist/index.d.ts CHANGED
@@ -791,9 +791,9 @@ declare class MixpanelProvider extends BaseProvider {
791
791
  private readonly http;
792
792
  constructor(config: MixpanelConfig, mytart: MytartLike);
793
793
  private encodeData;
794
- track({ event, properties, userId, anonymousId, timestamp }: TrackOptions): Promise<TrackResult>;
795
- identify({ userId, traits }: IdentifyOptions): Promise<TrackResult>;
796
- page({ name, url, userId, anonymousId, properties }: PageOptions): Promise<TrackResult>;
794
+ track({ event, properties, userId, anonymousId, sessionId, timestamp }: TrackOptions): Promise<TrackResult>;
795
+ identify({ userId, traits, sessionId }: IdentifyOptions): Promise<TrackResult>;
796
+ page({ name, url, userId, anonymousId, sessionId, properties }: PageOptions): Promise<TrackResult>;
797
797
  }
798
798
 
799
799
  declare class SegmentProvider extends BaseProvider {
@@ -828,7 +828,7 @@ declare class PlausibleProvider extends BaseProvider {
828
828
  private buildHeaders;
829
829
  track({ event, properties, context }: TrackOptions): Promise<TrackResult>;
830
830
  identify(_options: IdentifyOptions): Promise<TrackResult>;
831
- page({ name, url, properties }: PageOptions): Promise<TrackResult>;
831
+ page({ name, url, referrer, properties }: PageOptions): Promise<TrackResult>;
832
832
  }
833
833
 
834
834
  declare class PostHogProvider extends BaseProvider {
@@ -945,4 +945,4 @@ declare class ClarityProvider extends BaseProvider {
945
945
  page(options: PageOptions): Promise<TrackResult>;
946
946
  }
947
947
 
948
- export { type AmplitudeConfig, AmplitudeProvider, BaseProvider, type BaseProviderConfig, type CapturedIds, type ClarityConfig, ClarityProvider, type ConsentSettings, type ConsentState, type EventContext, type GoogleAnalyticsAppType, type GoogleAnalyticsConfig, GoogleAnalyticsProvider, type IdentifyOptions, type MetaPixelAdvancedMatching, type MetaPixelAppType, type MetaPixelConfig, MetaPixelProvider, type MixpanelConfig, MixpanelProvider, Mytart, type MytartConfig, type MytartError, type PageOptions, type PlausibleConfig, PlausibleProvider, type PostHogConfig, PostHogProvider, type ProviderConfig, type ProviderName, type SegmentConfig, SegmentProvider, type TrackOptions, type TrackResult };
948
+ export { type AmplitudeConfig, AmplitudeProvider, BaseProvider, type BaseProviderConfig, type CapturedIds, type ClarityConfig, ClarityProvider, type ConsentSettings, type ConsentState, type EventContext, type GoogleAnalyticsAppType, type GoogleAnalyticsConfig, GoogleAnalyticsProvider, type IdentifyOptions, type MetaPixelAdvancedMatching, type MetaPixelAppType, type MetaPixelConfig, MetaPixelProvider, type MixpanelConfig, MixpanelProvider, Mytart, type MytartConfig, type MytartError, type MytartLike, type MytartState, type PageOptions, type PlausibleConfig, PlausibleProvider, type PostHogConfig, PostHogProvider, type ProviderConfig, type ProviderName, type SegmentConfig, SegmentProvider, type TrackOptions, type TrackResult };
package/dist/index.js CHANGED
@@ -166,6 +166,9 @@ var GoogleAnalyticsProvider = class extends BaseProvider {
166
166
  this.http = createHttpClient();
167
167
  this.endpoint = config.debug ? GA4_DEBUG_ENDPOINT : GA4_ENDPOINT;
168
168
  this.isBrowser = config.appType === "browser";
169
+ if (this.isBrowser) {
170
+ this.gtagReady = this.initGtag();
171
+ }
169
172
  }
170
173
  /**
171
174
  * Initializes the gtag.js snippet exactly as Google's official documentation
@@ -245,7 +248,7 @@ var GoogleAnalyticsProvider = class extends BaseProvider {
245
248
  // ---------------------------------------------------------------------------
246
249
  // Browser mode methods — delegate to gtag()
247
250
  // ---------------------------------------------------------------------------
248
- async trackBrowser({ event, properties, userId }) {
251
+ async trackBrowser({ event, properties, userId, sessionId }) {
249
252
  if (typeof window === "undefined") {
250
253
  return this.buildGtagResult();
251
254
  }
@@ -253,21 +256,28 @@ var GoogleAnalyticsProvider = class extends BaseProvider {
253
256
  if (userId) {
254
257
  window.gtag("set", { user_id: userId });
255
258
  }
256
- window.gtag("event", event, properties ?? {});
259
+ const params = { ...properties };
260
+ if (sessionId) {
261
+ params["session_id"] = sessionId;
262
+ }
263
+ window.gtag("event", event, params);
257
264
  return this.buildGtagResult();
258
265
  }
259
- async identifyBrowser({ userId, traits }) {
266
+ async identifyBrowser({ userId, traits, sessionId }) {
260
267
  if (typeof window === "undefined") {
261
268
  return this.buildGtagResult();
262
269
  }
263
270
  await this.ensureGtag();
264
271
  window.gtag("set", { user_id: userId });
272
+ if (sessionId) {
273
+ window.gtag("set", { session_id: sessionId });
274
+ }
265
275
  if (traits) {
266
276
  window.gtag("set", "user_properties", traits);
267
277
  }
268
278
  return this.buildGtagResult();
269
279
  }
270
- async pageBrowser({ name, url, referrer, userId }) {
280
+ async pageBrowser({ name, url, referrer, userId, sessionId }) {
271
281
  if (typeof window === "undefined") {
272
282
  return this.buildGtagResult();
273
283
  }
@@ -275,11 +285,15 @@ var GoogleAnalyticsProvider = class extends BaseProvider {
275
285
  if (userId) {
276
286
  window.gtag("set", { user_id: userId });
277
287
  }
278
- window.gtag("event", "page_view", {
288
+ const params = {
279
289
  page_title: name,
280
290
  page_location: url,
281
291
  page_referrer: referrer
282
- });
292
+ };
293
+ if (sessionId) {
294
+ params["session_id"] = sessionId;
295
+ }
296
+ window.gtag("event", "page_view", params);
283
297
  return this.buildGtagResult();
284
298
  }
285
299
  buildGtagResult() {
@@ -356,14 +370,17 @@ var GoogleAnalyticsProvider = class extends BaseProvider {
356
370
  return this.identifyBrowser({ userId, traits, sessionId });
357
371
  }
358
372
  try {
373
+ const params = {};
374
+ if (sessionId) {
375
+ params["session_id"] = sessionId;
376
+ }
359
377
  const body = {
360
378
  client_id: userId,
361
379
  user_id: userId,
362
- session_id: sessionId,
363
380
  user_properties: traits ? Object.fromEntries(
364
381
  Object.entries(traits).map(([k, v]) => [k, { value: v }])
365
382
  ) : void 0,
366
- events: [{ name: "identify", params: {} }]
383
+ events: [{ name: "identify", params }]
367
384
  };
368
385
  const response = await this.http.post(this.endpoint, body, {
369
386
  params: {
@@ -416,20 +433,19 @@ var MixpanelProvider = class extends BaseProvider {
416
433
  const base64 = Buffer.from(json).toString("base64");
417
434
  return base64;
418
435
  }
419
- async track({ event, properties, userId, anonymousId, timestamp }) {
436
+ async track({ event, properties, userId, anonymousId, sessionId, timestamp }) {
420
437
  try {
421
438
  const distinctId = userId ?? anonymousId ?? "anonymous";
422
- const eventPayload = [
423
- {
424
- event,
425
- properties: {
426
- token: this.config.token,
427
- distinct_id: distinctId,
428
- time: timestamp ? Math.floor(timestamp.getTime() / 1e3) : Math.floor(Date.now() / 1e3),
429
- ...properties
430
- }
431
- }
432
- ];
439
+ const props = {
440
+ token: this.config.token,
441
+ distinct_id: distinctId,
442
+ time: timestamp ? Math.floor(timestamp.getTime() / 1e3) : Math.floor(Date.now() / 1e3),
443
+ ...properties
444
+ };
445
+ if (sessionId) {
446
+ props["$session_id"] = sessionId;
447
+ }
448
+ const eventPayload = [{ event, properties: props }];
433
449
  const endpoint = this.config.apiUrl ?? MIXPANEL_TRACK_ENDPOINT;
434
450
  const response = await this.http.post(
435
451
  endpoint,
@@ -450,13 +466,17 @@ var MixpanelProvider = class extends BaseProvider {
450
466
  return this.buildError(String(error), "MIXPANEL_UNKNOWN_ERROR", error);
451
467
  }
452
468
  }
453
- async identify({ userId, traits }) {
469
+ async identify({ userId, traits, sessionId }) {
454
470
  try {
471
+ const setData = { ...traits };
472
+ if (sessionId) {
473
+ setData["$session_id"] = sessionId;
474
+ }
455
475
  const engagePayload = [
456
476
  {
457
477
  $token: this.config.token,
458
478
  $distinct_id: userId,
459
- $set: traits ?? {}
479
+ $set: setData
460
480
  }
461
481
  ];
462
482
  const endpoint = this.config.apiUrl ? this.config.apiUrl.replace("/track", "/engage") : MIXPANEL_ENGAGE_ENDPOINT;
@@ -479,7 +499,7 @@ var MixpanelProvider = class extends BaseProvider {
479
499
  return this.buildError(String(error), "MIXPANEL_UNKNOWN_ERROR", error);
480
500
  }
481
501
  }
482
- async page({ name, url, userId, anonymousId, properties }) {
502
+ async page({ name, url, userId, anonymousId, sessionId, properties }) {
483
503
  return this.track({
484
504
  event: "Page View",
485
505
  properties: {
@@ -488,7 +508,8 @@ var MixpanelProvider = class extends BaseProvider {
488
508
  ...properties
489
509
  },
490
510
  userId,
491
- anonymousId
511
+ anonymousId,
512
+ sessionId
492
513
  });
493
514
  }
494
515
  };
@@ -741,6 +762,7 @@ var PlausibleProvider = class extends BaseProvider {
741
762
  return this.buildError(String(error), "PLAUSIBLE_UNKNOWN_ERROR", error);
742
763
  }
743
764
  }
765
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
744
766
  async identify(_options) {
745
767
  return {
746
768
  provider: this.name,
@@ -752,12 +774,13 @@ var PlausibleProvider = class extends BaseProvider {
752
774
  }
753
775
  };
754
776
  }
755
- async page({ name, url, properties }) {
777
+ async page({ name, url, referrer, properties }) {
756
778
  try {
757
779
  const body = {
758
780
  name: "pageview",
759
781
  url,
760
782
  domain: this.config.domain,
783
+ referrer,
761
784
  props: {
762
785
  page_name: name,
763
786
  ...properties
@@ -781,7 +804,7 @@ var PlausibleProvider = class extends BaseProvider {
781
804
  };
782
805
 
783
806
  // src/providers/posthog.ts
784
- var POSTHOG_ENDPOINT = "https://app.posthog.com/capture/";
807
+ var POSTHOG_ENDPOINT = "https://us.i.posthog.com/capture/";
785
808
  var PostHogProvider = class extends BaseProvider {
786
809
  constructor(config, mytart) {
787
810
  super(mytart);
@@ -1023,14 +1046,18 @@ var MetaPixelProvider = class extends BaseProvider {
1023
1046
  return this.buildFbqResult();
1024
1047
  }
1025
1048
  await this.ensureFbq();
1026
- const { event, properties, context } = options;
1049
+ const { event, properties, context, sessionId } = options;
1027
1050
  const eventId = context?.eventId;
1028
1051
  const isStandard = STANDARD_EVENTS.has(event);
1029
1052
  const command = isStandard ? "track" : "trackCustom";
1053
+ const params = { ...properties };
1054
+ if (sessionId) {
1055
+ params["xpl_session_id"] = sessionId;
1056
+ }
1030
1057
  if (eventId) {
1031
- window.fbq(command, event, properties ?? {}, { eventID: eventId });
1058
+ window.fbq(command, event, params, { eventID: eventId });
1032
1059
  } else {
1033
- window.fbq(command, event, properties ?? {});
1060
+ window.fbq(command, event, params);
1034
1061
  }
1035
1062
  return this.buildFbqResult();
1036
1063
  }
@@ -1049,7 +1076,8 @@ var MetaPixelProvider = class extends BaseProvider {
1049
1076
  return this.buildFbqResult();
1050
1077
  }
1051
1078
  // -- browser page --
1052
- async pageBrowser(options) {
1079
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
1080
+ async pageBrowser(_options) {
1053
1081
  if (typeof window === "undefined") {
1054
1082
  return this.buildFbqResult();
1055
1083
  }
@@ -1138,6 +1166,9 @@ var MetaPixelProvider = class extends BaseProvider {
1138
1166
  Object.assign(this.cachedUserData, options.traits);
1139
1167
  }
1140
1168
  this.cachedUserData["external_id"] = options.userId;
1169
+ if (options.sessionId) {
1170
+ this.cachedUserData["xpl_session_id"] = options.sessionId;
1171
+ }
1141
1172
  return this.buildSuccess(200);
1142
1173
  }
1143
1174
  // -- server page --
@@ -1259,6 +1290,9 @@ var ClarityProvider = class extends BaseProvider {
1259
1290
  if (options.userId) {
1260
1291
  window.clarity("identify", options.userId);
1261
1292
  }
1293
+ if (options.sessionId) {
1294
+ window.clarity("set", "sessionId", options.sessionId);
1295
+ }
1262
1296
  window.clarity("event", options.event);
1263
1297
  if (options.properties) {
1264
1298
  for (const [key, value] of Object.entries(options.properties)) {
@@ -1273,7 +1307,10 @@ var ClarityProvider = class extends BaseProvider {
1273
1307
  }
1274
1308
  await this.ensureClarity();
1275
1309
  const friendlyName = options.traits?.name ?? void 0;
1276
- window.clarity("identify", options.userId, void 0, void 0, friendlyName);
1310
+ window.clarity("identify", options.userId, options.sessionId, void 0, friendlyName);
1311
+ if (options.sessionId) {
1312
+ window.clarity("set", "sessionId", options.sessionId);
1313
+ }
1277
1314
  if (options.traits) {
1278
1315
  for (const [key, value] of Object.entries(options.traits)) {
1279
1316
  if (key !== "name") {
@@ -1289,6 +1326,9 @@ var ClarityProvider = class extends BaseProvider {
1289
1326
  }
1290
1327
  await this.ensureClarity();
1291
1328
  window.clarity("event", "PageView");
1329
+ if (options.sessionId) {
1330
+ window.clarity("set", "sessionId", options.sessionId);
1331
+ }
1292
1332
  if (options.url) {
1293
1333
  window.clarity("set", "pageUrl", options.url);
1294
1334
  }
package/dist/index.mjs CHANGED
@@ -121,6 +121,9 @@ var GoogleAnalyticsProvider = class extends BaseProvider {
121
121
  this.http = createHttpClient();
122
122
  this.endpoint = config.debug ? GA4_DEBUG_ENDPOINT : GA4_ENDPOINT;
123
123
  this.isBrowser = config.appType === "browser";
124
+ if (this.isBrowser) {
125
+ this.gtagReady = this.initGtag();
126
+ }
124
127
  }
125
128
  /**
126
129
  * Initializes the gtag.js snippet exactly as Google's official documentation
@@ -200,7 +203,7 @@ var GoogleAnalyticsProvider = class extends BaseProvider {
200
203
  // ---------------------------------------------------------------------------
201
204
  // Browser mode methods — delegate to gtag()
202
205
  // ---------------------------------------------------------------------------
203
- async trackBrowser({ event, properties, userId }) {
206
+ async trackBrowser({ event, properties, userId, sessionId }) {
204
207
  if (typeof window === "undefined") {
205
208
  return this.buildGtagResult();
206
209
  }
@@ -208,21 +211,28 @@ var GoogleAnalyticsProvider = class extends BaseProvider {
208
211
  if (userId) {
209
212
  window.gtag("set", { user_id: userId });
210
213
  }
211
- window.gtag("event", event, properties ?? {});
214
+ const params = { ...properties };
215
+ if (sessionId) {
216
+ params["session_id"] = sessionId;
217
+ }
218
+ window.gtag("event", event, params);
212
219
  return this.buildGtagResult();
213
220
  }
214
- async identifyBrowser({ userId, traits }) {
221
+ async identifyBrowser({ userId, traits, sessionId }) {
215
222
  if (typeof window === "undefined") {
216
223
  return this.buildGtagResult();
217
224
  }
218
225
  await this.ensureGtag();
219
226
  window.gtag("set", { user_id: userId });
227
+ if (sessionId) {
228
+ window.gtag("set", { session_id: sessionId });
229
+ }
220
230
  if (traits) {
221
231
  window.gtag("set", "user_properties", traits);
222
232
  }
223
233
  return this.buildGtagResult();
224
234
  }
225
- async pageBrowser({ name, url, referrer, userId }) {
235
+ async pageBrowser({ name, url, referrer, userId, sessionId }) {
226
236
  if (typeof window === "undefined") {
227
237
  return this.buildGtagResult();
228
238
  }
@@ -230,11 +240,15 @@ var GoogleAnalyticsProvider = class extends BaseProvider {
230
240
  if (userId) {
231
241
  window.gtag("set", { user_id: userId });
232
242
  }
233
- window.gtag("event", "page_view", {
243
+ const params = {
234
244
  page_title: name,
235
245
  page_location: url,
236
246
  page_referrer: referrer
237
- });
247
+ };
248
+ if (sessionId) {
249
+ params["session_id"] = sessionId;
250
+ }
251
+ window.gtag("event", "page_view", params);
238
252
  return this.buildGtagResult();
239
253
  }
240
254
  buildGtagResult() {
@@ -311,14 +325,17 @@ var GoogleAnalyticsProvider = class extends BaseProvider {
311
325
  return this.identifyBrowser({ userId, traits, sessionId });
312
326
  }
313
327
  try {
328
+ const params = {};
329
+ if (sessionId) {
330
+ params["session_id"] = sessionId;
331
+ }
314
332
  const body = {
315
333
  client_id: userId,
316
334
  user_id: userId,
317
- session_id: sessionId,
318
335
  user_properties: traits ? Object.fromEntries(
319
336
  Object.entries(traits).map(([k, v]) => [k, { value: v }])
320
337
  ) : void 0,
321
- events: [{ name: "identify", params: {} }]
338
+ events: [{ name: "identify", params }]
322
339
  };
323
340
  const response = await this.http.post(this.endpoint, body, {
324
341
  params: {
@@ -371,20 +388,19 @@ var MixpanelProvider = class extends BaseProvider {
371
388
  const base64 = Buffer.from(json).toString("base64");
372
389
  return base64;
373
390
  }
374
- async track({ event, properties, userId, anonymousId, timestamp }) {
391
+ async track({ event, properties, userId, anonymousId, sessionId, timestamp }) {
375
392
  try {
376
393
  const distinctId = userId ?? anonymousId ?? "anonymous";
377
- const eventPayload = [
378
- {
379
- event,
380
- properties: {
381
- token: this.config.token,
382
- distinct_id: distinctId,
383
- time: timestamp ? Math.floor(timestamp.getTime() / 1e3) : Math.floor(Date.now() / 1e3),
384
- ...properties
385
- }
386
- }
387
- ];
394
+ const props = {
395
+ token: this.config.token,
396
+ distinct_id: distinctId,
397
+ time: timestamp ? Math.floor(timestamp.getTime() / 1e3) : Math.floor(Date.now() / 1e3),
398
+ ...properties
399
+ };
400
+ if (sessionId) {
401
+ props["$session_id"] = sessionId;
402
+ }
403
+ const eventPayload = [{ event, properties: props }];
388
404
  const endpoint = this.config.apiUrl ?? MIXPANEL_TRACK_ENDPOINT;
389
405
  const response = await this.http.post(
390
406
  endpoint,
@@ -405,13 +421,17 @@ var MixpanelProvider = class extends BaseProvider {
405
421
  return this.buildError(String(error), "MIXPANEL_UNKNOWN_ERROR", error);
406
422
  }
407
423
  }
408
- async identify({ userId, traits }) {
424
+ async identify({ userId, traits, sessionId }) {
409
425
  try {
426
+ const setData = { ...traits };
427
+ if (sessionId) {
428
+ setData["$session_id"] = sessionId;
429
+ }
410
430
  const engagePayload = [
411
431
  {
412
432
  $token: this.config.token,
413
433
  $distinct_id: userId,
414
- $set: traits ?? {}
434
+ $set: setData
415
435
  }
416
436
  ];
417
437
  const endpoint = this.config.apiUrl ? this.config.apiUrl.replace("/track", "/engage") : MIXPANEL_ENGAGE_ENDPOINT;
@@ -434,7 +454,7 @@ var MixpanelProvider = class extends BaseProvider {
434
454
  return this.buildError(String(error), "MIXPANEL_UNKNOWN_ERROR", error);
435
455
  }
436
456
  }
437
- async page({ name, url, userId, anonymousId, properties }) {
457
+ async page({ name, url, userId, anonymousId, sessionId, properties }) {
438
458
  return this.track({
439
459
  event: "Page View",
440
460
  properties: {
@@ -443,7 +463,8 @@ var MixpanelProvider = class extends BaseProvider {
443
463
  ...properties
444
464
  },
445
465
  userId,
446
- anonymousId
466
+ anonymousId,
467
+ sessionId
447
468
  });
448
469
  }
449
470
  };
@@ -696,6 +717,7 @@ var PlausibleProvider = class extends BaseProvider {
696
717
  return this.buildError(String(error), "PLAUSIBLE_UNKNOWN_ERROR", error);
697
718
  }
698
719
  }
720
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
699
721
  async identify(_options) {
700
722
  return {
701
723
  provider: this.name,
@@ -707,12 +729,13 @@ var PlausibleProvider = class extends BaseProvider {
707
729
  }
708
730
  };
709
731
  }
710
- async page({ name, url, properties }) {
732
+ async page({ name, url, referrer, properties }) {
711
733
  try {
712
734
  const body = {
713
735
  name: "pageview",
714
736
  url,
715
737
  domain: this.config.domain,
738
+ referrer,
716
739
  props: {
717
740
  page_name: name,
718
741
  ...properties
@@ -736,7 +759,7 @@ var PlausibleProvider = class extends BaseProvider {
736
759
  };
737
760
 
738
761
  // src/providers/posthog.ts
739
- var POSTHOG_ENDPOINT = "https://app.posthog.com/capture/";
762
+ var POSTHOG_ENDPOINT = "https://us.i.posthog.com/capture/";
740
763
  var PostHogProvider = class extends BaseProvider {
741
764
  constructor(config, mytart) {
742
765
  super(mytart);
@@ -978,14 +1001,18 @@ var MetaPixelProvider = class extends BaseProvider {
978
1001
  return this.buildFbqResult();
979
1002
  }
980
1003
  await this.ensureFbq();
981
- const { event, properties, context } = options;
1004
+ const { event, properties, context, sessionId } = options;
982
1005
  const eventId = context?.eventId;
983
1006
  const isStandard = STANDARD_EVENTS.has(event);
984
1007
  const command = isStandard ? "track" : "trackCustom";
1008
+ const params = { ...properties };
1009
+ if (sessionId) {
1010
+ params["xpl_session_id"] = sessionId;
1011
+ }
985
1012
  if (eventId) {
986
- window.fbq(command, event, properties ?? {}, { eventID: eventId });
1013
+ window.fbq(command, event, params, { eventID: eventId });
987
1014
  } else {
988
- window.fbq(command, event, properties ?? {});
1015
+ window.fbq(command, event, params);
989
1016
  }
990
1017
  return this.buildFbqResult();
991
1018
  }
@@ -1004,7 +1031,8 @@ var MetaPixelProvider = class extends BaseProvider {
1004
1031
  return this.buildFbqResult();
1005
1032
  }
1006
1033
  // -- browser page --
1007
- async pageBrowser(options) {
1034
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
1035
+ async pageBrowser(_options) {
1008
1036
  if (typeof window === "undefined") {
1009
1037
  return this.buildFbqResult();
1010
1038
  }
@@ -1093,6 +1121,9 @@ var MetaPixelProvider = class extends BaseProvider {
1093
1121
  Object.assign(this.cachedUserData, options.traits);
1094
1122
  }
1095
1123
  this.cachedUserData["external_id"] = options.userId;
1124
+ if (options.sessionId) {
1125
+ this.cachedUserData["xpl_session_id"] = options.sessionId;
1126
+ }
1096
1127
  return this.buildSuccess(200);
1097
1128
  }
1098
1129
  // -- server page --
@@ -1214,6 +1245,9 @@ var ClarityProvider = class extends BaseProvider {
1214
1245
  if (options.userId) {
1215
1246
  window.clarity("identify", options.userId);
1216
1247
  }
1248
+ if (options.sessionId) {
1249
+ window.clarity("set", "sessionId", options.sessionId);
1250
+ }
1217
1251
  window.clarity("event", options.event);
1218
1252
  if (options.properties) {
1219
1253
  for (const [key, value] of Object.entries(options.properties)) {
@@ -1228,7 +1262,10 @@ var ClarityProvider = class extends BaseProvider {
1228
1262
  }
1229
1263
  await this.ensureClarity();
1230
1264
  const friendlyName = options.traits?.name ?? void 0;
1231
- window.clarity("identify", options.userId, void 0, void 0, friendlyName);
1265
+ window.clarity("identify", options.userId, options.sessionId, void 0, friendlyName);
1266
+ if (options.sessionId) {
1267
+ window.clarity("set", "sessionId", options.sessionId);
1268
+ }
1232
1269
  if (options.traits) {
1233
1270
  for (const [key, value] of Object.entries(options.traits)) {
1234
1271
  if (key !== "name") {
@@ -1244,6 +1281,9 @@ var ClarityProvider = class extends BaseProvider {
1244
1281
  }
1245
1282
  await this.ensureClarity();
1246
1283
  window.clarity("event", "PageView");
1284
+ if (options.sessionId) {
1285
+ window.clarity("set", "sessionId", options.sessionId);
1286
+ }
1247
1287
  if (options.url) {
1248
1288
  window.clarity("set", "pageUrl", options.url);
1249
1289
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mytart",
3
- "version": "0.6.5",
3
+ "version": "0.6.7",
4
4
  "description": "Multi-Yield Tracking & Analytics Relay Tool — framework-agnostic analytics for any project",
5
5
  "keywords": [
6
6
  "analytics",