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

Groups are not working #71

Open
mnapoli opened this issue Mar 11, 2025 · 2 comments
Open

Groups are not working #71

mnapoli opened this issue Mar 11, 2025 · 2 comments

Comments

@mnapoli
Copy link

mnapoli commented Mar 11, 2025

groupIdentify() doesn't work

            PostHog::groupIdentify([
                'groupType' => 'team',
                'groupKey' => $team->id,
                'properties' => [
                    'name' => $team->name,
                ],
            ]);

It pushes events that are attached to a broken user:

Image

Full payload:

{
  "uuid": "019585bf-7ccf-7ecd-b9c6-1a748a7eab86",
  "event": "$groupidentify",
  "properties": {
    "$group_set": {
      "created_at": "2024-07-11",
      "name": "Team name"
    },
    "$lib": "posthog-php",
    "$lib_version": "3.0.3",
    "$group_key": 1,
    "$lib_consumer": "LibCurl",
    "$active_feature_flags": [],
    "$group_type": "team",
    "$geoip_...": "...",
    "$transformations_succeeded": [],
    "$transformations_failed": []
  },
  "timestamp": "2025-03-11T16:07:53+01:00",
  "team_id": ...,
  "distinct_id": "$team_1",
  "elements_chain": "",
  "created_at": "2025-03-11T16:07:55.062000+01:00"
}

The problem lies here:

"distinct_id": "$team_1",

I think this line is wrong:

"distinctId" => "\${$message['groupType']}_{$message['groupKey']}",

$group_type in capture is not working

Because of the problem above, I tried attaching the group to every event instead:

        $properties = [...];

        $properties['$group_type'] = 'team';
        $properties['$group_key'] = $team->id;

        PostHog::capture([
            'distinctId' => $user->id,
            'event' => $event,
            'properties' => $properties,
        ]);

The event is now attached to the user but not the group.

Full payload:

{
  "uuid": "019585d4-62cc-71ec-a70a-bb2dbc06a386",
  "event": "Event name",
  "properties": {
    "$group_type": "team",
    "$lib": "posthog-php",
    "$active_feature_flags": [],
    "$lib_version": "3.0.3",
    "$group_key": 1,
    "$lib_consumer": "LibCurl",
    "$set": {
      "$geoip_...": "...",
    },
    "$set_once": {
      "$initial_geoip_...": "..."
    },
    "$geoip_...": "...",
    "$transformations_succeeded": [],
    "$transformations_failed": []
  },
  "timestamp": "2025-03-11T16:30:43+01:00",
  "team_id": ...,
  "distinct_id": "1",
  "elements_chain": "",
  "created_at": "2025-03-11T16:30:44.506000+01:00"
}

If we only look at user and group-related data:

{
  "properties": {
    "$group_type": "team",
    "$group_key": 1,
  },
  "distinct_id": "1",
}

That looks incorrect. If I compare to an event reported by the web (JavaScript) SDK:

{
  "event": "$pageview",
  "properties": {
    "distinct_id": "1",
    "$user_id": "1",
    "$groups": {
      "team": 1
    },
    "$group_0": 1
  }
}

Properties don't look formatted the same way.

Is the PHP SDK using an obsolete format for the events API?

To be clear:

  • events reported by the web SDK are attached to the group
  • events reported by the PHP SDK are not attached to the group
@benjackwhite
Copy link

Hi, there is a difference between groupIdentify and groups - for server side SDKs we don't "attach" groups to the user as the $groups property has to be sent with every event and hence it is generally a client-sdk thing.

For server side you call $groupidentify to set properties on that group. When capturing events for a given user you would specify the property $groups: { [key]: value }. Those $group_type,$group_key,$group_set properties are unique to the $groupidentify event and are ignored for normal events.

We have a doc explaining this here which hopefully makes more sense https://posthog.com/docs/product-analytics/group-analytics

@mnapoli
Copy link
Author

mnapoli commented Mar 11, 2025

Ah I see, so groupIdentify doesn't set the group for the rest of the session, makes sense. However that creates an invalid "person" in PostHog, is that expected?

As for the fix you suggested for events (not groupIdentify), it works, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants