PostHog / posthog-js-lite

Reimplementation of posthog-js to be as light and modular as possible.
https://posthog.com/docs/libraries
MIT License
69 stars 36 forks source link

(React Native) posthog.identify types do not match docs #312

Closed CallumHemsley closed 2 days ago

CallumHemsley commented 3 days ago

Bug description

Docs contain the following example:

posthog.identify('distinctID', 
  { // userProperties:
      email: 'user@posthog.com',
      name: 'My Name'
  },
  { // userPropertiesSetOnce:
      date_of_first_log_in: '2024-03-01'
  }
)

However, I can see the posthog types from posthog-react-native for identity is: identify(distinctId?: string, properties?: PostHogEventProperties, options?: PostHogCaptureOptions): void;

I guess aside from updating the docs, how can we achieve the userPropertiesSetOnce in RN, or is it just the types that are broken?

How to reproduce

  1. install latest version following docs e.g npx expo install posthog-react-native expo-file-system expo-application expo-device expo-localization
  2. See in package json you have "posthog-react-native": "^3.3.13",
  3. Call usePostHog with following code below, and right click into types for identify
import { usePostHog } from "posthog-react-native";
import {
  PostHogCaptureOptions,
  PostHogEventProperties,
} from "posthog-react-native/lib/posthog-core/src";

export const useAnalytics = () => {
  const posthog = usePostHog();

  const capture = (
    event: string,
    properties?: {
      [key: string]: unknown;
    },
    options?: PostHogCaptureOptions,
  ) => {
    posthog?.capture(event, properties);
  };

  const identify = (
    distinctId?: string,
    properties?: PostHogEventProperties,
    options?: PostHogCaptureOptions,
  ) => {
    // posthog?.identify(distinctId, properties, options);
    posthog.identify(
      "distinctID",
      {
        // userProperties:
        email: "user@posthog.com",
        name: "My Name",
      },
      {
        // userPropertiesSetOnce:
        date_of_first_log_in: "2024-03-01",
      },
    );
  };

  return { capture, identify };
};

Related sub-libraries

Additional context

Thank you for your bug report – we love squashing them!

marandaneto commented 3 days ago

Hello @CallumHemsley thanks for this issue The identify method does not have support for the userPropertiesSetOnce ($set_once) option, I'm gonna fix that.

You can do this calling capture for now: https://posthog.com/docs/libraries/react-native#set_once

marandaneto commented 3 days ago
    posthog.identify('distinctID', 
    { 
          // userProperties:
          email: 'user@posthog.com',
          name: 'My Name'
    })

    posthog.identify('distinctID', 
    { 
        // userProperties:
        $set: {
          email: 'user@posthog.com',
          name: 'My Name'
        },
        // userPropertiesSetOnce:
        $set_once: {
            date_of_first_log_in: '2024-03-01'
        },
    })

both options should be possible after fixing it.

marandaneto commented 3 days ago

https://github.com/PostHog/posthog-js-lite/pull/313 and https://github.com/PostHog/posthog.com/pull/9939