Skip to content

Commit 339c757

Browse files
committed
fix: tracking for mobiles
1 parent a67b2c6 commit 339c757

File tree

1 file changed

+46
-9
lines changed

1 file changed

+46
-9
lines changed

apps/dashboard/public/databuddy.js

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -716,10 +716,20 @@
716716
return false;
717717
}
718718

719+
// More reliable bot detection that doesn't flag mobile browsers
719720
return (
720721
navigator.webdriver ||
721-
!navigator.plugins.length ||
722-
!navigator.languages.length
722+
// Remove plugins check - mobile browsers legitimately have 0 plugins
723+
!navigator.languages.length ||
724+
// Additional bot indicators
725+
navigator.userAgent.includes('HeadlessChrome') ||
726+
navigator.userAgent.includes('PhantomJS') ||
727+
window.callPhantom ||
728+
window._phantom ||
729+
// Check for automation frameworks
730+
window.selenium ||
731+
window.webdriver ||
732+
document.documentElement.getAttribute('webdriver') === 'true'
723733
);
724734
}
725735

@@ -728,11 +738,24 @@
728738
return;
729739
}
730740

731-
for (const event of ['mousemove', 'scroll', 'keydown']) {
741+
const interactionEvents = [
742+
'mousemove',
743+
'scroll',
744+
'keydown',
745+
'touchstart',
746+
'touchmove',
747+
'click',
748+
];
749+
750+
for (const event of interactionEvents) {
732751
window.addEventListener(
733752
event,
734753
() => {
735754
this.hasInteracted = true;
755+
// Debug mobile interaction detection
756+
if (event.startsWith('touch') && this.options.debug) {
757+
console.log(`Databuddy: Mobile interaction detected (${event})`);
758+
}
736759
},
737760
{ once: true, passive: true }
738761
);
@@ -807,6 +830,11 @@
807830
return;
808831
}
809832

833+
// Add bot detection check to match screen_view behavior
834+
if (this.options.disabled || this.isLikelyBot) {
835+
return;
836+
}
837+
810838
const baseContext = this.getBaseContext();
811839

812840
const exitEventId = `exit_${this.sessionId}_${btoa(window.location.pathname)}_${this.pageEngagementStart}`;
@@ -833,7 +861,6 @@
833861
interaction_count,
834862
has_exit_intent: this.hasExitIntent,
835863
page_count,
836-
is_bounce: page_count <= 1 ? 1 : 0,
837864
},
838865
};
839866

@@ -842,15 +869,24 @@
842869

843870
async sendExitEventImmediately(exitEvent) {
844871
try {
845-
const beaconResult = await this.sendBeacon(exitEvent);
846-
if (beaconResult) {
847-
return beaconResult;
872+
if (navigator.sendBeacon) {
873+
const beaconResult = await this.sendBeacon(exitEvent);
874+
if (beaconResult) {
875+
return beaconResult;
876+
}
848877
}
849878

850879
return this.api.fetch('/', exitEvent, {
851880
keepalive: true,
852881
});
853882
} catch (_e) {
883+
try {
884+
if (navigator.sendBeacon) {
885+
return this.sendBeacon(exitEvent);
886+
}
887+
} catch (_e2) {
888+
// Silent fail - don't block page unload
889+
}
854890
return null;
855891
}
856892
}
@@ -1230,7 +1266,8 @@
12301266

12311267
if (this.options.trackScreenViews) {
12321268
this.trackScreenViews();
1233-
setTimeout(() => this.screenView(), 0);
1269+
// Delay initial screen view to ensure proper mobile initialization
1270+
setTimeout(() => this.screenView(), 100);
12341271
}
12351272

12361273
if (this.options.trackOutgoingLinks) {
@@ -1310,7 +1347,7 @@
13101347
});
13111348
this.isInternalNavigation = true;
13121349
this.screenView();
1313-
}, 50);
1350+
}, 100); // Increase debounce for mobile stability
13141351

13151352
this.options.trackHashChanges
13161353
? window.addEventListener('hashchange', i)

0 commit comments

Comments
 (0)