Skip to content

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

Refactor link handling in various components to use AppLink

Bill Yang3d530979c1e8
diff --git a/docs/src/app/(home)/pricing/components/ComparisonSection.tsx b/docs/src/app/(home)/pricing/components/ComparisonSection.tsx
index 252af7f25..2d160c99f 100644
--- a/docs/src/app/(home)/pricing/components/ComparisonSection.tsx
+++ b/docs/src/app/(home)/pricing/components/ComparisonSection.tsx
@@ -1,7 +1,8 @@
"use client";
-import { Check, Minus, X } from "lucide-react";
+import { Check, Minus } from "lucide-react";
import { cn } from "@/lib/utils";
+import { AppLink } from "@/components/AppLink";
import { DEFAULT_EVENT_LIMIT, FREE_SITE_LIMIT, STANDARD_SITE_LIMIT, STANDARD_TEAM_LIMIT } from "../../../../lib/const";
const COMPARISON_FEATURES = [
@@ -275,36 +276,36 @@ export function ComparisonSection({ isAnnual }: { isAnnual: boolean }) {
<div className="font-semibold text-lg text-center mb-3">
Free <span className="text-sm text-neutral-500 dark:text-neutral-400 font-normal block">$0/month</span>
</div>
- <a
+ <AppLink
href="https://app.rybbit.io/signup"
className="inline-flex items-center justify-center px-4 py-2 bg-neutral-300 dark:bg-neutral-700 hover:bg-neutral-400 dark:hover:bg-neutral-600 text-neutral-900 dark:text-white text-sm font-medium rounded-lg border border-neutral-400 dark:border-neutral-600 transform hover:-translate-y-0.5 transition-all duration-200"
>
Start Free
- </a>
+ </AppLink>
</div>
<div className="flex flex-col items-center justify-center px-6 border-r border-neutral-400/50 dark:border-neutral-700/50">
<div className="font-semibold text-lg text-center mb-3">
Standard{" "}
<span className="text-sm text-neutral-500 dark:text-neutral-400 font-normal block">From ${isAnnual ? "16" : "19"} /month</span>
</div>
- <a
+ <AppLink
href="https://app.rybbit.io/signup"
className="inline-flex items-center justify-center px-4 py-2 bg-emerald-600 hover:bg-emerald-500 text-white text-sm font-medium rounded-lg shadow-lg shadow-emerald-900/20 transform hover:-translate-y-0.5 transition-all duration-200"
>
Get started
- </a>
+ </AppLink>
</div>
<div className="flex flex-col items-center justify-center px-6 border-r border-neutral-400/50 dark:border-neutral-700/50">
<div className="font-semibold text-lg text-emerald-600 dark:text-emerald-400 text-center mb-3">
Pro{" "}
<span className="text-sm text-neutral-500 dark:text-neutral-400 font-normal block">From ${isAnnual ? "33" : "39"} /month</span>
</div>
- <a
+ <AppLink
href="https://app.rybbit.io/signup"
className="inline-flex items-center justify-center px-4 py-2 bg-emerald-600 hover:bg-emerald-500 text-white text-sm font-medium rounded-lg shadow-lg shadow-emerald-900/20 transform hover:-translate-y-0.5 transition-all duration-200"
>
Get started
- </a>
+ </AppLink>
</div>
<div className="flex flex-col items-center justify-center px-6">
<div className="font-semibold text-lg text-center mb-3">
diff --git a/docs/src/app/layout.tsx b/docs/src/app/layout.tsx
index 80127c835..ff87f6fc0 100644
--- a/docs/src/app/layout.tsx
+++ b/docs/src/app/layout.tsx
@@ -85,31 +85,6 @@ export default function Layout({ children }: { children: ReactNode }) {
{`(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">
- {`(function(){
- function decorateLinks(referral){
- document.querySelectorAll('a[href*="app.rybbit.io"]').forEach(function(a){
- try{
- var url=new URL(a.href);
- if(!url.searchParams.has('referral')){
- url.searchParams.set('referral',referral);
- a.href=url.toString();
- }
- }catch(e){}
- });
- }
- function start(referral){
- decorateLinks(referral);
- new MutationObserver(function(){decorateLinks(referral);})
- .observe(document.body,{childList:true,subtree:true});
- }
- window.rewardful('ready',function(){
- if(window.Rewardful&&window.Rewardful.referral){
- start(window.Rewardful.referral);
- }
- });
- })();`}
- </Script>
<body className={`flex flex-col min-h-screen ${inter.variable} font-sans`}>
<RootProvider
theme={{
diff --git a/docs/src/components/AppLink.tsx b/docs/src/components/AppLink.tsx
new file mode 100644
index 000000000..43332586a
--- /dev/null
+++ b/docs/src/components/AppLink.tsx
@@ -0,0 +1,30 @@
+"use client";
+
+import { appendReferral } from "@/lib/rewardful";
+
+type AppLinkProps = React.AnchorHTMLAttributes<HTMLAnchorElement>;
+
+export function AppLink({ href, onClick, target, children, ...rest }: AppLinkProps) {
+ return (
+ <a
+ href={href}
+ target={target}
+ {...rest}
+ onClick={(e) => {
+ onClick?.(e);
+ if (e.defaultPrevented || !href) return;
+ const decorated = appendReferral(href);
+ if (decorated !== href) {
+ e.preventDefault();
+ if (target === "_blank") {
+ window.open(decorated, "_blank", "noopener,noreferrer");
+ } else {
+ window.location.href = decorated;
+ }
+ }
+ }}
+ >
+ {children}
+ </a>
+ );
+}
diff --git a/docs/src/components/CustomHeader.tsx b/docs/src/components/CustomHeader.tsx
index f6e678fff..c290672c1 100644
--- a/docs/src/components/CustomHeader.tsx
+++ b/docs/src/components/CustomHeader.tsx
@@ -5,6 +5,7 @@ import { Menu, X } from "lucide-react";
import Image from "next/image";
import Link from "next/link";
import { useState } from "react";
+import { AppLink } from "./AppLink";
import { ThemeSwitcher } from "./ThemeSwitcher";
import { BlackFridayBanner } from "./BlackFridayBanner";
import { WelcomeBanner } from "./WelcomeBanner";
@@ -100,14 +101,14 @@ export function CustomHeader() {
<ThemeSwitcher />
{/* Login Button */}
- <a href="https://app.rybbit.io" target="_blank" rel="noopener noreferrer">
+ <AppLink href="https://app.rybbit.io" target="_blank" rel="noopener noreferrer">
<button
onClick={() => trackAdEvent("login", { location: "header" })}
className="bg-neutral-200 dark:bg-neutral-800 hover:bg-neutral-300 dark:hover:bg-neutral-700 text-neutral-900 dark:text-white text-sm font-medium px-3 py-1.5 rounded-md transform hover:-translate-y-0.5 transition-all duration-200 focus:outline-none focus:ring-2 focus:ring-neutral-400 focus:ring-opacity-50"
>
Login
</button>
- </a>
+ </AppLink>
</div>
{/* Mobile menu button */}
@@ -166,7 +167,7 @@ export function CustomHeader() {
</div>
<div className="border-t border-neutral-300 dark:border-neutral-800">
- <a href="https://app.rybbit.io" target="_blank" rel="noopener noreferrer" className="block w-full">
+ <AppLink href="https://app.rybbit.io" target="_blank" rel="noopener noreferrer" className="block w-full">
<button
onClick={() => trackAdEvent("login", { location: "header" })}
data-rybbit-event="login"
@@ -174,7 +175,7 @@ export function CustomHeader() {
>
Login
</button>
- </a>
+ </AppLink>
</div>
</div>
</div>
diff --git a/docs/src/components/PricingCard.tsx b/docs/src/components/PricingCard.tsx
index d4d8be6ed..305cbbf7b 100644
--- a/docs/src/components/PricingCard.tsx
+++ b/docs/src/components/PricingCard.tsx
@@ -1,7 +1,7 @@
import { Check, X, ArrowDown, ArrowUp } from "lucide-react";
import { cn } from "@/lib/utils";
import { trackAdEvent } from "@/lib/trackAdEvent";
-import Link from "next/link";
+import { AppLink } from "./AppLink";
import { useState } from "react";
export type FeatureItem = { feature: string; included?: boolean } | string;
@@ -78,7 +78,7 @@ export function PricingCard({
{customButton ? (
customButton
) : buttonHref ? (
- <Link href={buttonHref} className="w-full block">
+ <AppLink href={buttonHref} className="w-full block">
<button
onClick={() => {
if (eventLocation) {
@@ -98,7 +98,7 @@ export function PricingCard({
>
{buttonText}
</button>
- </Link>
+ </AppLink>
) : (
<button
onClick={onClick}
diff --git a/docs/src/components/TrackedButton.tsx b/docs/src/components/TrackedButton.tsx
index b13a023d5..4aefe3347 100644
--- a/docs/src/components/TrackedButton.tsx
+++ b/docs/src/components/TrackedButton.tsx
@@ -1,6 +1,6 @@
"use client";
-import Link from "next/link";
+import { AppLink } from "./AppLink";
interface TrackedButtonProps {
href: string;
@@ -14,7 +14,7 @@ interface TrackedButtonProps {
export function TrackedButton({ href, eventName, eventProps, className, children, target, rel }: TrackedButtonProps) {
return (
- <Link
+ <AppLink
href={href}
className="w-full sm:w-auto"
data-rybbit-event={eventName}
@@ -23,6 +23,6 @@ export function TrackedButton({ href, eventName, eventProps, className, children
rel={rel}
>
<button className={className}>{children}</button>
- </Link>
+ </AppLink>
);
}
diff --git a/docs/src/lib/rewardful.ts b/docs/src/lib/rewardful.ts
new file mode 100644
index 000000000..b8cdb3ed8
--- /dev/null
+++ b/docs/src/lib/rewardful.ts
@@ -0,0 +1,14 @@
+export function appendReferral(href: string): string {
+ if (!href.includes("app.rybbit.io")) return href;
+ try {
+ const referral = (window as any).Rewardful?.referral;
+ if (!referral) return href;
+ const url = new URL(href);
+ if (!url.searchParams.has("referral")) {
+ url.searchParams.set("referral", referral);
+ }
+ return url.toString();
+ } catch {
+ return href;
+ }
+}