Skip to content

🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.

Integrate Rewardful referral tracking and enhance subscription flow (#869)

Goldflag3bc24640e8c0
diff --git a/client/src/app/layout.tsx b/client/src/app/layout.tsx
index e6a3770bb..f9d97b0e4 100644
--- a/client/src/app/layout.tsx
+++ b/client/src/app/layout.tsx
@@ -2,6 +2,7 @@
import { useAppEnv } from "@/hooks/useIsProduction";
import { useStopImpersonation } from "@/hooks/useStopImpersonation";
+import { IS_CLOUD } from "@/lib/const";
import QueryProvider from "@/providers/QueryProvider";
import { ThemeProvider } from "next-themes";
import { Inter } from "next/font/google";
@@ -46,6 +47,14 @@ export default function RootLayout({ children }: { children: React.ReactNode })
{appEnv === "demo" && (
<Script src="https://demo.rybbit.com/api/script.js" data-site-id="22" strategy="afterInteractive" />
)}
+ {appEnv === "prod" && IS_CLOUD && (
+ <>
+ <Script id="rewardful-queue" strategy="beforeInteractive">
+ {`(function(w,r){w._rwq=r;w[r]=w[r]||function(){(w[r].q=w[r].q||[]).push(arguments)}})(window,'rewardful');`}
+ </Script>
+ <Script src="https://r.wdfl.co/rw.js" data-rewardful="fc3780" strategy="afterInteractive" />
+ </>
+ )}
</body>
</NuqsAdapter>
</html>
diff --git a/client/src/app/settings/organization/subscription/page.tsx b/client/src/app/settings/organization/subscription/page.tsx
index 067c3a549..61b6efad2 100644
--- a/client/src/app/settings/organization/subscription/page.tsx
+++ b/client/src/app/settings/organization/subscription/page.tsx
@@ -12,6 +12,7 @@ import { FreePlan } from "../../../../components/subscription/FreePlan";
import { OverridePlan } from "../../../../components/subscription/OverridePlan";
import { Building } from "lucide-react";
import { authClient } from "@/lib/auth";
+import { useEffect } from "react";
import { AppSumoPlan } from "../../../../components/subscription/AppSumoPlan";
export default function OrganizationSubscriptionPage() {
@@ -21,6 +22,13 @@ export default function OrganizationSubscriptionPage() {
const { data: activeOrg, isPending } = authClient.useActiveOrganization();
const { data: session } = authClient.useSession();
+ useEffect(() => {
+ const params = new URLSearchParams(window.location.search);
+ if (params.has("session_id") && session?.user?.email) {
+ window.rewardful?.("convert", { email: session.user.email });
+ }
+ }, [session?.user?.email]);
+
// Check if the current user is an owner by looking at the members in the active organization
const currentUserMember = activeOrg?.members?.find(member => member.userId === session?.user?.id);
const isOwner = currentUserMember?.role === "owner";
diff --git a/client/src/app/subscribe/components/PricingCards.tsx b/client/src/app/subscribe/components/PricingCards.tsx
index fb6e1d4de..3d51e2d32 100644
--- a/client/src/app/subscribe/components/PricingCards.tsx
+++ b/client/src/app/subscribe/components/PricingCards.tsx
@@ -78,6 +78,7 @@ export function PricingCards({ isLoggedIn }: { isLoggedIn: boolean }) {
successUrl: successUrl,
cancelUrl: cancelUrl,
organizationId: activeOrg.id,
+ referral: window.Rewardful?.referral || undefined,
}),
});
diff --git a/client/src/components/subscription/PaidPlain/PlanDialog.tsx b/client/src/components/subscription/PaidPlain/PlanDialog.tsx
index c24cfd9ea..c6789d0b2 100644
--- a/client/src/components/subscription/PaidPlain/PlanDialog.tsx
+++ b/client/src/components/subscription/PaidPlain/PlanDialog.tsx
@@ -66,6 +66,7 @@ export function PlanDialog({ open, onOpenChange, currentPlanName, hasActiveSubsc
successUrl,
cancelUrl,
organizationId: activeOrg.id,
+ referral: window.Rewardful?.referral || undefined,
}),
});
diff --git a/client/src/types/rewardful.d.ts b/client/src/types/rewardful.d.ts
new file mode 100644
index 000000000..a81593d94
--- /dev/null
+++ b/client/src/types/rewardful.d.ts
@@ -0,0 +1,4 @@
+interface Window {
+ rewardful?: (...args: any[]) => void;
+ Rewardful?: { referral?: string };
+}
diff --git a/docs/src/app/layout.tsx b/docs/src/app/layout.tsx
index 11e77cf9c..b2df6b1a8 100644
--- a/docs/src/app/layout.tsx
+++ b/docs/src/app/layout.tsx
@@ -81,6 +81,26 @@ export default function Layout({ children }: { children: ReactNode }) {
data-site-id="3b023d1a7895"
data-namespace="rybbit_demo"
/>
+ <Script id="rewardful-queue" strategy="beforeInteractive">
+ {`(function(w,r){w._rwq=r;w[r]=w[r]||function(){(w[r].q=w[r].q||[]).push(arguments)}})(window,'rewardful');`}
+ </Script>
+ <Script src="https://r.wdfl.co/rw.js" data-rewardful="fc3780" strategy="afterInteractive" />
+ <Script id="rewardful-cross-domain" strategy="afterInteractive">
+ {`document.addEventListener('click', function(e) {
+ var link = e.target && e.target.closest ? e.target.closest('a') : null;
+ if (!link || !link.href) return;
+ if (link.href.indexOf('app.rybbit.io') === -1) return;
+ var referral = window.Rewardful && window.Rewardful.referral;
+ if (!referral) return;
+ try {
+ var url = new URL(link.href);
+ if (!url.searchParams.has('referral')) {
+ url.searchParams.set('referral', referral);
+ link.href = url.toString();
+ }
+ } catch(e) {}
+ });`}
+ </Script>
<body className={`flex flex-col min-h-screen ${inter.variable} font-sans`}>
<RootProvider
theme={{
diff --git a/server/src/api/stripe/createCheckoutSession.ts b/server/src/api/stripe/createCheckoutSession.ts
index 35dc11d92..dee6c2367 100644
--- a/server/src/api/stripe/createCheckoutSession.ts
+++ b/server/src/api/stripe/createCheckoutSession.ts
@@ -10,13 +10,14 @@ interface CheckoutRequestBody {
successUrl: string;
cancelUrl: string;
organizationId: string;
+ referral?: string;
}
export async function createCheckoutSession(
request: FastifyRequest<{ Body: CheckoutRequestBody }>,
reply: FastifyReply
) {
- const { priceId, successUrl, cancelUrl, organizationId } = request.body;
+ const { priceId, successUrl, cancelUrl, organizationId, referral } = request.body;
const userId = request.user?.id;
if (!userId) {
@@ -83,6 +84,7 @@ export async function createCheckoutSession(
metadata: {
organizationId: org.id,
createdByUserId: userId, // For audit trail
+ ...(referral && { referral }),
},
});
stripeCustomerId = customer.id;
@@ -104,6 +106,7 @@ export async function createCheckoutSession(
],
success_url: successUrl,
cancel_url: cancelUrl,
+ ...(referral && { client_reference_id: referral }),
// Store organization ID in metadata for webhook processing
metadata: {
organizationId: organizationId,