Skip to content

OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.

add op.js to public site

Carl-Gerhard Lindesvärda92ce6604813
diff --git a/.gitignore b/.gitignore
index a70ee3fd9..3576b400c 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,4 +1,5 @@
# Based on https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore
+apps/public/public/op.js
packages/sdk/profileId.txt
packages/sdk/test.ts
dump.sql
diff --git a/apps/public/Dockerfile b/apps/public/Dockerfile
index 23fbefa2d..af63be710 100644
--- a/apps/public/Dockerfile
+++ b/apps/public/Dockerfile
@@ -69,6 +69,7 @@ COPY --from=prod /app/packages/db/node_modules /app/packages/db/node_modules
# COPY --from=prod /app/packages/queue/node_modules /app/packages/queue/node_modules
RUN pnpm db:codegen
+RUN pnpm js:codegen
WORKDIR /app/apps/public
diff --git a/apps/public/src/app/join-waitlist.tsx b/apps/public/src/app/join-waitlist.tsx
index 5592104a8..162cd5d82 100644
--- a/apps/public/src/app/join-waitlist.tsx
+++ b/apps/public/src/app/join-waitlist.tsx
@@ -1,6 +1,6 @@
'use client';
-import { useState } from 'react';
+import { useEffect, useState } from 'react';
import { Button } from '@/components/ui/button';
import {
Dialog,
@@ -14,6 +14,14 @@ import {
export function JoinWaitlist() {
const [value, setValue] = useState('');
const [open, setOpen] = useState(false);
+
+ useEffect(() => {
+ if (open) {
+ // @ts-ignore
+ window.openpanel.event('waitlist_success');
+ }
+ }, [open]);
+
return (
<>
<Dialog open={open} onOpenChange={setOpen}>
diff --git a/apps/public/src/app/layout.tsx b/apps/public/src/app/layout.tsx
index a6f0af4a5..e2271db55 100644
--- a/apps/public/src/app/layout.tsx
+++ b/apps/public/src/app/layout.tsx
@@ -3,6 +3,7 @@ import { cn } from '@/utils/cn';
import '@/styles/globals.css';
import type { Metadata } from 'next';
+import Script from 'next/script';
import { defaultMeta } from './meta';
@@ -23,6 +24,13 @@ export default async function RootLayout({
<body className={cn('min-h-screen font-sans antialiased grainy')}>
{children}
</body>
+ <Script
+ src="/op.js"
+ data-url="https://api.openpanel.dev"
+ data-client-id="301c6dc1-424c-4bc3-9886-a8beab09b615"
+ data-track-screen-views="1"
+ data-track-outgoing-links="1"
+ />
</html>
);
}
diff --git a/apps/test/src/pages/test.tsx b/apps/test/src/pages/test.tsx
index 300908d55..a177e3ab0 100644
--- a/apps/test/src/pages/test.tsx
+++ b/apps/test/src/pages/test.tsx
@@ -33,7 +33,8 @@ export default function Test() {
</button>
<button
onClick={() => {
- mixan.event('custom_click', {
+ // @ts-ignore
+ window.openpanel.event('custom_click', {
custom_string: 'test',
custom_number: 1,
});
diff --git a/package.json b/package.json
index dcaccd794..c30a419ad 100644
--- a/package.json
+++ b/package.json
@@ -8,6 +8,7 @@
"module": "index.ts",
"scripts": {
"db:codegen": "pnpm -r --filter db run codegen",
+ "js:codegen": "pnpm -r --filter sdk-web run build-for-openpanel",
"migrate": "pnpm -r --filter db run migrate",
"migrate:deploy": "pnpm -r --filter db run migrate:deploy",
"dev": "pnpm -r dev",
diff --git a/packages/sdk-web/index.ts b/packages/sdk-web/index.ts
index 8fcc3f72c..09ade1014 100644
--- a/packages/sdk-web/index.ts
+++ b/packages/sdk-web/index.ts
@@ -81,6 +81,8 @@ export class MixanWeb extends Mixan<MixanWebOptions> {
} else {
window.addEventListener('locationchange', () => this.screenView());
}
+
+ this.screenView();
}
public screenView(properties?: Record<string, unknown>): void {
diff --git a/packages/sdk-web/package.json b/packages/sdk-web/package.json
index da3533504..a453c65d6 100644
--- a/packages/sdk-web/package.json
+++ b/packages/sdk-web/package.json
@@ -4,6 +4,7 @@
"module": "index.ts",
"scripts": {
"build": "rm -rf dist && tsup",
+ "build-for-openpanel": "pnpm build && cp dist/cdn.global.js ../../apps/public/public/op.js",
"lint": "eslint .",
"format": "prettier --check \"**/*.{mjs,ts,md,json}\"",
"typecheck": "tsc --noEmit"
diff --git a/packages/sdk/index.ts b/packages/sdk/index.ts
index 8d9ab0ce2..7e85a2802 100644
--- a/packages/sdk/index.ts
+++ b/packages/sdk/index.ts
@@ -41,11 +41,11 @@ function createApi(_url: string, clientId: string, clientSecret?: string) {
...(options ?? {}),
})
.then(async (res) => {
- if (res.status !== 200) {
+ if (res.status !== 200 && res.status !== 202) {
return retry(attempt, resolve);
}
- const response = await res.json();
+ const response = await res.text();
if (!response) {
return resolve(null);
diff --git a/tooling/typescript/tsup.config.json b/tooling/typescript/tsup.config.json
index 2cff1264b..271f0013d 100644
--- a/tooling/typescript/tsup.config.json
+++ b/tooling/typescript/tsup.config.json
@@ -3,7 +3,7 @@
"format": ["cjs", "esm", "iife"],
"dts": true,
"splitting": false,
- "sourcemap": true,
+ "sourcemap": false,
"clean": true,
"minify": true
}