-
Notifications
You must be signed in to change notification settings - Fork 155
Closed as not planned
Closed as not planned
Copy link
Description
I am using @segment/analytics-next in my Next.js project, and I noticed that the AnalyticsBrowser.load method returns a promise.
However, this behavior is not clearly documented in the official documentation or the example project provided by Segment or Vercel.
Here is the relevant code snippet from my project:
import { AnalyticsBrowser } from "@segment/analytics-next";
export const analytics = AnalyticsBrowser.load({
writeKey: process.env.NEXT_PUBLIC_SEGMENT_KEY
}).catch(err => console.error(err));
export const page = () => {
analytics.page(); // This fails if the promise is not resolved
};
Issue:
- The
AnalyticsBrowser.loadmethod returns a promise, but this is not mentioned in the documentation or the example project. - If the promise is not handled correctly, it can lead to runtime errors because the
analyticsobject is not initialized before calling methods likepage,identify, ortrack. - This behavior is confusing for developers who expect
AnalyticsBrowser.loadto return a fully initializedanalyticsobject synchronously.
Steps to Reproduce:
- Use the
AnalyticsBrowser.loadmethod as shown in the example project or documentation. - Attempt to call
analytics.page()or other methods immediately afterAnalyticsBrowser.load. - Observe that the analytics object is not initialized, leading to runtime errors.
Expected Behavior:
The documentation and example project should clearly state that AnalyticsBrowser.load returns a promise and provide guidance on how to handle it properly.
Actual Behavior:
The documentation and example project do not mention that AnalyticsBrowser.load returns a promise, leading to confusion and potential runtime errors.
Suggested Fix:
- Update the documentation to explicitly mention that AnalyticsBrowser.load returns a promise.
- Update the example project to handle the promise correctly. For example:
import { AnalyticsBrowser } from "@segment/analytics-next";
const analyticsPromise = AnalyticsBrowser.load({
writeKey: process.env.NEXT_PUBLIC_SEGMENT_KEY
});
analyticsPromise.then(([analytics]) => {
analytics.page();
analytics.track("Test Event", { foo: "bar" });
});
- Provide examples of how to handle the promise in real-world scenarios, such as in a Next.js app with server-side rendering or client-side hydration.
Environment:
Library: @segment/analytics-next
Version: 1.78.1
Framework: Next.js 14
Node.js Version: 23.10.0
Metadata
Metadata
Assignees
Labels
No labels