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

Fixed analytics #834

Merged
merged 7 commits into from
Feb 22, 2023
Merged

Fixed analytics #834

merged 7 commits into from
Feb 22, 2023

Conversation

davidthor
Copy link
Member

Overview

We had some issues attributing events to individual sessions making it hard to measure performance of our longer running commands.

Changes

Added tracking event to capture command start time, and updated capture method to use anonymous id.

src/app-config/posthog.ts Show resolved Hide resolved
src/app-config/posthog.ts Show resolved Hide resolved
}

capture(message: { event: string; properties?: Record<string | number, any>; }): void {
return super.capture({ distinctId: this.getPersistedProperty('anonymous_id'), ...message });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Setting the distinctId to the anon will create a new user each time and won't allow for aliasing multiple times

Copy link
Member

@tjhiggins tjhiggins Feb 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we might just need to override this method and use anon instead of distinct when necessary:
https://github.com/PostHog/posthog-js-lite/blob/cb050098714676b00ddbdae3a5a4e1bdf1feec11/posthog-core/src/index.ts#L145

Love that they completely rewrote in a minor version

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would work for a single user's machine, but will create spam in recurring CI pipelines which is why I had to override in the first place.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would work for a single user's machine, but will create spam in recurring CI pipelines which is why I had to override in the first place.

Totally forgot about this use case, but should we instead just force disable for CI? This flow works correctly for individual developer usage whereas the other one didn't.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd be down to force disable for CI. Prob not the analytics you're looking for. We have the is-ci pkg

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd be down to force disable for CI. Prob not the analytics you're looking for. We have the is-ci pkg

Added now

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure why posthog-node doesn't inherit from this class, but this is what the web and react packages extend so there's easy getDistinctId() methods which falls back to the anon ID if there's nothing else there:

https://github.com/PostHog/posthog-js-lite/blob/master/posthog-core/src/index.ts#L606

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The docs and marketing sites don't use distinct id at all right? So we shouldn't be using it here when its not the distinct id. What do you think of my proposal for captureStateless?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah so we def want to use the anon id. It seems like an easy change and then we don't need to worry about disabling CI. Although up to you if you still want to disable. We could add a tag for isCI so we could filter out that usage.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mueschm looping you in here per @tjhiggins request. He has to take off and said that you can take over reviewing and merging as you see fit.

We spoke offline as well and concluded that the implementation in this PR seems correct. See this docs article which took us a while to find that confirms how the aliasing works:
https://posthog.com/docs/integrate/user-properties#how-are-properties-managed-when-merging-or-aliasing-users

@@ -47,7 +47,7 @@
"opener": "^1.5.2",
"p-limit": "^3.1.0",
"patch-package": "^6.4.7",
"posthog-node": "^2.4.0",
"posthog-node": "^2.5.1",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This pkg requires node 15+which is kind of weird. I guess we have the ci tests for node 14.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's odd. The tests seem to have passed on node 14 and npm didn't alert me of the issue. How did you find out about the requirement?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was looking at the package-lock.json trying to figure out why my previous patch didn't work. Thought maybe it was using 2.5+ in the package-lock which would explain why the override wasn't working anymore.

@@ -18,24 +18,35 @@ interface AuthResult {
id_token?: string;
}

export type AuthClientOptions = {
checkLogin: () => Promise<User>;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So with the last minute realization on Friday. Do we need the checkLogin callbacks? Your initial logic which called alias in the checkLogin seemingly would have been sufficient.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmmm, there's definitely some room for re-working, but we can't just outright get rid of it because we use it to acquire the User object here:

https://github.com/architect-team/architect-cli/blob/bugfix/621/src/app-config/auth.ts#L166

Though there's something weirdly circular about the current implementation of the two callbacks now that you point it out.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tjhiggins updated this. Feel free to review and then let's get it merged in so we capture events correctly.

@tjhiggins tjhiggins merged commit 926902e into rc Feb 22, 2023
@tjhiggins tjhiggins deleted the bugfix/621 branch February 22, 2023 17:07
github-actions bot pushed a commit that referenced this pull request Feb 22, 2023
## [1.33.2-rc.1](v1.33.1...v1.33.2-rc.1) (2023-02-22)

### Bug Fixes

* **analytics:** Fix issue with incorrect distinct id ([#834](#834)) ([926902e](926902e))
github-actions bot pushed a commit that referenced this pull request Feb 23, 2023
## [1.33.2](v1.33.1...v1.33.2) (2023-02-23)

### Bug Fixes

* **analytics:** Fix issue with incorrect distinct id ([#834](#834)) ([926902e](926902e))
* **deploy:** Support deploying multiple components in one pipeline ([#837](#837)) ([0457eed](0457eed))
@github-actions
Copy link

🎉 This PR is included in version 1.33.2 🎉

The release is available on:

Your semantic-release bot 📦🚀

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

Successfully merging this pull request may close these issues.

None yet

2 participants