update docs

This commit is contained in:
Carl-Gerhard Lindesvärd
2024-08-06 23:04:08 +02:00
committed by Carl-Gerhard Lindesvärd
parent 2226cb463d
commit 03cee826ff
23 changed files with 684 additions and 570 deletions

View File

@@ -1,6 +1,4 @@
'use client';
import React, { useEffect } from 'react';
import React from 'react';
import Script from 'next/script';
import type {
@@ -14,7 +12,7 @@ import type {
export * from '@openpanel/web';
const CDN_URL = 'https://openpanel.dev/op.js';
const CDN_URL = 'https://openpanel.dev/op1.js';
type OpenPanelComponentProps = Omit<OpenPanelOptions, 'filter'> & {
profileId?: string;

View File

@@ -67,12 +67,13 @@ export type OpenPanelOptions = {
sdkVersion?: string;
waitForProfile?: boolean;
filter?: (payload: TrackHandlerPayload) => boolean;
disable?: boolean;
};
export class OpenPanel {
api: Api;
profileId?: string;
global?: Record<string, any>;
global?: Record<string, unknown>;
queue: TrackHandlerPayload[] = [];
constructor(public options: OpenPanelOptions) {
@@ -94,6 +95,7 @@ export class OpenPanel {
});
}
// placeholder for future use
init() {
// empty
}
@@ -104,6 +106,10 @@ export class OpenPanel {
}
async send(payload: TrackHandlerPayload) {
if (this.options.disable) {
return Promise.resolve();
}
if (this.options.filter && !this.options.filter(payload)) {
return Promise.resolve();
}
@@ -115,7 +121,7 @@ export class OpenPanel {
return this.api.fetch('/track', payload);
}
setGlobalProperties(properties: Record<string, any>) {
setGlobalProperties(properties: Record<string, unknown>) {
this.global = {
...this.global,
...properties,
@@ -179,18 +185,18 @@ export class OpenPanel {
clear() {
this.profileId = undefined;
// session end?
// should we force a session end here?
}
flush() {
this.queue.forEach((item) => {
this.send({
...item,
// Not user why ts-expect-error is needed here
// Not sure why ts-expect-error is needed here
// @ts-expect-error
payload: {
...item.payload,
profileId: this.profileId,
profileId: item.payload.profileId ?? this.profileId,
},
});
});

View File

@@ -6,7 +6,8 @@ import type {
} from '@openpanel/sdk';
import { OpenPanel as OpenPanelBase } from '@openpanel/sdk';
export * from '@openpanel/sdk';
export type * from '@openpanel/sdk';
export { OpenPanel as OpenPanelBase } from '@openpanel/sdk';
export type OpenPanelOptions = OpenPanelBaseOptions & {
trackOutgoingLinks?: boolean;
@@ -129,20 +130,20 @@ export class OpenPanel extends OpenPanelBase {
const target = event.target as HTMLElement;
const btn = target.closest('button');
const anchor = target.closest('a');
const element = btn?.getAttribute('data-event')
const element = btn?.getAttribute('data-track')
? btn
: anchor?.getAttribute('data-event')
: anchor?.getAttribute('data-track')
? anchor
: null;
if (element) {
const properties: Record<string, unknown> = {};
for (const attr of element.attributes) {
if (attr.name.startsWith('data-') && attr.name !== 'data-event') {
if (attr.name.startsWith('data-') && attr.name !== 'data-track') {
properties[toCamelCase(attr.name.replace(/^data-/, ''))] =
attr.value;
}
}
const name = element.getAttribute('data-event');
const name = element.getAttribute('data-track');
if (name) {
super.track(name, properties);
}

View File

@@ -18,7 +18,7 @@ import { OpenPanel } from './index';
// @ts-expect-error
fn(...args);
} else {
console.warn(`op.js: ${t} is not a function`);
console.warn(`OpenPanel: ${t} is not a function`);
}
};