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

analytics.user function should be listed as optionally undefined in typescript definitions #1097

Open
william-will-angi opened this issue Jun 27, 2024 · 2 comments
Labels
enhancement New feature or request

Comments

@william-will-angi
Copy link

william-will-angi commented Jun 27, 2024

We are running into an issue for users that have ad blocking or cross-site tracking disabled.

Our code:

import type { AnalyticsSnippet } from '@segment/analytics-next';

const getAnalytics = (): AnalyticsSnippet | undefined => typeof window !== 'undefined' ? window.analytics : undefined;

getAnalytics()?.user()

Throws the error:

VM70:1 Uncaught TypeError: analytics.user is not a function
    at <anonymous>:1:11

My expectation is that the typescript definition lists the user function as optionally undefined so that typescript enforces that we call the code as:

getAnalytics()?.user?.()

This would prevent us from running into runtime errors for users with ad blockers or cross site trackers disabled.

We are using @segment/[email protected] & [email protected]

@silesky silesky added the enhancement New feature or request label Jul 27, 2024
@silesky silesky closed this as completed Sep 14, 2024
@silesky silesky reopened this Sep 14, 2024
@william-will-angi
Copy link
Author

We are following step 2a of this doc:
https://segment.com/docs/connections/sources/catalog/libraries/website/javascript/quickstart/#step-2a-add-the-segment-snippet

It says to include an inline script which creates a window.analytics object that has a number of different methods on it, but it doesn't include user,

My understanding is that this is meant as a temporary queue so that you can call analytics methods while the request for the segment script is in flight. If a user has an ad blocker enabled though, the request to segment is blocked, meaning window.analytics is defined from the inline script but it will not have the user method available. Which means when we try to call the method user(), we get a run time error.

Now we first check for the existence of user on the analytics object before calling it, but we have to ignore the typescript definition since the compiler thinks it's always defined.

@silesky
Copy link
Contributor

silesky commented Sep 14, 2024

@william-will-angi Apologies, I deleted my comment and reopened the issue that you responded immediately after I posted it, when I double checked and realized that user is not one of the buffered methods on the list -- didn't mean for you to see it (but I assume you got notified). You're correct.

On npm (AnalyticsBrowser), user is always defined and returns a promise, but not in the snippet -- a bit confusing. Unifying the api is something I'm noodling on.

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

No branches or pull requests

2 participants