Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error in sendEvent when localStorage is null or otherwise unavailable #25

Closed
ellotheth opened this issue Feb 23, 2022 · 3 comments · Fixed by #31
Closed

Error in sendEvent when localStorage is null or otherwise unavailable #25

ellotheth opened this issue Feb 23, 2022 · 3 comments · Fixed by #31
Labels
bug Something isn't working

Comments

@ellotheth
Copy link

ellotheth commented Feb 23, 2022

Versions

Describe the bug

I'm using Plausible with ReactJS, and I enable automatic pageview tracking:

import Plausible from "plausible-tracker";

const plausible = Plausible({domain: 'my-domain'});

// Context setup, routes, yadda yadda

plausible.enableAutoPageviews();

If localStorage is null or disabled (e.g. Block all cookies in Chromium 98), plausible.enableAutoPageviews() throws an error:

TypeError: Cannot read properties of null (reading 'getItem')
  at sendEvent(./node_modules/plausible-tracker/build/module/lib/request.js:13:53)
  at trackEvent(./node_modules/plausible-tracker/build/module/lib/tracker.js:51:9)
  at trackPageview(./node_modules/plausible-tracker/build/module/lib/tracker.js:54:9)
  at enableAutoPageviews(./node_modules/plausible-tracker/build/module/lib/tracker.js:73:9)
  at moduleId(./spa/js/index.jsx:114:11)

Expected behavior

Tracking should continue as if the plausible_ignore key were unset.

Steps to reproduce

  1. Disable localStorage in your browser of choice
  2. Load a page that uses plausible.enableAutoPageviews()

Your Environment

  • Device: PC
  • OS: Linux (Ubuntu)
  • Browser: Chrome 98, Firefox 98 with DOM storage disabled
@ellotheth ellotheth added the bug Something isn't working label Feb 23, 2022
@ellotheth
Copy link
Author

In case anybody else hits this, I've got a (very temporary) workaround that wraps Plausible operations and eats any errors:

const safe = (callback, args) => {
    try {
        return callback(...args);
    } catch (e) {
        // fail silently
    }
}

const safePlausible = plausible => ({
    trackEvent: (...args) => safe(plausible.trackEvent, args),
    trackPageview: (...args) => safe(plausible.trackPageview, args),
    enableAutoPageviews: (...args) => safe(plausible.enableAutoPageviews, args),
    enableAutoOutboundTracking: (...args) => safe(plausible.enableAutoOutboundTracking, args),
});

const plausible = safePlausible(Plausible(options));

@sedubois
Copy link

sedubois commented Apr 8, 2022

Using plausible-tracker@0.3.5, I am encountering the same error Cannot read properties of null (reading 'getItem') when calling plausible.trackPageview(), especially with calls from Android Webviews where setDomStorageEnabled has not been called (it is false by default).

@Windvis
Copy link
Contributor

Windvis commented May 13, 2022

Sorry about that. I didn't know that disabling localStorage was a thing 😄. Should be fixed by #31 👍 .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants