sdk: update nextjs and web sdk

This commit is contained in:
Carl-Gerhard Lindesvärd
2024-05-31 20:47:36 +02:00
parent 51bb0c969d
commit f75fe498ba
3 changed files with 101 additions and 55 deletions

View File

@@ -50,11 +50,11 @@ export function OpenpanelProvider({
cdnUrl,
...options
}: OpenpanelProviderProps) {
const events: { name: OpenpanelMethods; value: unknown }[] = [
const methods: { name: OpenpanelMethods; value: unknown }[] = [
{ name: 'ctor', value: options },
];
if (profileId) {
events.push({ name: 'setProfileId', value: profileId });
methods.push({ name: 'setProfileId', value: profileId });
}
return (
<>
@@ -62,9 +62,9 @@ export function OpenpanelProvider({
<Script
dangerouslySetInnerHTML={{
__html: `window.op = window.op || function(...args) {(window.op.q = window.op.q || []).push(args)};
${events
.map((event) => {
return `window.op('${event.name}', ${JSON.stringify(event.value)});`;
${methods
.map((method) => {
return `window.op('${method.name}', ${JSON.stringify(method.value)});`;
})
.join('\n')}`,
}}
@@ -89,6 +89,20 @@ export function SetProfileId({ value }: SetProfileIdProps) {
);
}
type SetProfileProps = UpdateProfilePayload;
export function SetProfile(props: SetProfileProps) {
return (
<>
<Script
dangerouslySetInnerHTML={{
__html: `window.op('setProfile', ${JSON.stringify(props)});`,
}}
/>
</>
);
}
export function trackEvent(
name: string,
data?: PostEventPayload['properties']