Problem/Motivation

Once the posthog js is attached and initiated, we get the following error in the console:
[PostHog.js] Unique user id has not been set in posthog.identify

If the setting "identify anonymous users" is checked, we always identify the anonymous user, but we don't pass a distinct_id. At first, this seems to be allowed, as said in the docs:

However, in the frontend of a web or mobile app, a distinct_id is not a required argument — PostHog's SDKs will generate an anonymous distinct_id for you automatically and you can capture events anonymously, provided you use the appropriate configuration.

(https://posthog.com/docs/getting-started/identify-users)

BUT if you read the last bit carefully:

provided you use the appropriate configuration.

Which links to https://posthog.com/docs/libraries/js/usage#capturing-anonymous-events where the "person_profiles: 'always'" setting is described. And if we scroll further down, we can read the following:

Calling identify creates a person profile if one doesn't exist already. This means all events for that distinct ID count as identified events.

Since this doesn't happen for anonymous users, I'd say, that calling "identify" without a "distinct_id" will always throw an error as long as person_profiles: 'always' is not set.

Steps to reproduce

Proposed resolution

Adjust the identify call to also set person_profiles: 'always' and create an update hook to disable the identifying anonymous user setting, as this change will result in higher costs (even if these costs were documented on the checkbox, although they were not applied).

Remaining tasks

User interface changes

API changes

Data model changes

Issue fork posthog-3585392

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

grevil created an issue. See original summary.

grevil’s picture

Title: Fix js error "Unique user id has not been set in posthog.identify" » Fix js error "Unique user id has not been set in posthog.identify" (overhaul "identify_anonymous")

grevil’s picture

Assigned: Unassigned » anybody
Status: Active » Needs review

Alright, code wise this is done. Please review!

I will test it manually as well on Monday.

anybody’s picture

Assigned: anybody » grevil
Status: Needs review » Needs work

Tests fail

anybody’s picture

Review done! Nice work, some adjustments, especially in naming and hook position.

anybody’s picture

grevil’s picture

Title: Fix js error "Unique user id has not been set in posthog.identify" (overhaul "identify_anonymous") » Set specific Drupal roles (including anonymous) for which person profiles should be created for
Status: Needs work » Needs review

Alright, I had to adjust the Issue Summary Label.

Everything works as expected. E.g., when I only allow person profiles for "content editor" roles and create a new user with the "content editor" role the expected flow happens:
- I go to the site => PostHog logs, that an anonymous user is on the site.
- I login => PostHog creates a profile for the logged in user and connects it with the old anonymous account

I also created a user with the "Test" role. That user is not identified, even when logged in (as expected).

But for some reason, I still get the following error message:
~~~
logger.ts:50 [PostHog.js] Unique user id has not been set in posthog.identify
critical @ logger.ts:50
_validateIdentifyId @ posthog-core.ts:2310
identify @ posthog-core.ts:2380
(anonymous) @ index.ts:78
(anonymous) @ posthog-core.ts:1104
eachArray @ index.ts:16
(anonymous) @ posthog-core.ts:1096
_execute_array @ posthog-core.ts:1110
(anonymous) @ posthog-core.ts:4034
each @ index.ts:25
(anonymous) @ posthog-core.ts:4024
(anonymous) @ posthog-core.ts:4042
PendingScript
e.__SV.e.init @ cdn.js?tdsmkv:18
attach @ init.js?tdsmkv:42
(anonymous) @ drupal.js?v=11.3.5:166
Drupal.attachBehaviors @ drupal.js?v=11.3.5:162
(anonymous) @ drupal.init.js?v=11.3.5:32
listener @ drupal.init.js?v=11.3.5:20
~~~

Even though everything works as expected. I'll create a support request ticket to posthog, since in their documentation it is documented, that identify can be called without any arguments if "create_person_profile": "always" is set

grevil’s picture

Title: Set specific Drupal roles (including anonymous) for which person profiles should be created for » Set specific Drupal roles (including anonymous) for which person profiles should be created for (Fix js error "Unique user id has not been set in posthog.identify")
grevil’s picture

Ok, this is super weird. Before creating the posthog ticket, I asked the AI if it knows a way to fix the js error being shown and interestingly enough it said the following:

Since person_profiles: 'always' is already set in the init_config (via posthog_js.module) when create_person_profile is true, PostHog automatically creates the person profile for the anonymous user without needing an identify() call.

This was very unexpected, since the documentation clearly states, that identify can be called without a distinct_id. But apparently, if "create_person_profiles" is set to "always" we always create person profiles, even without the "identify" call.

This is definitly true and also makes sense seeing the name of the config, but very unexpected, since the documentation says "identify()" can be called without a distinct id (which isn't true, since it throws an error), but instead we don't need to call "identify" entirely in that case (which is also not properly documented IMO):

This is straightforward to do when capturing backend events, as you associate events to a specific user using a distinct_id, which is a required argument.

However, in the frontend of a web or mobile app, a distinct_id is not a required argument — PostHog's SDKs will generate an anonymous distinct_id for you automatically and you can capture events anonymously, provided you use the appropriate configuration.

(https://posthog.com/docs/getting-started/identify-users)

Time to fix the tests now and we should be good to go.

grevil’s picture

Assigned: grevil » anybody

Nice all green, let's get this merged!

Please do a final review! @anybody :)

anybody’s picture

Assigned: anybody » grevil
Status: Needs review » Needs work
Issue tags: +Needs tests

Thanks @grevil LGTM!

Still I think we should now use the chance to add some tests for the person profiles to work as expected and I guess the JS settings attachments should simplify this.

I think we should add tests for the various cases of person_profiles settings.
AND we should also add a similar tests for the new experimental submodule here (in that submodule) to work as expected and also work as expected in combination with this issue.

If we don't do it NOW, we will probably have hard times understanding all the logics later.

grevil’s picture

Status: Needs work » Needs review
Issue tags: -Needs tests

Ok, I added both tests for posthog_js and commented out tests for posthog_js_conditional_profiles. Which should be refined in the future through a follow up issue, since we currently can't check, whether the person profiles are created or not.

Please review!

  • grevil committed af43b119 on 2.x
    feat: #3585392 Fix js error "Unique user id has not been set in posthog....
grevil’s picture

Assigned: grevil » Unassigned
Status: Needs review » Fixed

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.