|
483 | 483 | finalProperties = { value: properties }; |
484 | 484 | } |
485 | 485 |
|
| 486 | + const customEvent = { |
| 487 | + type: 'custom', |
| 488 | + eventId: generateUUIDv4(), |
| 489 | + name: eventName, |
| 490 | + anonymousId: this.anonymousId, |
| 491 | + sessionId: this.sessionId, |
| 492 | + timestamp: Date.now(), |
| 493 | + properties: finalProperties, |
| 494 | + }; |
| 495 | + |
| 496 | + if (this.options.enableBatching) { |
| 497 | + return this.send(customEvent); |
| 498 | + } |
| 499 | + |
| 500 | + try { |
| 501 | + const beaconResult = await this.sendBeacon(customEvent); |
| 502 | + if (beaconResult) { |
| 503 | + return beaconResult; |
| 504 | + } |
| 505 | + } catch (_e) {} |
| 506 | + |
| 507 | + return this.send(customEvent); |
| 508 | + } |
| 509 | + |
| 510 | + async _trackSystemEvent(eventName, properties) { |
| 511 | + if (this.options.disabled || this.isLikelyBot) { |
| 512 | + return; |
| 513 | + } |
| 514 | + |
| 515 | + if (this.options.samplingRate < 1.0) { |
| 516 | + const samplingValue = Math.random(); |
| 517 | + |
| 518 | + if (samplingValue > this.options.samplingRate) { |
| 519 | + return { sampled: false }; |
| 520 | + } |
| 521 | + } |
| 522 | + |
| 523 | + let finalProperties; |
| 524 | + if (properties === undefined || properties === null) { |
| 525 | + finalProperties = {}; |
| 526 | + } else if (typeof properties === 'object') { |
| 527 | + finalProperties = properties; |
| 528 | + } else { |
| 529 | + finalProperties = { value: properties }; |
| 530 | + } |
| 531 | + |
486 | 532 | // Collect base context data |
487 | 533 | const baseContext = this.getBaseContext(); |
488 | 534 |
|
|
970 | 1016 | }; |
971 | 1017 | } |
972 | 1018 |
|
973 | | - trackCustomEvent(eventName, properties = {}) { |
974 | | - if (this.isServer()) { |
975 | | - return; |
976 | | - } |
977 | | - |
978 | | - let finalProperties; |
979 | | - if (properties === undefined || properties === null) { |
980 | | - finalProperties = {}; |
981 | | - } else if (typeof properties === 'object') { |
982 | | - finalProperties = properties; |
983 | | - } else { |
984 | | - finalProperties = { value: properties }; |
985 | | - } |
986 | | - |
987 | | - const customEvent = { |
988 | | - type: 'custom', |
989 | | - eventId: generateUUIDv4(), |
990 | | - name: eventName, |
991 | | - anonymousId: this.anonymousId, |
992 | | - sessionId: this.sessionId, |
993 | | - timestamp: Date.now(), |
994 | | - properties: finalProperties, |
995 | | - }; |
996 | | - |
997 | | - if (this.options.enableBatching) { |
998 | | - return this.send(customEvent); |
999 | | - } |
1000 | | - |
1001 | | - try { |
1002 | | - const beaconResult = this.sendBeacon(customEvent); |
1003 | | - if (beaconResult) { |
1004 | | - return beaconResult; |
1005 | | - } |
1006 | | - } catch (_e) {} |
1007 | | - |
1008 | | - return this.send(customEvent); |
1009 | | - } |
1010 | | - |
1011 | 1019 | async trackOutgoingLink(linkData) { |
1012 | 1020 | if (this.isServer()) { |
1013 | 1021 | return; |
|
1373 | 1381 | ...(n ?? {}), |
1374 | 1382 | }; |
1375 | 1383 |
|
1376 | | - this.track('screen_view', pageData); |
| 1384 | + this._trackSystemEvent('screen_view', pageData); |
1377 | 1385 | } |
1378 | 1386 | } |
1379 | 1387 | }; |
|
1398 | 1406 | clear: () => {}, |
1399 | 1407 | flush: () => {}, |
1400 | 1408 | setGlobalProperties: () => {}, |
1401 | | - trackCustomEvent: () => {}, |
| 1409 | + |
1402 | 1410 | trackOutgoingLink: () => {}, |
1403 | 1411 | options: { disabled: true }, |
1404 | 1412 | }; |
|
1409 | 1417 | clear: () => {}, |
1410 | 1418 | flush: () => {}, |
1411 | 1419 | setGlobalProperties: () => {}, |
1412 | | - trackCustomEvent: () => {}, |
| 1420 | + |
1413 | 1421 | trackOutgoingLink: () => {}, |
1414 | 1422 | }; |
1415 | 1423 |
|
|
1562 | 1570 | }); |
1563 | 1571 |
|
1564 | 1572 | window.db = { |
1565 | | - track: (...args) => window.databuddy?.trackCustomEvent(...args), |
| 1573 | + track: (...args) => window.databuddy?.track(...args), |
1566 | 1574 | screenView: (...args) => window.databuddy?.screenView(...args), |
1567 | 1575 | clear: () => window.databuddy?.clear(), |
1568 | 1576 | flush: () => window.databuddy?.flush(), |
1569 | 1577 | setGlobalProperties: (...args) => |
1570 | 1578 | window.databuddy?.setGlobalProperties(...args), |
1571 | | - trackCustomEvent: (...args) => |
1572 | | - window.databuddy?.trackCustomEvent(...args), |
| 1579 | + |
1573 | 1580 | trackOutgoingLink: (...args) => |
1574 | 1581 | window.databuddy?.trackOutgoingLink(...args), |
1575 | 1582 | }; |
|
1608 | 1615 | const noop = () => {}; |
1609 | 1616 | window.databuddy.track = noop; |
1610 | 1617 | window.databuddy.screenView = noop; |
1611 | | - window.databuddy.trackCustomEvent = noop; |
1612 | 1618 | window.databuddy.trackOutgoingLink = noop; |
1613 | 1619 | window.databuddy.clear = noop; |
1614 | 1620 | window.databuddy.flush = noop; |
|
1619 | 1625 | const noop = () => {}; |
1620 | 1626 | window.db.track = noop; |
1621 | 1627 | window.db.screenView = noop; |
1622 | | - window.db.trackCustomEvent = noop; |
1623 | 1628 | window.db.trackOutgoingLink = noop; |
1624 | 1629 | window.db.clear = noop; |
1625 | 1630 | window.db.flush = noop; |
|
0 commit comments