posthog-js icon indicating copy to clipboard operation
posthog-js copied to clipboard

Cannot read properties of undefined (reading 'incr')

Open ytambi opened this issue 2 years ago • 1 comments

I'm trying to setup Posthog instance in one of my React applications. Since I'm using the communication logic from a separate module, I'm using posthog-js package despite it being React app. I've created the Posthog class encapsulating all the methods as per need. Below is how it looks like -

import posthog from "posthog-js";

class PostHog {
  init = () =>
    posthog?.init(process.env.ANALYTICS_POSTHOG_KEY, {
      api_host: process.env.ANALYTICS_POSTHOG_HOST,
      autocapture: {
        event_allowlist: ["click"], // DOM events from this list ['click', 'change', 'submit']
        element_allowlist: ["button"], // DOM elements from this list ['a', 'button', 'form', 'input', 'select', 'textarea', 'label']
        css_selector_allowlist: ["[ph-autocapture]"], // List of CSS selectors
      },
    });

  setGroupProperties = (...args) => posthog?.group(...args);

  setIdentifierProperties = (...args) => posthog?.identify(...args);

  logCustomEvent = (...args) => posthog?.capture(...args);
}

const posthogInstance = new PostHog();

export default posthogInstance;

I'm calling the init method in my App.js inside a useEffect. While, I'm calling the setGroupProperties inside another module which takes care of message exchange. However, I see this error while the messages are received -

VM3241 Communication.js:128 TypeError: Cannot read properties of undefined (reading 'incr')
    at e6.value (VM3308 chunk-V7BPUASV.js:2889:28)
    at PostHog.setGroupProperties (VM3306 posthog.js:12:46)
    at Object.setGroupProperties (VM3131 index.js:28:28)
    at StompAPI.startNewChatSession (VM3244 api.js:237:23)

Does anyone reckon why is this happening ?

Posthog-JS version - ^1.68.4

ytambi avatar Jun 27 '23 06:06 ytambi

solved by using js snippet , seems that the npm package is not initialized globaly

3xau1o avatar Jul 11 '23 21:07 3xau1o