From b3180e617d8afb72a7a4e55b070efe6d3e5f4264 Mon Sep 17 00:00:00 2001 From: javayhu Date: Wed, 25 Jun 2025 20:28:09 +0800 Subject: [PATCH] chore: support datafast analytics revenue track --- env.example | 4 ++-- src/actions/create-checkout-session.ts | 14 +++++++++++++- src/config/website.tsx | 1 + src/types/index.d.ts | 1 + 4 files changed, 17 insertions(+), 3 deletions(-) diff --git a/env.example b/env.example index 42165be..6519c16 100644 --- a/env.example +++ b/env.example @@ -119,8 +119,8 @@ NEXT_PUBLIC_SELINE_TOKEN="" # DataFast Analytics (https://datafa.st) # https://mksaas.com/docs/analytics#datafast # ----------------------------------------------------------------------------- -NEXT_PUBLIC_DATAFAST_ANALYTICS_ID="" -NEXT_PUBLIC_DATAFAST_ANALYTICS_DOMAIN="" +NEXT_PUBLIC_DATAFAST_WEBSITE_ID="" +NEXT_PUBLIC_DATAFAST_DOMAIN="" # ----------------------------------------------------------------------------- diff --git a/src/actions/create-checkout-session.ts b/src/actions/create-checkout-session.ts index 7a52ae6..f5b6afb 100644 --- a/src/actions/create-checkout-session.ts +++ b/src/actions/create-checkout-session.ts @@ -1,5 +1,6 @@ 'use server'; +import { websiteConfig } from '@/config/website'; import { findPlanByPlanId } from '@/lib/price-plan'; import { getSession } from '@/lib/server'; import { getUrlWithLocale } from '@/lib/urls/urls'; @@ -8,6 +9,7 @@ import type { CreateCheckoutParams } from '@/payment/types'; import { Routes } from '@/routes'; import { getLocale } from 'next-intl/server'; import { createSafeActionClient } from 'next-safe-action'; +import { cookies } from 'next/headers'; import { z } from 'zod'; // Create a safe action client @@ -67,12 +69,22 @@ export const createCheckoutAction = actionClient } // Add user id to metadata, so we can get it in the webhook event - const customMetadata = { + const customMetadata: Record = { ...metadata, userId: session.user.id, userName: session.user.name, }; + // https://datafa.st/docs/stripe-checkout-api + // if datafast analytics is enabled, add the revenue attribution to the metadata + if (websiteConfig.features.enableDatafastRevenueTrack) { + const cookieStore = await cookies(); + customMetadata.datafast_visitor_id = + cookieStore.get('datafast_visitor_id')?.value ?? ''; + customMetadata.datafast_session_id = + cookieStore.get('datafast_session_id')?.value ?? ''; + } + // Create the checkout session with localized URLs const successUrl = getUrlWithLocale( '/settings/billing?session_id={CHECKOUT_SESSION_ID}', diff --git a/src/config/website.tsx b/src/config/website.tsx index cb07491..d5eed4d 100644 --- a/src/config/website.tsx +++ b/src/config/website.tsx @@ -37,6 +37,7 @@ export const websiteConfig: WebsiteConfig = { enableUpgradeCard: true, enableAffonsoAffiliate: false, enablePromotekitAffiliate: false, + enableDatafastRevenueTrack: false, }, routes: { defaultLoginRedirect: '/dashboard', diff --git a/src/types/index.d.ts b/src/types/index.d.ts index 373a463..2658d83 100644 --- a/src/types/index.d.ts +++ b/src/types/index.d.ts @@ -70,6 +70,7 @@ export interface FeaturesConfig { enableUpgradeCard?: boolean; // Whether to enable the upgrade card in the sidebar enableAffonsoAffiliate?: boolean; // Whether to enable affonso affiliate enablePromotekitAffiliate?: boolean; // Whether to enable promotekit affiliate + enableDatafastRevenueTrack?: boolean; // Whether to enable datafast revenue tracking } /**