Wait for GDPR Consent
If you include user information in your TrackJS configuration, you may need to wait for user consent before tracking errors. To do this, you’ll install the agent normally, but intercept and store errors with the onError
callback until you receive user consent. You can adapt the following code sample into your application.
NOTE By default, the TrackJS agent does not capture any user-identifiable information. Waiting for user consent is not required with an anonymous installation.
// Setup a holding place for errors before user accepts
TrackJS._cache = [];
TrackJS._userConsent = false;
window.TrackJS && TrackJS.install({
token: "{TOKEN}",
onError: function (payload, err) {
// User has not consented, cache the error payload and wait
if (!TrackJS._userConsent) {
TrackJS._cache.push(payload);
return false;
}
// We're flushing the cache, ignore the error and take one off the cache
else if (err && err.message && err.message.indexOf('cache_flush') === 0 && TrackJS._cache.length) {
Object.assign(payload, TrackJS._cache.pop());
return true;
}
return true;
});
// When the user accepts, trigger errors to flush the cache.
onUserConsent() {
TrackJS._userConsent = true;
TrackJS._cache.forEach((x, i) => {
TrackJS.track('cache_flush ' + i);
});
}