@threekit/analytics
Advanced tools
Comparing version 0.1.103 to 0.1.105
@@ -27,2 +27,5 @@ import { ARStage, type AssetOption, Configuration, type Context, OptionInteractionType, OptionsType, PlayerType, type QuizOption, ShareType, type ThreekitAuthProps, type ValueOption, VisualInteractionType } from '@threekit/rest-api'; | ||
private pageLoadTime; | ||
private debounceTimeout; | ||
private eventDebounceTimers; | ||
private eventsToDebounce; | ||
log: boolean; | ||
@@ -38,5 +41,6 @@ constructor({ auth, sessionId, userId, experienceName, experienceVersion, customUserId }: SessionProps); | ||
errorLoopProtection: boolean; | ||
private reportEvent; | ||
private sendEvent; | ||
errorCount: number; | ||
maxErrorCount: number; | ||
private reportEvent; | ||
error({ error }: { | ||
@@ -43,0 +47,0 @@ error: Error; |
@@ -18,2 +18,13 @@ import { errorToString } from '@threekit/diagnostics'; | ||
pageLoadTime = 0; | ||
debounceTimeout = 500; | ||
eventDebounceTimers = new Map(); | ||
eventsToDebounce = new Set([ | ||
Event2Type.OptionInteraction, | ||
Event2Type.Change, | ||
Event2Type.ParametricValue, | ||
Event2Type.PersonalizeText, | ||
Event2Type.Error, | ||
Event2Type.Stage, | ||
Event2Type.VisualInteraction | ||
]); | ||
log = false; | ||
@@ -112,3 +123,3 @@ constructor({ auth, sessionId = uuid.v4(), userId = uuid.v4(), experienceName, experienceVersion, customUserId }) { | ||
// never await on this function! let it run in the background | ||
reportEvent(event) { | ||
sendEvent(event) { | ||
// ensure we don't get in a recursive loop of errors | ||
@@ -130,2 +141,16 @@ if (this.errorLoopProtection) { | ||
maxErrorCount = 10; | ||
reportEvent(event) { | ||
if (!this.eventsToDebounce.has(event.eventType)) { | ||
return this.sendEvent(event); | ||
} | ||
// otherwise debounce event, based on ChatGPT strategy here: https://chatgpt.com/share/e/de565b81-b8b0-4380-ab65-611454b0525d | ||
if (this.eventDebounceTimers.has(event.eventType)) { | ||
clearTimeout(this.eventDebounceTimers.get(event.eventType)); | ||
} | ||
const timer = setTimeout(() => { | ||
this.sendEvent(event); | ||
this.eventDebounceTimers.delete(event.eventType); // Clean up after sending | ||
}, this.debounceTimeout); | ||
this.eventDebounceTimers.set(event.eventType, timer); | ||
} | ||
error({ error }) { | ||
@@ -132,0 +157,0 @@ // ensure we don't spam the service with repeated errors when things are in a bad state |
{ | ||
"name": "@threekit/analytics", | ||
"version": "0.1.103", | ||
"version": "0.1.105", | ||
"type": "module", | ||
@@ -9,7 +9,7 @@ "main": "dist/index.js", | ||
"@threekit/diagnostics": "*", | ||
"@threekit/rest-api": "^0.1.103", | ||
"@threekit/rest-api": "^0.1.105", | ||
"uuid": "^9.0.1" | ||
}, | ||
"sideEffects": false, | ||
"gitHead": "9a00149f48c3755dd5606d64739eb6b4411ef453" | ||
"gitHead": "22c5d5b8d3eb89d4b9f8a92e911beeb440aec664" | ||
} |
@@ -9,4 +9,4 @@ { | ||
"format": {}, | ||
"test": {} | ||
"test:jest": {} | ||
} | ||
} |
@@ -68,2 +68,15 @@ import { errorToString } from '@threekit/diagnostics'; | ||
private debounceTimeout = 500; | ||
private eventDebounceTimers: Map<string, ReturnType<typeof setTimeout>> = | ||
new Map(); | ||
private eventsToDebounce = new Set([ | ||
Event2Type.OptionInteraction, | ||
Event2Type.Change, | ||
Event2Type.ParametricValue, | ||
Event2Type.PersonalizeText, | ||
Event2Type.Error, | ||
Event2Type.Stage, | ||
Event2Type.VisualInteraction | ||
]); | ||
public log = false; | ||
@@ -194,3 +207,3 @@ | ||
// never await on this function! let it run in the background | ||
private reportEvent(event: Event2) { | ||
private sendEvent(event: Event2) { | ||
// ensure we don't get in a recursive loop of errors | ||
@@ -216,2 +229,20 @@ if (this.errorLoopProtection) { | ||
private reportEvent(event: Event2) { | ||
if (!this.eventsToDebounce.has(event.eventType)) { | ||
return this.sendEvent(event); | ||
} | ||
// otherwise debounce event, based on ChatGPT strategy here: https://chatgpt.com/share/e/de565b81-b8b0-4380-ab65-611454b0525d | ||
if (this.eventDebounceTimers.has(event.eventType)) { | ||
clearTimeout(this.eventDebounceTimers.get(event.eventType)); | ||
} | ||
const timer = setTimeout(() => { | ||
this.sendEvent(event); | ||
this.eventDebounceTimers.delete(event.eventType); // Clean up after sending | ||
}, this.debounceTimeout); | ||
this.eventDebounceTimers.set(event.eventType, timer); | ||
} | ||
error({ error }: { error: Error }) { | ||
@@ -218,0 +249,0 @@ // ensure we don't spam the service with repeated errors when things are in a bad state |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
200830
2181
Updated@threekit/rest-api@^0.1.105