feature(sdk): add ability to set global properties in nextjs

This commit is contained in:
Carl-Gerhard Lindesvärd
2024-10-01 10:13:02 +02:00
parent fa5ff6e5d4
commit d920f6951c
4 changed files with 26 additions and 6 deletions

View File

@@ -18,6 +18,7 @@ type OpenPanelComponentProps = Omit<OpenPanelOptions, 'filter'> & {
profileId?: string;
cdnUrl?: string;
filter?: string;
globalProperties?: Record<string, unknown>;
};
const stringify = (obj: unknown) => {
@@ -37,6 +38,7 @@ const stringify = (obj: unknown) => {
export function OpenPanelComponent({
profileId,
cdnUrl,
globalProperties,
...options
}: OpenPanelComponentProps) {
const methods: { name: OpenPanelMethodNames; value: unknown }[] = [
@@ -57,6 +59,12 @@ export function OpenPanelComponent({
},
});
}
if (globalProperties) {
methods.push({
name: 'setGlobalProperties',
value: globalProperties,
});
}
return (
<>
<Script src={cdnUrl ?? CDN_URL} async defer />
@@ -88,6 +96,18 @@ export function IdentifyComponent(props: IdentifyComponentProps) {
);
}
export function SetGlobalPropertiesComponent(props: Record<string, unknown>) {
return (
<>
<Script
dangerouslySetInnerHTML={{
__html: `window.op('setGlobalProperties', ${JSON.stringify(props)});`,
}}
/>
</>
);
}
export function useOpenPanel() {
return {
track,