Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.
feat: add event tracking in jitsu
Marc Seitzc4433639c514
diff --git a/lib/analytics/index.ts b/lib/analytics/index.tsnew file mode 100644index 00000000..865b2439--- /dev/null+++ b/lib/analytics/index.ts@@ -0,0 +1,75 @@+import { emptyAnalytics, jitsuAnalytics } from "@jitsu/js";+import { AnalyticsEvents } from "../types";+import prisma from "@/lib/prisma";++export const analytics =+ process.env.JITSU_HOST && process.env.JITSU_WRITE_KEY+ ? jitsuAnalytics({+ host: process.env.JITSU_HOST,+ writeKey: process.env.JITSU_WRITE_KEY,+ })+ : emptyAnalytics;++const fetchUserWithCounts = async (userId: string) => {+ return await prisma.user.findUnique({+ where: { id: userId },+ select: {+ id: true,+ email: true,+ _count: {+ select: {+ documents: true,+ domains: true,+ },+ },+ documents: {+ select: {+ _count: {+ select: {+ links: true,+ views: true,+ },+ },+ }+ }+ },+ });+};++const getUserProperties = (user: any) => {+ console.log("user", user)+ console.log("user.documents", user.documents)++ let linkCount = user.documents.reduce(+ (acc: any, document: { _count: { links: number; views: number } }) =>+ acc + document._count.links,+ 0+ );++ let viewCount = user.documents.reduce(+ (acc: any, document: { _count: { links: number; views: number } }) =>+ acc + document._count.views,+ 0+ );++ const userData = {+ userId: user.id,+ email: user.email,+ documentCount: user._count.documents,+ domainCount: user._count.domains,+ linkCount: linkCount,+ viewCount: viewCount,+ };++ console.log("userData", userData)++ return userData;+};++export const identifyUser = async (userId: string) => {+ const user = await fetchUserWithCounts(userId);+ const userProperties = getUserProperties(user);+ analytics.identify(userId, userProperties);+};++export const trackAnalytics = (args: AnalyticsEvents) => analytics.track(args);diff --git a/lib/types.ts b/lib/types.tsindex 718a1005..2b77b108 100644--- a/lib/types.ts+++ b/lib/types.ts@@ -100,3 +100,43 @@ export interface DomainVerificationResponse {reason: string;}[];}++export type AnalyticsEvents =+ | {+ event: "User Signed Up";+ userId: string;+ email: string | null | undefined;+ }+ | {+ event: "Document Added";+ documentId: string;+ name: string;+ fileSize: string | null | undefined;+ path: string;+ }+ | {+ event: "Link Added";+ linkId: string;+ documentId: string;+ customDomain: string | null | undefined;+ }+ | { event: "User Upgraded"; email: string | null | undefined; }+ | {+ event: "User Signed In";+ email: string | null | undefined;+ }+ | {+ event: "Link Viewed";+ documentId: string;+ linkId: string;+ viewerId: string;+ viewerEmail: string | null | undefined;+ }+ | {+ event: "Domain Added";+ slug: string;+ }+ | {+ event: "Domain Verified";+ slug: string;+ };diff --git a/package-lock.json b/package-lock.jsonindex 94d6594d..93d3bed9 100644--- a/package-lock.json+++ b/package-lock.json@@ -12,6 +12,7 @@"@chronark/zod-bird": "^0.2.2","@headlessui/react": "^1.7.17","@heroicons/react": "^2.0.18",+ "@jitsu/js": "^1.3.0","@next-auth/prisma-adapter": "^1.0.7","@prisma/client": "^4.16.2","@radix-ui/react-avatar": "^1.0.4",@@ -98,6 +99,71 @@"url": "https://github.com/sponsors/sindresorhus"}},+ "node_modules/@analytics/cookie-utils": {+ "version": "0.2.12",+ "resolved": "https://registry.npmjs.org/@analytics/cookie-utils/-/cookie-utils-0.2.12.tgz",+ "integrity": "sha512-2h/yuIu3kmu+ZJlKmlT6GoRvUEY2k1BbQBezEv5kGhnn9KpmzPz715Y3GmM2i+m7Y0QmBdVUoA260dQZkofs2A==",+ "dependencies": {+ "@analytics/global-storage-utils": "^0.1.7"+ }+ },+ "node_modules/@analytics/core": {+ "version": "0.12.7",+ "resolved": "https://registry.npmjs.org/@analytics/core/-/core-0.12.7.tgz",+ "integrity": "sha512-etmIPCoxWLoUZ/o1o2zvIk4cdVHa8I1xUQtTuLA+YXQ4SsFbm75ZoMXJBqWrNSENpqCJgoL6hizl5uTbkNN+1Q==",+ "funding": [+ {+ "type": "GitHub Sponsors",+ "url": "https://github.com/sponsors/davidwells"+ }+ ],+ "dependencies": {+ "@analytics/global-storage-utils": "^0.1.7",+ "@analytics/type-utils": "^0.6.2",+ "analytics-utils": "^1.0.12"+ }+ },+ "node_modules/@analytics/global-storage-utils": {+ "version": "0.1.7",+ "resolved": "https://registry.npmjs.org/@analytics/global-storage-utils/-/global-storage-utils-0.1.7.tgz",+ "integrity": "sha512-V+spzGLZYm4biZT4uefaylm80SrLXf8WOTv9hCgA46cLcyxx3LD4GCpssp1lj+RcWLl/uXJQBRO4Mnn/o1x6Gw==",+ "dependencies": {+ "@analytics/type-utils": "^0.6.2"+ }+ },+ "node_modules/@analytics/localstorage-utils": {+ "version": "0.1.10",+ "resolved": "https://registry.npmjs.org/@analytics/localstorage-utils/-/localstorage-utils-0.1.10.tgz",+ "integrity": "sha512-uJS+Jp1yLG5VFCgA5T82ZODYBS0xuDQx0NtAZrgbqt9j51BX3TcgmOez5LVkrUNu/lpbxjCLq35I4TKj78VmOQ==",+ "dependencies": {+ "@analytics/global-storage-utils": "^0.1.7"+ }+ },+ "node_modules/@analytics/session-storage-utils": {+ "version": "0.0.7",+ "resolved": "https://registry.npmjs.org/@analytics/session-storage-utils/-/session-storage-utils-0.0.7.tgz",+ "integrity": "sha512-PSv40UxG96HVcjY15e3zOqU2n8IqXnH8XvTkg1X43uXNTKVSebiI2kUjA3Q7ESFbw5DPwcLbJhV7GforpuBLDw==",+ "dependencies": {+ "@analytics/global-storage-utils": "^0.1.7"+ }+ },+ "node_modules/@analytics/storage-utils": {+ "version": "0.4.2",+ "resolved": "https://registry.npmjs.org/@analytics/storage-utils/-/storage-utils-0.4.2.tgz",+ "integrity": "sha512-AXObwyVQw9h2uJh1t2hUgabtVxzYpW+7uKVbdHQK80vr3Td5rrmCxrCxarh7HUuAgSDZ0bZWqmYxVgmwKceaLg==",+ "dependencies": {+ "@analytics/cookie-utils": "^0.2.12",+ "@analytics/global-storage-utils": "^0.1.7",+ "@analytics/localstorage-utils": "^0.1.10",+ "@analytics/session-storage-utils": "^0.0.7",+ "@analytics/type-utils": "^0.6.2"+ }+ },+ "node_modules/@analytics/type-utils": {+ "version": "0.6.2",+ "resolved": "https://registry.npmjs.org/@analytics/type-utils/-/type-utils-0.6.2.tgz",+ "integrity": "sha512-TD+xbmsBLyYy/IxFimW/YL/9L2IEnM7/EoV9Aeh56U64Ify8o27HJcKjo38XY9Tcn0uOq1AX3thkKgvtWvwFQg=="+ },"node_modules/@babel/code-frame": {"version": "7.22.5","resolved": "https://registry.npmjs.org/@babel/code-frame/-/code-frame-7.22.5.tgz",@@ -779,6 +845,14 @@"node": "^14.15.0 || ^16.10.0 || >=18.0.0"}},+ "node_modules/@jitsu/js": {+ "version": "1.3.0",+ "resolved": "https://registry.npmjs.org/@jitsu/js/-/js-1.3.0.tgz",+ "integrity": "sha512-7k3dM84xqf6Ivaama8VWwGYOcmGI/WWQTUHmHNXao+M/tneU6L72SCNlUE587vgtaxvoN2oROo4sBGO+sTh4ZQ==",+ "dependencies": {+ "analytics": "^0.8.1"+ }+ },"node_modules/@jridgewell/gen-mapping": {"version": "0.3.3","resolved": "https://registry.npmjs.org/@jridgewell/gen-mapping/-/gen-mapping-0.3.3.tgz",@@ -2832,6 +2906,12 @@"resolved": "https://registry.npmjs.org/@types/d3-timer/-/d3-timer-3.0.0.tgz","integrity": "sha512-HNB/9GHqu7Fo8AQiugyJbv6ZxYz58wef0esl4Mv828w1ZKpAshw/uFWVDUcIB9KKFeFKoxS3cHY07FFgtTRZ1g=="},+ "node_modules/@types/dlv": {+ "version": "1.1.2",+ "resolved": "https://registry.npmjs.org/@types/dlv/-/dlv-1.1.2.tgz",+ "integrity": "sha512-OyiZ3jEKu7RtGO1yp9oOdK0cTwZ/10oE9PDJ6fyN3r9T5wkyOcvr6awdugjYdqF6KVO5eUvt7jx7rk2Eylufow==",+ "peer": true+ },"node_modules/@types/istanbul-lib-coverage": {"version": "2.0.4","resolved": "https://registry.npmjs.org/@types/istanbul-lib-coverage/-/istanbul-lib-coverage-2.0.4.tgz",@@ -3161,6 +3241,33 @@"url": "https://github.com/sponsors/epoberezkin"}},+ "node_modules/analytics": {+ "version": "0.8.9",+ "resolved": "https://registry.npmjs.org/analytics/-/analytics-0.8.9.tgz",+ "integrity": "sha512-oTbUzQpncMTslakqfK70GgB6bopk5hY+uuekwnadMkDyqNLgcD02KRzteTnO7q5Ko6wDECVtT8xi/6OuAMZykA==",+ "funding": [+ {+ "type": "GitHub Sponsors",+ "url": "https://github.com/sponsors/davidwells"+ }+ ],+ "dependencies": {+ "@analytics/core": "^0.12.7",+ "@analytics/storage-utils": "^0.4.2"+ }+ },+ "node_modules/analytics-utils": {+ "version": "1.0.12",+ "resolved": "https://registry.npmjs.org/analytics-utils/-/analytics-utils-1.0.12.tgz",+ "integrity": "sha512-WvV2YWgsnXLxaY0QYux0crpBAg/0JA763NmbMVz22jKhMPo7dpTBet8G2IlF7ixTjLDzGlkHk1ZaKqqQmjJ+4w==",+ "dependencies": {+ "@analytics/type-utils": "^0.6.2",+ "dlv": "^1.1.3"+ },+ "peerDependencies": {+ "@types/dlv": "^1.0.0"+ }+ },"node_modules/ansi-regex": {"version": "5.0.1","resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-5.0.1.tgz",@@ -10740,6 +10847,65 @@"resolved": "https://registry.npmjs.org/@alloc/quick-lru/-/quick-lru-5.2.0.tgz","integrity": "sha512-UrcABB+4bUrFABwbluTIBErXwvbsU/V7TZWfmbgJfbkwiBuziS9gxdODUyuiecfdGQ85jglMW6juS3+z5TsKLw=="},+ "@analytics/cookie-utils": {+ "version": "0.2.12",+ "resolved": "https://registry.npmjs.org/@analytics/cookie-utils/-/cookie-utils-0.2.12.tgz",+ "integrity": "sha512-2h/yuIu3kmu+ZJlKmlT6GoRvUEY2k1BbQBezEv5kGhnn9KpmzPz715Y3GmM2i+m7Y0QmBdVUoA260dQZkofs2A==",+ "requires": {+ "@analytics/global-storage-utils": "^0.1.7"+ }+ },+ "@analytics/core": {+ "version": "0.12.7",+ "resolved": "https://registry.npmjs.org/@analytics/core/-/core-0.12.7.tgz",+ "integrity": "sha512-etmIPCoxWLoUZ/o1o2zvIk4cdVHa8I1xUQtTuLA+YXQ4SsFbm75ZoMXJBqWrNSENpqCJgoL6hizl5uTbkNN+1Q==",+ "requires": {+ "@analytics/global-storage-utils": "^0.1.7",+ "@analytics/type-utils": "^0.6.2",+ "analytics-utils": "^1.0.12"+ }+ },+ "@analytics/global-storage-utils": {+ "version": "0.1.7",+ "resolved": "https://registry.npmjs.org/@analytics/global-storage-utils/-/global-storage-utils-0.1.7.tgz",+ "integrity": "sha512-V+spzGLZYm4biZT4uefaylm80SrLXf8WOTv9hCgA46cLcyxx3LD4GCpssp1lj+RcWLl/uXJQBRO4Mnn/o1x6Gw==",+ "requires": {+ "@analytics/type-utils": "^0.6.2"+ }+ },+ "@analytics/localstorage-utils": {+ "version": "0.1.10",+ "resolved": "https://registry.npmjs.org/@analytics/localstorage-utils/-/localstorage-utils-0.1.10.tgz",+ "integrity": "sha512-uJS+Jp1yLG5VFCgA5T82ZODYBS0xuDQx0NtAZrgbqt9j51BX3TcgmOez5LVkrUNu/lpbxjCLq35I4TKj78VmOQ==",+ "requires": {+ "@analytics/global-storage-utils": "^0.1.7"+ }+ },+ "@analytics/session-storage-utils": {+ "version": "0.0.7",+ "resolved": "https://registry.npmjs.org/@analytics/session-storage-utils/-/session-storage-utils-0.0.7.tgz",+ "integrity": "sha512-PSv40UxG96HVcjY15e3zOqU2n8IqXnH8XvTkg1X43uXNTKVSebiI2kUjA3Q7ESFbw5DPwcLbJhV7GforpuBLDw==",+ "requires": {+ "@analytics/global-storage-utils": "^0.1.7"+ }+ },+ "@analytics/storage-utils": {+ "version": "0.4.2",+ "resolved": "https://registry.npmjs.org/@analytics/storage-utils/-/storage-utils-0.4.2.tgz",+ "integrity": "sha512-AXObwyVQw9h2uJh1t2hUgabtVxzYpW+7uKVbdHQK80vr3Td5rrmCxrCxarh7HUuAgSDZ0bZWqmYxVgmwKceaLg==",+ "requires": {+ "@analytics/cookie-utils": "^0.2.12",+ "@analytics/global-storage-utils": "^0.1.7",+ "@analytics/localstorage-utils": "^0.1.10",+ "@analytics/session-storage-utils": "^0.0.7",+ "@analytics/type-utils": "^0.6.2"+ }+ },+ "@analytics/type-utils": {+ "version": "0.6.2",+ "resolved": "https://registry.npmjs.org/@analytics/type-utils/-/type-utils-0.6.2.tgz",+ "integrity": "sha512-TD+xbmsBLyYy/IxFimW/YL/9L2IEnM7/EoV9Aeh56U64Ify8o27HJcKjo38XY9Tcn0uOq1AX3thkKgvtWvwFQg=="+ },"@babel/code-frame": {"version": "7.22.5","resolved": "https://registry.npmjs.org/@babel/code-frame/-/code-frame-7.22.5.tgz",@@ -11136,6 +11302,14 @@"chalk": "^4.0.0"}},+ "@jitsu/js": {+ "version": "1.3.0",+ "resolved": "https://registry.npmjs.org/@jitsu/js/-/js-1.3.0.tgz",+ "integrity": "sha512-7k3dM84xqf6Ivaama8VWwGYOcmGI/WWQTUHmHNXao+M/tneU6L72SCNlUE587vgtaxvoN2oROo4sBGO+sTh4ZQ==",+ "requires": {+ "analytics": "^0.8.1"+ }+ },"@jridgewell/gen-mapping": {"version": "0.3.3","resolved": "https://registry.npmjs.org/@jridgewell/gen-mapping/-/gen-mapping-0.3.3.tgz",@@ -12432,6 +12606,12 @@"resolved": "https://registry.npmjs.org/@types/d3-timer/-/d3-timer-3.0.0.tgz","integrity": "sha512-HNB/9GHqu7Fo8AQiugyJbv6ZxYz58wef0esl4Mv828w1ZKpAshw/uFWVDUcIB9KKFeFKoxS3cHY07FFgtTRZ1g=="},+ "@types/dlv": {+ "version": "1.1.2",+ "resolved": "https://registry.npmjs.org/@types/dlv/-/dlv-1.1.2.tgz",+ "integrity": "sha512-OyiZ3jEKu7RtGO1yp9oOdK0cTwZ/10oE9PDJ6fyN3r9T5wkyOcvr6awdugjYdqF6KVO5eUvt7jx7rk2Eylufow==",+ "peer": true+ },"@types/istanbul-lib-coverage": {"version": "2.0.4","resolved": "https://registry.npmjs.org/@types/istanbul-lib-coverage/-/istanbul-lib-coverage-2.0.4.tgz",@@ -12687,6 +12867,24 @@"uri-js": "^4.2.2"}},+ "analytics": {+ "version": "0.8.9",+ "resolved": "https://registry.npmjs.org/analytics/-/analytics-0.8.9.tgz",+ "integrity": "sha512-oTbUzQpncMTslakqfK70GgB6bopk5hY+uuekwnadMkDyqNLgcD02KRzteTnO7q5Ko6wDECVtT8xi/6OuAMZykA==",+ "requires": {+ "@analytics/core": "^0.12.7",+ "@analytics/storage-utils": "^0.4.2"+ }+ },+ "analytics-utils": {+ "version": "1.0.12",+ "resolved": "https://registry.npmjs.org/analytics-utils/-/analytics-utils-1.0.12.tgz",+ "integrity": "sha512-WvV2YWgsnXLxaY0QYux0crpBAg/0JA763NmbMVz22jKhMPo7dpTBet8G2IlF7ixTjLDzGlkHk1ZaKqqQmjJ+4w==",+ "requires": {+ "@analytics/type-utils": "^0.6.2",+ "dlv": "^1.1.3"+ }+ },"ansi-regex": {"version": "5.0.1","resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-5.0.1.tgz",diff --git a/package.json b/package.jsonindex 6706b8cf..0211cdca 100644--- a/package.json+++ b/package.json@@ -18,6 +18,7 @@"@chronark/zod-bird": "^0.2.2","@headlessui/react": "^1.7.17","@heroicons/react": "^2.0.18",+ "@jitsu/js": "^1.3.0","@next-auth/prisma-adapter": "^1.0.7","@prisma/client": "^4.16.2","@radix-ui/react-avatar": "^1.0.4",diff --git a/pages/api/auth/[...nextauth].ts b/pages/api/auth/[...nextauth].tsindex b39edba0..3607e808 100644--- a/pages/api/auth/[...nextauth].ts+++ b/pages/api/auth/[...nextauth].ts@@ -4,6 +4,7 @@ import { PrismaAdapter } from "@next-auth/prisma-adapter";import prisma from "@/lib/prisma";import { CreateUserEmailProps, CustomUser } from "@/lib/types";import { sendWelcomeEmail } from "@/lib/emails/send-welcome";+import { analytics, identifyUser, trackAnalytics } from "@/lib/analytics";const VERCEL_DEPLOYMENT = !!process.env.VERCEL_URL;@@ -56,7 +57,23 @@ export const authOptions: NextAuthOptions = {email: message.user.email,},};+ await analytics.identify(message.user.id, {+ userId: message.user.id,+ email: message.user.email,+ });+ await trackAnalytics({+ event: "User Signed Up",+ email: message.user.email,+ userId: message.user.id,+ });await sendWelcomeEmail(params);+ },+ async signIn(message) {+ await identifyUser(message.user.id);+ await trackAnalytics({+ event: "User Signed In",+ email: message.user.email,+ });}},};diff --git a/pages/api/documents/index.ts b/pages/api/documents/index.tsindex 4c3506c0..951ec6a7 100644--- a/pages/api/documents/index.ts+++ b/pages/api/documents/index.ts@@ -4,6 +4,7 @@ import prisma from "@/lib/prisma";import { authOptions } from "../auth/[...nextauth]";import { CustomUser } from "@/lib/types";import { getExtension, log } from "@/lib/utils";+import { identifyUser, trackAnalytics } from "@/lib/analytics";export default async function handle(req: NextApiRequest,@@ -76,6 +77,15 @@ export default async function handle(},});+ await identifyUser((session.user as CustomUser).id);+ await trackAnalytics({+ event: "Document Added",+ documentId: document.id,+ name: document.name,+ fileSize: null,+ path: req.body.path,+ });+res.status(201).json(document);} catch (error) {log(`Failed to create document. Error: \n\n ${error}`)diff --git a/pages/api/domains/[domain]/verify.ts b/pages/api/domains/[domain]/verify.tsindex 61dbacfa..d1812206 100644--- a/pages/api/domains/[domain]/verify.ts+++ b/pages/api/domains/[domain]/verify.ts@@ -4,8 +4,9 @@ import {getDomainResponse,verifyDomain,} from "@/lib/domains";-import { DomainVerificationStatusProps } from "@/lib/types";+import { CustomUser, DomainVerificationStatusProps } from "@/lib/types";import prisma from "@/lib/prisma";+import { analytics, identifyUser, trackAnalytics } from '@/lib/analytics';export default async function handle(req: NextApiRequest, res: NextApiResponse) {// GET /api/domains/[domain]/verify - get domain verification status@@ -57,6 +58,14 @@ export default async function handle(req: NextApiRequest, res: NextApiResponse)lastChecked: new Date(),},});++ // TODO: cannot identify user here because we don't have the session+ // await identifyUser((session.user as CustomUser).id);+ await analytics.identify();+ await trackAnalytics({+ event: "Domain Verified",+ slug: domain,+ });}return res.status(200).json({diff --git a/pages/api/domains/index.ts b/pages/api/domains/index.tsindex 67dd7283..f8ae7614 100644--- a/pages/api/domains/index.ts+++ b/pages/api/domains/index.ts@@ -5,6 +5,7 @@ import { authOptions } from "../auth/[...nextauth]";import { CustomUser } from "@/lib/types";import { log } from "@/lib/utils";import { addDomainToVercel, validDomainRegex } from "@/lib/domains";+import { identifyUser, trackAnalytics } from "@/lib/analytics";export default async function handle(req: NextApiRequest,@@ -54,14 +55,20 @@ export default async function handle(}// console.log("Valid domain", domain);-+const response = await prisma.domain.create({data: {slug: domain,userId: (session.user as CustomUser).id,},});- await addDomainToVercel(domain)+ await addDomainToVercel(domain);++ await identifyUser((session.user as CustomUser).id);+ await trackAnalytics({+ event: "Domain Added",+ slug: domain,+ });res.status(201).json(response);} catch (error) {diff --git a/pages/api/links/index.ts b/pages/api/links/index.tsindex 6e67798c..2c8a902f 100644--- a/pages/api/links/index.ts+++ b/pages/api/links/index.ts@@ -3,6 +3,8 @@ import prisma from "@/lib/prisma";import { getServerSession } from "next-auth/next";import { authOptions } from "../auth/[...nextauth]";import { hashPassword } from "@/lib/utils";+import { identifyUser, trackAnalytics } from "@/lib/analytics";+import { CustomUser } from "@/lib/types";export default async function handler(req: NextApiRequest,@@ -84,6 +86,14 @@ export default async function handler(return res.status(404).json({ error: "Link not found" });}+ await identifyUser((session.user as CustomUser).id);+ await trackAnalytics({+ event: "Link Added",+ linkId: linkWithView.id,+ documentId: linkWithView.documentId,+ customDomain: linkWithView.domainSlug,+ });+return res.status(200).json(linkWithView);}diff --git a/pages/api/stripe/webhook.ts b/pages/api/stripe/webhook.tsindex 6bf90fc0..c596caf1 100644--- a/pages/api/stripe/webhook.ts+++ b/pages/api/stripe/webhook.ts@@ -6,6 +6,7 @@ import { stripe } from "@/lib/stripe";import { getPlanFromPriceId, isNewCustomer } from "@/lib/stripe/utils";import { log } from "@/lib/utils";import { sendUpgradePlanEmail } from "@/lib/emails/send-upgrade-plan";+import { identifyUser, trackAnalytics } from "@/lib/analytics";// Stripe requires the raw body to construct the event.export const config = {@@ -79,8 +80,12 @@ export default async function webhookHandler(const plan = getPlanFromPriceId(priceId);const stripeId = subscriptionUpdated.customer.toString();const subscriptionId = subscriptionUpdated.id;- const startsAt = new Date(subscriptionUpdated.current_period_start * 1000);- const endsAt = new Date(subscriptionUpdated.current_period_end * 1000);+ const startsAt = new Date(+ subscriptionUpdated.current_period_start * 1000+ );+ const endsAt = new Date(+ subscriptionUpdated.current_period_end * 1000+ );// If a project upgrades/downgrades their subscription, update their usage limit in the database.const user = await prisma.user.update({@@ -95,15 +100,25 @@ export default async function webhookHandler(},});if (!user) {- await log("User not found in Stripe webhook `customer.subscription.created` callback");+ await log(+ "User not found in Stripe webhook `customer.subscription.created` callback"+ );return;}// Send thank you email to project owner if they are a new customerif (newCustomer) {- await sendUpgradePlanEmail({user: {email: user.email as string, name: user.name as string}});+ await sendUpgradePlanEmail({+ user: { email: user.email as string, name: user.name as string },+ });}+ await identifyUser(user.id);+ await trackAnalytics({+ event: "User Upgraded",+ email: user.email,+ });+// If project cancels their subscription} else if (event.type === "customer.subscription.deleted") {const subscriptionDeleted = event.data.object as Stripe.Subscription;diff --git a/pages/api/views.ts b/pages/api/views.tsindex 8e93e560..3b7ee527 100644--- a/pages/api/views.ts+++ b/pages/api/views.ts@@ -1,6 +1,8 @@import { NextApiRequest, NextApiResponse } from "next";import prisma from "@/lib/prisma";import { checkPassword, log } from "@/lib/utils";+import { analytics, identifyUser, trackAnalytics } from "@/lib/analytics";+import { CustomUser } from "@/lib/types";export default async function handle(req: NextApiRequest,@@ -72,6 +74,17 @@ export default async function handle(}});+ // TODO: cannot identify user because session is not available+ // await identifyUser((session.user as CustomUser).id);+ await analytics.identify();+ await trackAnalytics({+ event: "Link Viewed",+ linkId: linkId,+ documentId: documentId,+ viewerId: newView.id,+ viewerEmail: email,+ });+res.status(200).json({ message: "View recorded", viewId: newView.id, file: newView.document.file });} catch (error) {log(`Failed to record view for ${linkId}. Error: \n\n ${error}`);