Amplitude-JavaScript icon indicating copy to clipboard operation
Amplitude-JavaScript copied to clipboard

Tracking fails if a page is leaved

Open ghost opened this issue 4 years ago • 4 comments

Looks like amplitude.logEvent crashes if it's followed by window.location.raplace, consider example:

...
amplitude.getInstance().init(amplitudeApiKey);
Vue.prototype.$amplitude = amplitude;
...
navigateAfterLogin() {
    this.$amplitude.getInstance().logEvent('Login passed');
    setTimeout(() => {
        this.$amplitude.getInstance().logEvent('test tracking crash');
        window.location.replace(this.nextPage);
     }, 1000);
},

I have tried to call this.$amplitude.getInstance().sendEvents() on page startup but nothing happens, so I guess LocalStorage contains no events.

Expected Behavior

Both events are successfully uploaded

Current Behavior

Second logEvent crashed. image

Environment

"amplitude-js": "^7.4.4" Chrome Version 89.0.4389.82 (Official Build) (x86_64)

ghost avatar Mar 14 '21 22:03 ghost

Seems to be related to https://github.com/amplitude/Amplitude-JavaScript/issues/136

ghost avatar Mar 15 '21 11:03 ghost

Thanks @ViktorDanyliuk for reporting this, we'll take a look!

kelvin-lu avatar Mar 15 '21 17:03 kelvin-lu

@ViktorDanyliuk I think your issue looks pretty related to page changes. We'll discuss more internally about how to build the sendBeacon API into our SDK.

For the second issue (sendEvents not working after page load) - potential culprits might be the JS SDK not loading fully w/events before trying to send, or an issue with our logic using sendEvents publicly - we can investigate this separately.

kelvin-lu avatar Mar 17 '21 17:03 kelvin-lu

I followed https://github.com/amplitude/GTM-Web-Demo, with GTM in the middle it sends events when the next page is loaded.

I would like to avoid canceled requests but it's not critical anymore.

POD666 avatar Mar 22 '21 20:03 POD666