@hyve-sdk/js 2.11.0 → 2.11.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/dist/index.mjs CHANGED
@@ -408,6 +408,309 @@ function generateUUID() {
408
408
  return uuidv4();
409
409
  }
410
410
 
411
+ // src/utils/device-info.ts
412
+ function detectBrowser(ua) {
413
+ if (/Edg\/(\d+)/.test(ua)) return { name: "Edge", major: RegExp.$1 };
414
+ if (/OPR\/(\d+)/.test(ua)) return { name: "Opera", major: RegExp.$1 };
415
+ if (/Chrome\/(\d+)/.test(ua)) return { name: "Chrome", major: RegExp.$1 };
416
+ if (/Firefox\/(\d+)/.test(ua)) return { name: "Firefox", major: RegExp.$1 };
417
+ if (/Version\/(\d+).*Safari/.test(ua)) return { name: "Safari", major: RegExp.$1 };
418
+ return { name: "unknown", major: "" };
419
+ }
420
+ function detectOS(ua) {
421
+ if (/Windows NT ([\d.]+)/.test(ua)) return { name: "Windows", version: RegExp.$1 };
422
+ if (/Mac OS X ([\d_]+)/.test(ua)) return { name: "macOS", version: RegExp.$1.replace(/_/g, ".") };
423
+ if (/Android ([\d.]+)/.test(ua)) return { name: "Android", version: RegExp.$1 };
424
+ if (/iPhone OS ([\d_]+)/.test(ua)) return { name: "iOS", version: RegExp.$1.replace(/_/g, ".") };
425
+ if (/iPad.*OS ([\d_]+)/.test(ua)) return { name: "iOS", version: RegExp.$1.replace(/_/g, ".") };
426
+ if (/CrOS/.test(ua)) return { name: "Chrome OS", version: "" };
427
+ if (/Linux/.test(ua)) return { name: "Linux", version: "" };
428
+ return { name: "unknown", version: "" };
429
+ }
430
+ function detectDeviceType(ua) {
431
+ if (/Mobi|Android(?!.*Tablet)|iPhone|iPod/i.test(ua)) return "mobile";
432
+ if (/Tablet|iPad|Android.*Tablet/i.test(ua)) return "tablet";
433
+ return "desktop";
434
+ }
435
+ var _cache = null;
436
+ function getEssentialDeviceInfo() {
437
+ if (_cache) return _cache;
438
+ if (typeof window === "undefined" || typeof navigator === "undefined") {
439
+ return {
440
+ browser: "unknown",
441
+ os: "unknown",
442
+ device_type: null,
443
+ viewport: "0x0",
444
+ screen: "0x0",
445
+ pixel_ratio: null,
446
+ language: null,
447
+ timezone: null,
448
+ country_code: null
449
+ };
450
+ }
451
+ const ua = navigator.userAgent;
452
+ const browser = detectBrowser(ua);
453
+ const os = detectOS(ua);
454
+ const deviceType = detectDeviceType(ua);
455
+ let timezone = null;
456
+ try {
457
+ timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
458
+ } catch {
459
+ }
460
+ const language = navigator.language || null;
461
+ let country_code = null;
462
+ if (language) {
463
+ const parts = language.split("-");
464
+ if (parts.length > 1) {
465
+ country_code = parts[parts.length - 1].toUpperCase();
466
+ }
467
+ }
468
+ _cache = {
469
+ browser: browser.major ? `${browser.name} ${browser.major}` : browser.name,
470
+ os: os.version ? `${os.name} ${os.version}` : os.name,
471
+ device_type: deviceType,
472
+ viewport: `${window.innerWidth ?? 0}x${window.innerHeight ?? 0}`,
473
+ screen: `${window.screen?.width ?? 0}x${window.screen?.height ?? 0}`,
474
+ pixel_ratio: window.devicePixelRatio || null,
475
+ language,
476
+ timezone,
477
+ country_code
478
+ };
479
+ return _cache;
480
+ }
481
+
482
+ // src/utils/attribution.ts
483
+ var EMBEDDING_PLATFORM_PATTERNS = {
484
+ twitter: [/t\.co/i, /twitter\.com/i, /x\.com/i, /mobile\.twitter\.com/i, /mobile\.x\.com/i],
485
+ discord: [/discord\.com/i, /discordapp\.com/i, /discord\.gg/i],
486
+ telegram: [/telegram\.org/i, /telegram\.me/i, /t\.me/i, /web\.telegram\.org/i],
487
+ facebook: [/facebook\.com/i, /fb\.com/i, /m\.facebook\.com/i, /mobile\.facebook\.com/i, /meta\.com/i],
488
+ reddit: [/reddit\.com/i, /redd\.it/i, /old\.reddit\.com/i, /www\.reddit\.com/i],
489
+ linkedin: [/linkedin\.com/i, /lnkd\.in/i],
490
+ instagram: [/instagram\.com/i, /instagr\.am/i],
491
+ youtube: [/youtube\.com/i, /youtu\.be/i, /m\.youtube\.com/i],
492
+ twitch: [/twitch\.tv/i, /m\.twitch\.tv/i],
493
+ farcaster: [/warpcast\.com/i, /farcaster\.xyz/i, /miniapps\.farcaster\.xyz/i],
494
+ worldapp: [/world\.app/i, /worldcoin\.org/i],
495
+ tiktok: [/tiktok\.com/i, /vm\.tiktok\.com/i]
496
+ };
497
+ var AttributionManager = class _AttributionManager {
498
+ static instance;
499
+ cachedData = null;
500
+ STORAGE_KEY = "hyve_attribution";
501
+ SESSION_COUNT_KEY = "hyve_session_count";
502
+ static getInstance() {
503
+ if (!_AttributionManager.instance) {
504
+ _AttributionManager.instance = new _AttributionManager();
505
+ }
506
+ return _AttributionManager.instance;
507
+ }
508
+ captureAttribution() {
509
+ if (typeof window === "undefined") return this.getEmptyAttribution();
510
+ if (this.cachedData) return this.cachedData;
511
+ const urlParams = new URLSearchParams(window.location.search);
512
+ const referrerUrl = document.referrer;
513
+ const referrerDomain = this.extractDomain(referrerUrl);
514
+ const platformDetection = this.detectEmbeddingPlatform(urlParams, referrerUrl);
515
+ const attribution = {
516
+ utm_source: urlParams.get("utm_source"),
517
+ utm_medium: urlParams.get("utm_medium"),
518
+ utm_campaign: urlParams.get("utm_campaign"),
519
+ utm_term: urlParams.get("utm_term"),
520
+ utm_content: urlParams.get("utm_content"),
521
+ utm_id: urlParams.get("utm_id"),
522
+ utm_source_platform: urlParams.get("utm_source_platform"),
523
+ platform_id: platformDetection.platform,
524
+ is_embedded: platformDetection.isEmbedded,
525
+ referrer_url: referrerUrl || null,
526
+ referrer_domain: referrerDomain,
527
+ referrer_source: this.categorizeReferrer(referrerUrl),
528
+ is_first_party_referrer: this.isFirstPartyReferrer(referrerDomain),
529
+ traffic_type: this.classifyTrafficType(urlParams, referrerUrl),
530
+ first_visit_timestamp: Date.now(),
531
+ session_count: this.getSessionCount() + 1,
532
+ is_bot: this.detectBot(),
533
+ attribution_confidence: this.calculateConfidence(urlParams, referrerUrl)
534
+ };
535
+ this.cachedData = attribution;
536
+ this.storeAttribution(attribution);
537
+ return attribution;
538
+ }
539
+ getAttribution() {
540
+ if (this.cachedData) return this.cachedData;
541
+ if (typeof window === "undefined") return this.getEmptyAttribution();
542
+ const stored = sessionStorage.getItem(this.STORAGE_KEY);
543
+ if (stored) {
544
+ try {
545
+ const parsed = JSON.parse(stored);
546
+ if (this.isValidAttributionData(parsed)) {
547
+ this.cachedData = parsed;
548
+ return parsed;
549
+ }
550
+ } catch {
551
+ }
552
+ }
553
+ return this.captureAttribution();
554
+ }
555
+ classifyTrafficType(urlParams, referrerUrl) {
556
+ if (urlParams.get("utm_source")) return "campaign";
557
+ if (!referrerUrl) return "direct";
558
+ const domain = this.extractDomain(referrerUrl);
559
+ if (this.isSearchEngine(domain)) return "search";
560
+ if (this.isSocialPlatform(domain)) return "social";
561
+ if (this.isEmailClient(domain)) return "email";
562
+ if (this.isFirstPartyReferrer(domain)) return "direct";
563
+ return "referral";
564
+ }
565
+ isCdnUrl(url) {
566
+ if (!url) return false;
567
+ return [/cloudfront\.net/i, /cdn\.[\w-]+\.com/i, /\.cdn\./i, /fastly\.net/i, /akamai/i, /\.cloudflare\./i].some(
568
+ (p) => p.test(url)
569
+ );
570
+ }
571
+ detectEmbeddingPlatform(urlParams, referrerUrl) {
572
+ let isEmbedded = false;
573
+ try {
574
+ isEmbedded = window.self !== window.top;
575
+ } catch {
576
+ isEmbedded = true;
577
+ }
578
+ if (!isEmbedded) return { platform: "web", isEmbedded: false };
579
+ const platformParam = urlParams.get("platform")?.toLowerCase().trim();
580
+ if (platformParam && /^[a-z0-9_-]+$/.test(platformParam)) {
581
+ return { platform: platformParam, isEmbedded: true };
582
+ }
583
+ if (!referrerUrl || this.isCdnUrl(referrerUrl)) {
584
+ return { platform: "unknown", isEmbedded: true };
585
+ }
586
+ const normalizedRef = referrerUrl.toLowerCase();
587
+ for (const [platform, patterns] of Object.entries(EMBEDDING_PLATFORM_PATTERNS)) {
588
+ if (patterns.some((p) => p.test(normalizedRef))) {
589
+ return { platform, isEmbedded: true };
590
+ }
591
+ }
592
+ try {
593
+ if (window.location.ancestorOrigins?.length > 0) {
594
+ const parentOrigin = window.location.ancestorOrigins[0];
595
+ if (parentOrigin && !this.isCdnUrl(parentOrigin)) {
596
+ for (const [platform, patterns] of Object.entries(EMBEDDING_PLATFORM_PATTERNS)) {
597
+ if (patterns.some((p) => p.test(parentOrigin))) {
598
+ return { platform, isEmbedded: true };
599
+ }
600
+ }
601
+ }
602
+ }
603
+ } catch {
604
+ }
605
+ return { platform: "unknown", isEmbedded: true };
606
+ }
607
+ categorizeReferrer(referrerUrl) {
608
+ if (!referrerUrl) return "direct";
609
+ const r = referrerUrl.toLowerCase();
610
+ if (r.includes("checkout.stripe.com")) return "direct";
611
+ if (r.includes("t.co") || r.includes("x.com") || r.includes("twitter.com")) return "twitter";
612
+ if (r.includes("farcaster.xyz") || r.includes("warpcast.com")) return "farcaster";
613
+ if (r.includes("discord.com") || r.includes("discord.gg")) return "discord";
614
+ if (r.includes("t.me") || r.includes("telegram.me") || r.includes("telegram.org")) return "telegram";
615
+ if (r.includes("reddit.com") || r.includes("old.reddit.com")) return "reddit";
616
+ if (r.includes("facebook.com") || r.includes("fb.com") || r.includes("meta.com")) return "facebook";
617
+ if (r.includes("linkedin.com")) return "linkedin";
618
+ if (r.includes("youtube.com") || r.includes("youtu.be")) return "youtube";
619
+ if (r.includes("google.com")) return "google";
620
+ if (r.includes("bing.com")) return "bing";
621
+ if (r.includes("duckduckgo.com")) return "duckduckgo";
622
+ if (r.includes("yahoo.com")) return "yahoo";
623
+ if (r.includes("world.app")) return "worldapp";
624
+ if (r.includes("instagram.com")) return "instagram";
625
+ if (r.includes("tiktok.com")) return "tiktok";
626
+ return this.extractDomain(referrerUrl) || "unknown";
627
+ }
628
+ detectBot() {
629
+ if (typeof navigator === "undefined") return false;
630
+ const ua = navigator.userAgent.toLowerCase();
631
+ return ["bot", "crawler", "spider", "scraper", "headless", "phantom", "selenium", "puppeteer", "playwright", "googlebot", "bingbot", "slurp", "duckduckbot"].some(
632
+ (indicator) => ua.includes(indicator)
633
+ );
634
+ }
635
+ calculateConfidence(urlParams, referrerUrl) {
636
+ let confidence = 0.5;
637
+ if (urlParams.get("utm_source")) confidence += 0.3;
638
+ if (urlParams.get("utm_campaign")) confidence += 0.2;
639
+ if (urlParams.get("utm_medium")) confidence += 0.1;
640
+ if (referrerUrl && !this.isFirstPartyReferrer(this.extractDomain(referrerUrl))) confidence += 0.2;
641
+ if (this.detectBot()) confidence -= 0.5;
642
+ if (urlParams.get("utm_source") === "test" || urlParams.get("utm_campaign") === "test") confidence -= 0.2;
643
+ return Math.max(0, Math.min(1, confidence));
644
+ }
645
+ extractDomain(url) {
646
+ if (!url) return "";
647
+ try {
648
+ return new URL(url).hostname.toLowerCase();
649
+ } catch {
650
+ return "";
651
+ }
652
+ }
653
+ isFirstPartyReferrer(domain) {
654
+ return ["hyve.gg", "dev.hyve.gg", "localhost"].some((d) => domain.includes(d));
655
+ }
656
+ isSearchEngine(domain) {
657
+ return ["google.com", "bing.com", "duckduckgo.com", "yahoo.com", "yandex.com"].some((e) => domain.includes(e));
658
+ }
659
+ isSocialPlatform(domain) {
660
+ return ["twitter.com", "x.com", "facebook.com", "meta.com", "linkedin.com", "reddit.com", "instagram.com", "tiktok.com", "youtube.com", "discord.com", "telegram.org", "farcaster.xyz", "warpcast.com", "t.me", "world.app"].some(
661
+ (p) => domain.includes(p)
662
+ );
663
+ }
664
+ isEmailClient(domain) {
665
+ return ["mail.google.com", "outlook.live.com", "mail.yahoo.com", "mail.aol.com"].some((c) => domain.includes(c));
666
+ }
667
+ getSessionCount() {
668
+ if (typeof window === "undefined") return 0;
669
+ try {
670
+ const stored = localStorage.getItem(this.SESSION_COUNT_KEY);
671
+ return stored ? parseInt(stored, 10) : 0;
672
+ } catch {
673
+ return 0;
674
+ }
675
+ }
676
+ storeAttribution(attribution) {
677
+ if (typeof window === "undefined") return;
678
+ try {
679
+ sessionStorage.setItem(this.STORAGE_KEY, JSON.stringify(attribution));
680
+ localStorage.setItem(this.SESSION_COUNT_KEY, attribution.session_count.toString());
681
+ } catch {
682
+ }
683
+ }
684
+ isValidAttributionData(data) {
685
+ return data !== null && typeof data === "object" && "traffic_type" in data && "attribution_confidence" in data && "platform_id" in data && "is_embedded" in data;
686
+ }
687
+ getEmptyAttribution() {
688
+ return {
689
+ utm_source: null,
690
+ utm_medium: null,
691
+ utm_campaign: null,
692
+ utm_term: null,
693
+ utm_content: null,
694
+ utm_id: null,
695
+ utm_source_platform: null,
696
+ platform_id: "web",
697
+ is_embedded: false,
698
+ referrer_url: null,
699
+ referrer_domain: null,
700
+ referrer_source: null,
701
+ is_first_party_referrer: false,
702
+ traffic_type: "direct",
703
+ first_visit_timestamp: 0,
704
+ session_count: 0,
705
+ is_bot: false,
706
+ attribution_confidence: 0
707
+ };
708
+ }
709
+ };
710
+ function getAttributionData() {
711
+ return AttributionManager.getInstance().getAttribution();
712
+ }
713
+
411
714
  // src/services/ads.ts
412
715
  var AdsService = class {
413
716
  config = {
@@ -1718,20 +2021,39 @@ var HyveClient = class {
1718
2021
  return false;
1719
2022
  }
1720
2023
  }
1721
- const toJsonString = (data) => {
1722
- if (!data) return null;
1723
- return typeof data === "string" ? data : JSON.stringify(data);
2024
+ let parsedEventDetails = {};
2025
+ if (eventDetails) {
2026
+ if (typeof eventDetails === "string") {
2027
+ try {
2028
+ parsedEventDetails = JSON.parse(eventDetails);
2029
+ } catch {
2030
+ }
2031
+ } else {
2032
+ parsedEventDetails = eventDetails;
2033
+ }
2034
+ }
2035
+ const attribution = getAttributionData();
2036
+ const enrichedEventDetails = {
2037
+ // Device info
2038
+ ...getEssentialDeviceInfo(),
2039
+ // Attribution data
2040
+ ...attribution,
2041
+ // Timestamp and session context
2042
+ timestamp: (/* @__PURE__ */ new Date()).toISOString(),
2043
+ session_id: this.sessionId,
2044
+ // Event-specific details can override any of the above
2045
+ ...parsedEventDetails
1724
2046
  };
1725
2047
  const telemetryEvent = {
1726
2048
  game_id: this.gameId,
1727
2049
  session_id: this.sessionId,
1728
- platform_id: platformId || null,
2050
+ platform_id: platformId || attribution.platform_id || null,
1729
2051
  event_location: eventLocation,
1730
2052
  event_category: eventCategory,
1731
2053
  event_action: eventAction,
1732
2054
  event_sub_category: eventSubCategory || null,
1733
2055
  event_sub_action: eventSubAction || null,
1734
- event_details: toJsonString(eventDetails)
2056
+ event_details: enrichedEventDetails
1735
2057
  };
1736
2058
  logger.debug("Sending telemetry event:", telemetryEvent);
1737
2059
  const telemetryUrl = `${this.apiBaseUrl}/api/v1/analytics/send`;