getu-attribution-v2-sdk 0.2.1 → 0.2.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.
package/dist/index.esm.js CHANGED
@@ -16,6 +16,8 @@ var EventType;
16
16
  EventType["PRODUCT_VIEW"] = "product_view";
17
17
  EventType["ADD_TO_CART"] = "add_to_cart";
18
18
  EventType["PURCHASE"] = "purchase";
19
+ // Post-conversion / back-office conversion
20
+ EventType["AUDIT_APPROVED"] = "audit_approved";
19
21
  })(EventType || (EventType = {}));
20
22
  // Currency types
21
23
  var Currency;
@@ -30,6 +32,7 @@ const IMMEDIATE_EVENTS = [
30
32
  EventType.SIGNUP,
31
33
  EventType.FORM_SUBMIT,
32
34
  EventType.EMAIL_VERIFICATION,
35
+ EventType.AUDIT_APPROVED,
33
36
  ];
34
37
 
35
38
  // Generate unique ID
@@ -550,10 +553,131 @@ class AttributionStorageManager {
550
553
  constructor(logger) {
551
554
  this.UTM_STORAGE_KEY = "attribution_utm_data";
552
555
  this.SESSION_STORAGE_KEY = "attribution_session";
556
+ this.USER_ID_KEY = "getuai_user_id";
557
+ this.USER_ID_COOKIE_EXPIRES = 365; // days
553
558
  this.logger = logger;
554
559
  this.localStorage = new LocalStorageManager(logger);
555
560
  this.indexedDB = new IndexedDBManager(logger);
556
561
  }
562
+ // User ID management - stores in both cookie and localStorage
563
+ setUserId(userId) {
564
+ if (!userId || userId.trim() === "") {
565
+ this.logger.warn("Cannot set empty user ID");
566
+ return;
567
+ }
568
+ const trimmedUserId = userId.trim();
569
+ // Store in cookie
570
+ try {
571
+ this.setCookie(this.USER_ID_KEY, trimmedUserId, this.USER_ID_COOKIE_EXPIRES);
572
+ this.logger.debug(`👤 User ID stored in cookie: ${trimmedUserId}`);
573
+ }
574
+ catch (error) {
575
+ this.logger.error("Failed to store user ID in cookie:", error);
576
+ }
577
+ // Store in localStorage
578
+ try {
579
+ if (typeof localStorage !== "undefined") {
580
+ localStorage.setItem(this.USER_ID_KEY, trimmedUserId);
581
+ this.logger.debug(`👤 User ID stored in localStorage: ${trimmedUserId}`);
582
+ }
583
+ }
584
+ catch (error) {
585
+ this.logger.error("Failed to store user ID in localStorage:", error);
586
+ }
587
+ }
588
+ getUserId() {
589
+ // Try localStorage first
590
+ try {
591
+ if (typeof localStorage !== "undefined") {
592
+ const userId = localStorage.getItem(this.USER_ID_KEY);
593
+ if (userId) {
594
+ return userId;
595
+ }
596
+ }
597
+ }
598
+ catch (error) {
599
+ this.logger.debug("Failed to get user ID from localStorage:", error);
600
+ }
601
+ // Fallback to cookie
602
+ try {
603
+ const userId = this.getCookie(this.USER_ID_KEY);
604
+ if (userId) {
605
+ // Sync back to localStorage if cookie exists but localStorage doesn't
606
+ try {
607
+ if (typeof localStorage !== "undefined") {
608
+ localStorage.setItem(this.USER_ID_KEY, userId);
609
+ }
610
+ }
611
+ catch (error) {
612
+ // Ignore sync errors
613
+ }
614
+ return userId;
615
+ }
616
+ }
617
+ catch (error) {
618
+ this.logger.debug("Failed to get user ID from cookie:", error);
619
+ }
620
+ return null;
621
+ }
622
+ removeUserId() {
623
+ // Remove from cookie
624
+ try {
625
+ this.deleteCookie(this.USER_ID_KEY);
626
+ this.logger.debug("👤 User ID removed from cookie");
627
+ }
628
+ catch (error) {
629
+ this.logger.error("Failed to remove user ID from cookie:", error);
630
+ }
631
+ // Remove from localStorage
632
+ try {
633
+ if (typeof localStorage !== "undefined") {
634
+ localStorage.removeItem(this.USER_ID_KEY);
635
+ this.logger.debug("👤 User ID removed from localStorage");
636
+ }
637
+ }
638
+ catch (error) {
639
+ this.logger.error("Failed to remove user ID from localStorage:", error);
640
+ }
641
+ }
642
+ // Cookie helper methods
643
+ setCookie(name, value, days) {
644
+ try {
645
+ const expires = new Date();
646
+ expires.setTime(expires.getTime() + days * 24 * 60 * 60 * 1000);
647
+ const cookieValue = `${name}=${encodeURIComponent(value)};expires=${expires.toUTCString()};path=/;SameSite=Lax`;
648
+ document.cookie = cookieValue;
649
+ }
650
+ catch (error) {
651
+ this.logger.error("Failed to set cookie:", error);
652
+ }
653
+ }
654
+ getCookie(name) {
655
+ try {
656
+ const nameEQ = name + "=";
657
+ const ca = document.cookie.split(";");
658
+ for (let i = 0; i < ca.length; i++) {
659
+ let c = ca[i];
660
+ while (c.charAt(0) === " ")
661
+ c = c.substring(1, c.length);
662
+ if (c.indexOf(nameEQ) === 0) {
663
+ return decodeURIComponent(c.substring(nameEQ.length, c.length));
664
+ }
665
+ }
666
+ return null;
667
+ }
668
+ catch (error) {
669
+ this.logger.error("Failed to get cookie:", error);
670
+ return null;
671
+ }
672
+ }
673
+ deleteCookie(name) {
674
+ try {
675
+ document.cookie = `${name}=;expires=Thu, 01 Jan 1970 00:00:00 UTC;path=/;`;
676
+ }
677
+ catch (error) {
678
+ this.logger.error("Failed to delete cookie:", error);
679
+ }
680
+ }
557
681
  async init() {
558
682
  await this.indexedDB.init();
559
683
  }
@@ -872,8 +996,17 @@ class AttributionSDK {
872
996
  }
873
997
  try {
874
998
  this.logger.info("Initializing GetuAI Attribution SDK");
875
- // Initialize storage
876
- await this.storage.init();
999
+ // Initialize storage (IndexedDB failure should not block SDK initialization)
1000
+ try {
1001
+ await this.storage.init();
1002
+ }
1003
+ catch (storageError) {
1004
+ this.logger.warn("Storage initialization failed (IndexedDB may be unavailable), continuing with basic features:", storageError);
1005
+ // Continue initialization even if IndexedDB fails
1006
+ // User ID and other features using cookie/localStorage will still work
1007
+ }
1008
+ // Initialize user ID (from config or existing storage)
1009
+ this.initializeUserId();
877
1010
  // Initialize session
878
1011
  this.initializeSession();
879
1012
  // Extract and store UTM data from current URL
@@ -946,10 +1079,12 @@ class AttributionSDK {
946
1079
  last_activity: this.session?.lastActivity,
947
1080
  page_views: this.session?.pageViews,
948
1081
  };
1082
+ // Use provided tracking_user_id or fallback to stored user ID
1083
+ const finalUserId = tracking_user_id || this.getUserId();
949
1084
  const event = {
950
1085
  event_id: generateId(),
951
1086
  event_type: eventType,
952
- tracking_user_id: tracking_user_id,
1087
+ tracking_user_id: finalUserId || undefined,
953
1088
  timestamp: getTimestamp(),
954
1089
  event_data: eventData,
955
1090
  context: { page: pageContext, session: sessionContext },
@@ -1046,6 +1181,46 @@ class AttributionSDK {
1046
1181
  async trackEmailVerification(tracking_user_id, verificationData) {
1047
1182
  await this.trackEvent(EventType.EMAIL_VERIFICATION, verificationData, tracking_user_id);
1048
1183
  }
1184
+ // Track audit approved (conversion)
1185
+ async trackAuditApproved(tracking_user_id, auditData) {
1186
+ await this.trackEvent(EventType.AUDIT_APPROVED, auditData, tracking_user_id);
1187
+ }
1188
+ // Track purchase with auto user ID (object parameter format)
1189
+ async trackPurchaseAuto(options) {
1190
+ const userId = options.tracking_user_id || this.getUserId();
1191
+ if (!userId) {
1192
+ this.logger.error("❌ trackPurchaseAuto requires tracking_user_id. Please set user ID using setUserId() or pass it in options.tracking_user_id.");
1193
+ return;
1194
+ }
1195
+ await this.trackEvent(EventType.PURCHASE, options.purchaseData, userId, options.revenue, options.currency || Currency.USD);
1196
+ }
1197
+ // Track login with auto user ID (object parameter format)
1198
+ async trackLoginAuto(options) {
1199
+ const userId = options.tracking_user_id || this.getUserId();
1200
+ if (!userId) {
1201
+ this.logger.error("❌ trackLoginAuto requires tracking_user_id. Please set user ID using setUserId() or pass it in options.tracking_user_id.");
1202
+ return;
1203
+ }
1204
+ await this.trackEvent(EventType.LOGIN, options.loginData, userId);
1205
+ }
1206
+ // Track signup with auto user ID (object parameter format)
1207
+ async trackSignupAuto(options) {
1208
+ const userId = options.tracking_user_id || this.getUserId();
1209
+ if (!userId) {
1210
+ this.logger.error("❌ trackSignupAuto requires tracking_user_id. Please set user ID using setUserId() or pass it in options.tracking_user_id.");
1211
+ return;
1212
+ }
1213
+ await this.trackEvent(EventType.SIGNUP, options.signupData, userId);
1214
+ }
1215
+ // Track email verification with auto user ID (object parameter format)
1216
+ async trackEmailVerificationAuto(options) {
1217
+ const userId = options.tracking_user_id || this.getUserId();
1218
+ if (!userId) {
1219
+ this.logger.error("❌ trackEmailVerificationAuto requires tracking_user_id. Please set user ID using setUserId() or pass it in options.tracking_user_id.");
1220
+ return;
1221
+ }
1222
+ await this.trackEvent(EventType.EMAIL_VERIFICATION, options.verificationData, userId);
1223
+ }
1049
1224
  // Track product view
1050
1225
  async trackProductView(tracking_user_id, productData) {
1051
1226
  await this.trackEvent(EventType.PRODUCT_VIEW, productData, tracking_user_id);
@@ -1131,6 +1306,42 @@ class AttributionSDK {
1131
1306
  this.logger.debug("UTM params for event:", filteredParams);
1132
1307
  return filteredParams;
1133
1308
  }
1309
+ // Initialize user ID
1310
+ initializeUserId() {
1311
+ // If userId is provided in config, set it (will override existing)
1312
+ if (this.config.userId) {
1313
+ this.setUserId(this.config.userId);
1314
+ this.logger.info(`👤 User ID initialized from config: ${this.config.userId}`);
1315
+ }
1316
+ else {
1317
+ // Check if user ID already exists in storage
1318
+ const existingUserId = this.getUserId();
1319
+ if (existingUserId) {
1320
+ this.logger.debug(`👤 Existing user ID found: ${existingUserId}`);
1321
+ }
1322
+ else {
1323
+ this.logger.debug("👤 No user ID found, will be set when user identifies");
1324
+ }
1325
+ }
1326
+ }
1327
+ // Set user ID
1328
+ setUserId(userId) {
1329
+ if (!userId || userId.trim() === "") {
1330
+ this.logger.warn("Cannot set empty user ID");
1331
+ return;
1332
+ }
1333
+ this.storage.setUserId(userId);
1334
+ this.logger.info(`👤 User ID set: ${userId}`);
1335
+ }
1336
+ // Get user ID
1337
+ getUserId() {
1338
+ return this.storage.getUserId();
1339
+ }
1340
+ // Remove user ID
1341
+ removeUserId() {
1342
+ this.storage.removeUserId();
1343
+ this.logger.info("👤 User ID removed");
1344
+ }
1134
1345
  // Initialize user session
1135
1346
  initializeSession() {
1136
1347
  const existingSession = this.storage.getSession();
@@ -1580,6 +1791,7 @@ function autoInit() {
1580
1791
  autoCleanUTM: script.getAttribute("data-auto-clean-utm") !== "false",
1581
1792
  batchSize: parseInt(script.getAttribute("data-batch-size") || "100"),
1582
1793
  batchInterval: parseInt(script.getAttribute("data-batch-interval") || "2000"),
1794
+ userId: script.getAttribute("data-user-id") || undefined,
1583
1795
  };
1584
1796
  // Initialize SDK
1585
1797
  init(config);
@@ -1723,6 +1935,51 @@ async function trackEmailVerification(tracking_user_id, verificationData) {
1723
1935
  }
1724
1936
  await sdk.trackEmailVerification(tracking_user_id, verificationData);
1725
1937
  }
1938
+ // Track audit approved (conversion)
1939
+ async function trackAuditApproved(tracking_user_id, auditData) {
1940
+ const sdk = getSDK();
1941
+ if (!sdk) {
1942
+ console.warn("GetuAI SDK: Not initialized. Call init() first.");
1943
+ return;
1944
+ }
1945
+ await sdk.trackAuditApproved(tracking_user_id, auditData);
1946
+ }
1947
+ // Track purchase with auto user ID (object parameter format)
1948
+ async function trackPurchaseAuto(options) {
1949
+ const sdk = getSDK();
1950
+ if (!sdk) {
1951
+ console.warn("GetuAI SDK: Not initialized. Call init() first.");
1952
+ return;
1953
+ }
1954
+ await sdk.trackPurchaseAuto(options);
1955
+ }
1956
+ // Track login with auto user ID (object parameter format)
1957
+ async function trackLoginAuto(options) {
1958
+ const sdk = getSDK();
1959
+ if (!sdk) {
1960
+ console.warn("GetuAI SDK: Not initialized. Call init() first.");
1961
+ return;
1962
+ }
1963
+ await sdk.trackLoginAuto(options);
1964
+ }
1965
+ // Track signup with auto user ID (object parameter format)
1966
+ async function trackSignupAuto(options) {
1967
+ const sdk = getSDK();
1968
+ if (!sdk) {
1969
+ console.warn("GetuAI SDK: Not initialized. Call init() first.");
1970
+ return;
1971
+ }
1972
+ await sdk.trackSignupAuto(options);
1973
+ }
1974
+ // Track email verification with auto user ID (object parameter format)
1975
+ async function trackEmailVerificationAuto(options) {
1976
+ const sdk = getSDK();
1977
+ if (!sdk) {
1978
+ console.warn("GetuAI SDK: Not initialized. Call init() first.");
1979
+ return;
1980
+ }
1981
+ await sdk.trackEmailVerificationAuto(options);
1982
+ }
1726
1983
  // Track product view
1727
1984
  async function trackProductView(tracking_user_id, productData) {
1728
1985
  const sdk = getSDK();
@@ -1792,6 +2049,33 @@ function getCurrentUTMParams() {
1792
2049
  }
1793
2050
  return sdk.getCurrentUTMParams();
1794
2051
  }
2052
+ // Set user ID
2053
+ function setUserId(userId) {
2054
+ const sdk = getSDK();
2055
+ if (!sdk) {
2056
+ console.warn("GetuAI SDK: Not initialized. Call init() first.");
2057
+ return;
2058
+ }
2059
+ sdk.setUserId(userId);
2060
+ }
2061
+ // Get user ID
2062
+ function getUserId() {
2063
+ const sdk = getSDK();
2064
+ if (!sdk) {
2065
+ console.warn("GetuAI SDK: Not initialized. Call init() first.");
2066
+ return null;
2067
+ }
2068
+ return sdk.getUserId();
2069
+ }
2070
+ // Remove user ID
2071
+ function removeUserId() {
2072
+ const sdk = getSDK();
2073
+ if (!sdk) {
2074
+ console.warn("GetuAI SDK: Not initialized. Call init() first.");
2075
+ return;
2076
+ }
2077
+ sdk.removeUserId();
2078
+ }
1795
2079
  // Destroy SDK
1796
2080
  function destroy() {
1797
2081
  if (globalSDK) {
@@ -1838,6 +2122,22 @@ class AttributionSDKStatic extends AttributionSDK {
1838
2122
  static async trackEmailVerification(tracking_user_id, verificationData) {
1839
2123
  return await trackEmailVerification(tracking_user_id, verificationData);
1840
2124
  }
2125
+ // Static method to track purchase with auto user ID
2126
+ static async trackPurchaseAuto(options) {
2127
+ return await trackPurchaseAuto(options);
2128
+ }
2129
+ // Static method to track login with auto user ID
2130
+ static async trackLoginAuto(options) {
2131
+ return await trackLoginAuto(options);
2132
+ }
2133
+ // Static method to track signup with auto user ID
2134
+ static async trackSignupAuto(options) {
2135
+ return await trackSignupAuto(options);
2136
+ }
2137
+ // Static method to track email verification with auto user ID
2138
+ static async trackEmailVerificationAuto(options) {
2139
+ return await trackEmailVerificationAuto(options);
2140
+ }
1841
2141
  // Static method to track product view
1842
2142
  static async trackProductView(tracking_user_id, productData) {
1843
2143
  return await trackProductView(tracking_user_id, productData);
@@ -1870,6 +2170,18 @@ class AttributionSDKStatic extends AttributionSDK {
1870
2170
  static getCurrentUTMParams() {
1871
2171
  return getCurrentUTMParams();
1872
2172
  }
2173
+ // Static method to set user ID
2174
+ static setUserId(userId) {
2175
+ setUserId(userId);
2176
+ }
2177
+ // Static method to get user ID
2178
+ static getUserId() {
2179
+ return getUserId();
2180
+ }
2181
+ // Static method to remove user ID
2182
+ static removeUserId() {
2183
+ removeUserId();
2184
+ }
1873
2185
  // Static method to destroy SDK
1874
2186
  static destroy() {
1875
2187
  destroy();
@@ -1904,13 +2216,21 @@ if (typeof window !== "undefined") {
1904
2216
  trackFormSubmit,
1905
2217
  trackVideoPlay,
1906
2218
  trackEmailVerification,
2219
+ trackAuditApproved,
1907
2220
  trackProductView,
1908
2221
  trackAddToCart,
2222
+ trackPurchaseAuto,
2223
+ trackLoginAuto,
2224
+ trackSignupAuto,
2225
+ trackEmailVerificationAuto,
1909
2226
  getAttributionData,
1910
2227
  flush,
1911
2228
  getStatus,
1912
2229
  addUTMToURL,
1913
2230
  getCurrentUTMParams,
2231
+ setUserId,
2232
+ getUserId,
2233
+ removeUserId,
1914
2234
  destroy,
1915
2235
  EventType,
1916
2236
  Currency,
@@ -1930,11 +2250,18 @@ if (typeof window !== "undefined") {
1930
2250
  window.trackEmailVerification = trackEmailVerification;
1931
2251
  window.trackProductView = trackProductView;
1932
2252
  window.trackAddToCart = trackAddToCart;
2253
+ window.trackPurchaseAuto = trackPurchaseAuto;
2254
+ window.trackLoginAuto = trackLoginAuto;
2255
+ window.trackSignupAuto = trackSignupAuto;
2256
+ window.trackEmailVerificationAuto = trackEmailVerificationAuto;
1933
2257
  window.getAttributionData = getAttributionData;
1934
2258
  window.flush = flush;
1935
2259
  window.getStatus = getStatus;
1936
2260
  window.addUTMToURL = addUTMToURL;
1937
2261
  window.getCurrentUTMParams = getCurrentUTMParams;
2262
+ window.setUserId = setUserId;
2263
+ window.getUserId = getUserId;
2264
+ window.removeUserId = removeUserId;
1938
2265
  window.destroy = destroy;
1939
2266
  window.AttributionSDK = AttributionSDKStatic; // 直接暴露带静态方法的AttributionSDK类
1940
2267
  }
@@ -1954,16 +2281,23 @@ var index = {
1954
2281
  trackEmailVerification,
1955
2282
  trackProductView,
1956
2283
  trackAddToCart,
2284
+ trackPurchaseAuto,
2285
+ trackLoginAuto,
2286
+ trackSignupAuto,
2287
+ trackEmailVerificationAuto,
1957
2288
  getAttributionData,
1958
2289
  flush,
1959
2290
  getStatus,
1960
2291
  addUTMToURL,
1961
2292
  getCurrentUTMParams,
2293
+ setUserId,
2294
+ getUserId,
2295
+ removeUserId,
1962
2296
  destroy,
1963
2297
  EventType,
1964
2298
  Currency,
1965
2299
  AttributionSDK: AttributionSDKStatic, // 包含带静态方法的AttributionSDK类
1966
2300
  };
1967
2301
 
1968
- export { AttributionSDKStatic as AttributionSDK, Currency, EventType, addUTMToURL, index as default, destroy, flush, getAttributionData, getCurrentUTMParams, getSDK, getStatus, init, trackAddToCart, trackEmailVerification, trackEvent, trackFormSubmit, trackLogin, trackPageClick, trackPageView, trackProductView, trackPurchase, trackSignup, trackVideoPlay, waitForSDK };
2302
+ export { AttributionSDKStatic as AttributionSDK, Currency, EventType, addUTMToURL, index as default, destroy, flush, getAttributionData, getCurrentUTMParams, getSDK, getStatus, getUserId, init, removeUserId, setUserId, trackAddToCart, trackAuditApproved, trackEmailVerification, trackEmailVerificationAuto, trackEvent, trackFormSubmit, trackLogin, trackLoginAuto, trackPageClick, trackPageView, trackProductView, trackPurchase, trackPurchaseAuto, trackSignup, trackSignupAuto, trackVideoPlay, waitForSDK };
1969
2303
  //# sourceMappingURL=index.esm.js.map