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
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
Comment #2
grevil commentedComment #4
grevil commentedAlright, code wise this is done. Please review!
I will test it manually as well on Monday.
Comment #5
anybodyTests fail
Comment #6
anybodyReview done! Nice work, some adjustments, especially in naming and hook position.
Comment #7
anybodySlightly related for the naming part: #3578775: Add optional "Posthog UI Host" and rename "Posthog Host" to "Posthog API Host"
Comment #8
grevil commentedAlright, 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 setComment #9
grevil commentedComment #10
grevil commentedOk, 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:
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):
(https://posthog.com/docs/getting-started/identify-users)
Time to fix the tests now and we should be good to go.
Comment #11
grevil commentedNice all green, let's get this merged!
Please do a final review! @anybody :)
Comment #12
anybodyThanks @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.
Comment #13
grevil commentedOk, 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!
Comment #15
grevil commented