Fix build issues for meta imports and WSL filesystem
This commit is contained in:
@@ -7,7 +7,7 @@ import { rateLimit, getClientIdentifier, RateLimits } from '@/lib/rateLimit';
|
||||
import { getAuthCookieOptions } from '@/lib/cookieConfig';
|
||||
import { signupSchema, validateRequest } from '@/lib/validationSchemas';
|
||||
import { sendWelcomeEmail } from '@/lib/email';
|
||||
import { sendConversionEvent } from '@/lib/meta';
|
||||
import { sendConversionEvent } from '@/lib/metaConversions';
|
||||
import {
|
||||
ATTRIBUTION_COOKIE_NAME,
|
||||
getEmailDomain,
|
||||
|
||||
@@ -3,7 +3,7 @@ import { headers } from 'next/headers';
|
||||
import { stripe } from '@/lib/stripe';
|
||||
import { db } from '@/lib/db';
|
||||
import Stripe from 'stripe';
|
||||
import { sendConversionEvent } from '@/lib/meta';
|
||||
import { sendConversionEvent } from '@/lib/metaConversions';
|
||||
import { scoreUserLifecycle } from '@/lib/revops-server';
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
|
||||
68
src/lib/metaConversions.ts
Normal file
68
src/lib/metaConversions.ts
Normal file
@@ -0,0 +1,68 @@
|
||||
import * as crypto from 'crypto';
|
||||
|
||||
const BASE_URL = 'https://graph.facebook.com/v21.0';
|
||||
const PIXEL_ID = process.env.META_PIXEL_ID;
|
||||
const ACCESS_TOKEN = process.env.META_ACCESS_TOKEN;
|
||||
|
||||
function hashValue(value: string): string {
|
||||
return crypto.createHash('sha256').update(value.trim().toLowerCase()).digest('hex');
|
||||
}
|
||||
|
||||
interface UserData {
|
||||
email?: string;
|
||||
ip?: string;
|
||||
userAgent?: string;
|
||||
fbc?: string;
|
||||
fbp?: string;
|
||||
}
|
||||
|
||||
interface ConversionEvent {
|
||||
eventName: 'CompleteRegistration' | 'Purchase' | 'ViewContent' | 'Lead' | 'InitiateCheckout';
|
||||
eventTime?: number;
|
||||
eventSourceUrl?: string;
|
||||
userData: UserData;
|
||||
customData?: Record<string, unknown>;
|
||||
}
|
||||
|
||||
export async function sendConversionEvent(event: ConversionEvent): Promise<void> {
|
||||
if (!PIXEL_ID || !ACCESS_TOKEN) {
|
||||
return;
|
||||
}
|
||||
|
||||
const hashedUserData: Record<string, string> = {};
|
||||
if (event.userData.email) hashedUserData.em = hashValue(event.userData.email);
|
||||
if (event.userData.ip) hashedUserData.client_ip_address = event.userData.ip;
|
||||
if (event.userData.userAgent) hashedUserData.client_user_agent = event.userData.userAgent;
|
||||
if (event.userData.fbc) hashedUserData.fbc = event.userData.fbc;
|
||||
if (event.userData.fbp) hashedUserData.fbp = event.userData.fbp;
|
||||
|
||||
const payload = {
|
||||
data: [
|
||||
{
|
||||
event_name: event.eventName,
|
||||
event_time: event.eventTime ?? Math.floor(Date.now() / 1000),
|
||||
event_source_url: event.eventSourceUrl ?? process.env.NEXT_PUBLIC_APP_URL,
|
||||
action_source: 'website',
|
||||
user_data: hashedUserData,
|
||||
custom_data: event.customData ?? {},
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
try {
|
||||
const res = await fetch(`${BASE_URL}/${PIXEL_ID}/events?access_token=${ACCESS_TOKEN}`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(payload),
|
||||
});
|
||||
|
||||
if (!res.ok) {
|
||||
const err = await res.json();
|
||||
console.error('[Meta CAPI] Error:', err);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('[Meta CAPI] Network error:', error);
|
||||
}
|
||||
}
|
||||
|
||||
export { hashValue };
|
||||
Reference in New Issue
Block a user