Comments (7)
This is done in #476! Thanks @yordis.
Now that we have a conventional monorepo -- if there are enhancements or specific packages you think are worth adding, please open up separate issues (or PRs)! FYI: now that we've laid the groundwork, we're currently working on breaking out node into a seperate package.
from analytics-next.
Do you need any help? By now I am in auto-pilot doing these grunt work 😄 happy to help
from analytics-next.
Hey @yordis, are you suggesting building a native React implementation ( e.g., a useSegmentAnalytics
hook for example ) that abstracts away the library usage?
from analytics-next.
@pooyaj for the most part, my way to do things is "one function at the time, you make it declarative", that means, one function removed would allow me to be closer to bare-metal way to do things.
So yes, most of the time, they are more declarative abstractions for a given use-case, with given constraints that could tackle the 95% of the use cases, for the rest of 5%, just show people how to go one-level below.
Here is my working setup,
import * as React from 'react';
import { Analytics, AnalyticsBrowser, Context as TContext } from '@segment/analytics-next';
import { useQuery } from 'react-query';
import { AnalyticsSettings, InitOptions } from '@segment/analytics-next/src/analytics';
import { UseQueryOptions } from 'react-query/types/react/types';
type OmittedQueryOption =
| 'queryKey'
| 'select'
| 'queryFn'
| 'refetchOnMount'
| 'refetchOnReconnect'
| 'refetchOnWindowFocus';
type UseSegmentAnalyticsProps = {
options?: Omit<
UseQueryOptions<[Analytics, TContext], unknown, { analytics: Analytics; context: TContext }>,
OmittedQueryOption
>;
settings: AnalyticsSettings;
initOptions?: InitOptions;
};
function useSegmentAnalytics(props: UseSegmentAnalyticsProps) {
const query = useQuery<[Analytics, TContext], unknown, { analytics: Analytics; context: TContext }>({
...(props.options ?? {}),
queryKey: ['@segment/analytics-next/analytics-browser/load', props.settings.writeKey],
refetchOnMount: false,
refetchOnReconnect: false,
refetchOnWindowFocus: false,
select(data) {
return {
analytics: data[0],
context: data[1],
};
},
queryFn() {
// @ts-ignore
return AnalyticsBrowser.load(props.settings, props.initOptions ?? {});
},
});
return {
isReady: query.isSuccess,
analytics: query.data?.analytics,
context: query.data?.context,
};
}
const Context = React.createContext<ReturnType<typeof useSegmentAnalytics>>(undefined);
export function useSegmentAnalyticsContext() {
return React.useContext(Context);
}
type SegmentAnalyticsV2Props = React.PropsWithChildren<UseSegmentAnalyticsProps>;
export function SegmentAnalytics(props: SegmentAnalyticsV2Props) {
const query = useSegmentAnalytics(props);
return <Context.Provider value={query}>{props.children}</Context.Provider>;
}
Obviously ignore the React Query stuff, since we have it in our system, no point in dealing with isLoading
, and isError
and things like that, since React Query works with any Promise.
You would swap that with something basic, I can make it to work. Hopefully, you get the idea.
from analytics-next.
Also notice I had to do the following import:
import { AnalyticsSettings, InitOptions } from '@segment/analytics-next/src/analytics';
I was thinking eventually ones it is in prod, some ticket to expose that from @segment/analytics-next
instead.
from analytics-next.
Alright, dont do what I did around React Query: #310 (comment)
If by any chance you need to reset the cache of React Query (for example when using logout), you will be asking for trouble.
from analytics-next.
@yordis This is on the roadmap for this quarter.
from analytics-next.
Related Issues (20)
- In a sandboxed environment, addEventListener may not behave as expected
- Protocol support for batching?
- Mocking with Jest or Vitest is VERY hard HOT 1
- [Feature Request] Add ability to listen to identity changes HOT 3
- Delete
- ReferenceError: getGlobalCDNUrl function window is not defined HOT 3
- DestinationMiddlewareFunction type is missing from export HOT 1
- Feature request to expose fetch mechanism? HOT 3
- Track method missing events in source debugger HOT 2
- Missing return types and promises not awaited HOT 5
- Provide a way to lazy load AnalyticsBrowser without pulling in all the other dependencies HOT 4
- SSR broke in v1.58.0 HOT 6
- Segment broke with next js 13.5.2 HOT 3
- Segment is not loaded when OneTrust consent integration is setup and jurisdiction does not require a banner to be shown. HOT 4
- OneTrust integration - support a required group for loading a.js HOT 3
- Destination filters only applying *sometimes*? HOT 2
- Delete
- Metrics Calls When using a Proxy HOT 1
- support flush on demand for node.js client HOT 2
- Update vulnerable axios version in `@segment/analytics-node` HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from analytics-next.