posthog icon indicating copy to clipboard operation
posthog copied to clipboard

setPersonProperties is undefined on PostHog Web

Open claydiffrient opened this issue 10 months ago • 3 comments

Bug Description

Bug description

The documentation https://posthog.com/docs/libraries/js#setting-user-properties indicates that there is a method setPersonProperties that can be called.

However, that doesn't appear to be a valid method on the SDK

How to reproduce

  1. Install the posthog web sdk onto a page via script tags
  2. Attempt to call setPersonProperties({ something: 'something else' })
  3. Get an Uncaught TypeError: posthog.setPersonProperties is not a function error.

Additional context

Notice, it's not an available method on the object:

Screenshot 2024-04-17 at 10 10 44 AM

Debug info

Session: https://us.posthog.com/project/sTMFPsFhdP1Ssg/replay/018eecc5-1d7a-73bf-b05b-49968a70619d?t=918 (at https://us.posthog.com/project/52237/feature_flags#panel=support%3Asupport%3A%3A)
Admin: http://go/adminOrgUS/018d6a88-03f6-0000-19e4-b1272fd2c461 (Project: 52237)
Sentry: http://go/sentryUS/52237

claydiffrient avatar Apr 17 '24 16:04 claydiffrient

Hey @claydiffrient, I wasn't able to replicate the issue. From the screenshot, it looks like PostHog hasn't been initialized yet. When are you calling setPersonProperties({ something: 'something else' })?

MarconLP avatar Apr 17 '24 18:04 MarconLP

@MarconLP, this is how my code looks:

posthog.init("<REDACTED_KEY>", {
  api_host: "https://app.posthog.com",
  autocapture: false,
  capture_pageview: false,
  advanced_disable_feature_flags_on_first_load: true,
  session_recording: {
    maskTextSelector: ".fs-exclude",
    maskAllInputs: false,
    maskInputOptions: {
      password: true,
    },
  },

  advanced_disable_feature_flags: true,
  bootstrap: {
    distinctID: "<REDACTED_ID>",
    isIdentifiedID: true,
    featureFlags: JSON.parse(
      "{\u0022test-flag\u0022:true,\u0022use-posthog\u0022:true}"
    ),
  },
});

posthog.setPersonProperties({
  property_to_set: "property_value",
});

claydiffrient avatar Apr 17 '24 18:04 claydiffrient

@MarconLP Just to follow up on this,

The install script from https://posthog.com/docs/feature-flags/installation appears as follows:

<script>
    !function(t,e){var o,n,p,r;e.__SV||(window.posthog=e,e._i=[],e.init=function(i,s,a){function g(t,e){var o=e.split(".");2==o.length&&(t=t[o[0]],e=o[1]),t[e]=function(){t.push([e].concat(Array.prototype.slice.call(arguments,0)))}}(p=t.createElement("script")).type="text/javascript",p.async=!0,p.src=s.api_host+"/static/array.js",(r=t.getElementsByTagName("script")[0]).parentNode.insertBefore(p,r);var u=e;for(void 0!==a?u=e[a]=[]:a="posthog",u.people=u.people||[],u.toString=function(t){var e="posthog";return"posthog"!==a&&(e+="."+a),t||(e+=" (stub)"),e},u.people.toString=function(){return u.toString(1)+".people (stub)"},o="capture identify alias people.set people.set_once set_config register register_once unregister opt_out_capturing has_opted_out_capturing opt_in_capturing reset isFeatureEnabled onFeatureFlags getFeatureFlag getFeatureFlagPayload reloadFeatureFlags group updateEarlyAccessFeatureEnrollment getEarlyAccessFeatures getActiveMatchingSurveys getSurveys onSessionId".split(" "),n=0;n<o.length;n++)g(u,o[n]);e._i.push([i,s,a])},e.__SV=1)}(document,window.posthog||[]);
    posthog.init('<ph_project_api_key>', {api_host: '<ph_client_api_host>'})
</script>

I was able to get this to work by adding setPersonProperties to the o string that is defined there.

claydiffrient avatar May 09 '24 20:05 claydiffrient