Compare commits
4 Commits
3561012fb9
...
v0.0.6
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a9a9f5ed30 | ||
|
|
0e1a449cb6 | ||
|
|
764da5db2e | ||
|
|
6692338b83 |
@@ -4,7 +4,7 @@
|
||||
"type": "git",
|
||||
"url": "https://gitea.auvem.com/svelte-toolkit/spectator.git"
|
||||
},
|
||||
"version": "0.0.5",
|
||||
"version": "0.0.6",
|
||||
"license": "MIT",
|
||||
"scripts": {
|
||||
"dev": "vite dev",
|
||||
|
||||
@@ -113,11 +113,11 @@ export class ConversionControl {
|
||||
const event = new ServerEvent()
|
||||
.setEventName(eventName)
|
||||
.setEventTime(Math.floor(Date.now() / 1000))
|
||||
.setEventId(details.eventID)
|
||||
.setUserData(buildUserData(details.userData))
|
||||
.setActionSource(details.actionSource);
|
||||
|
||||
if (details?.eventSourceURL) event.setEventSourceUrl(details.eventSourceURL);
|
||||
if (details.eventID) event.setEventId(details.eventID);
|
||||
if (details.eventSourceURL) event.setEventSourceUrl(details.eventSourceURL);
|
||||
if (params) {
|
||||
const customData = buildCustomData(params);
|
||||
event.setCustomData(customData);
|
||||
|
||||
@@ -8,8 +8,9 @@ import type {
|
||||
} from '$lib/types/conversion.js';
|
||||
|
||||
import { StatusCodes } from 'http-status-codes';
|
||||
import { getFbpFbcFromCookies } from '../metapixel/fbc.ts';
|
||||
|
||||
const getEventIP = (request: Request, getClientAddress: () => string) => {
|
||||
export const getRequestIP = (request: Request, getClientAddress: () => string) => {
|
||||
return (
|
||||
request.headers.get('x-forwarded-for') ||
|
||||
request.headers.get('cf-connecting-ip') ||
|
||||
@@ -26,18 +27,21 @@ const getEventIP = (request: Request, getClientAddress: () => string) => {
|
||||
export const createConversionRequestHandler: (control: ConversionControl) => RequestHandler = (
|
||||
control
|
||||
) => {
|
||||
const handle: RequestHandler = async ({ request, getClientAddress }) => {
|
||||
const handle: RequestHandler = async ({ request, getClientAddress, cookies }) => {
|
||||
try {
|
||||
const body = (await request.json()) as ConversionRequestBody;
|
||||
|
||||
// Build user data with IP and user agent
|
||||
const ip = getEventIP(request, getClientAddress);
|
||||
const ip = getRequestIP(request, getClientAddress);
|
||||
const ua = request.headers.get('user-agent');
|
||||
const { fbp, fbc } = getFbpFbcFromCookies(cookies);
|
||||
|
||||
const userData: ConversionUserData = {
|
||||
...body.user,
|
||||
ip,
|
||||
ua: ua ?? body.user?.ua ?? undefined
|
||||
ua: ua ?? body.user?.ua ?? undefined,
|
||||
fbc: body.user?.fbc ?? fbc,
|
||||
fbp: body.user?.fbp ?? fbp
|
||||
};
|
||||
|
||||
// Build custom data with UTM params if applicable
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import type { Cookies } from '@sveltejs/kit';
|
||||
import log from 'loglevel';
|
||||
|
||||
export type EnsureFbcOptions = {
|
||||
@@ -79,10 +80,19 @@ export function ensureFbc(options: EnsureFbcOptions = {}) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper to read both _fbp and _fbc for your CAPI payload.
|
||||
* Helper to read both _fbp and _fbc for your CAPI payload from browser cookies.
|
||||
*/
|
||||
export function getFbpFbc(): { fbp?: string; fbc?: string } {
|
||||
const fbp = getCookie('_fbp');
|
||||
const fbc = getCookie('_fbc');
|
||||
return { fbp, fbc };
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper to read both _fbp and _fbc for your CAPI payload from cookies object.
|
||||
*/
|
||||
export function getFbpFbcFromCookies(cookies: Cookies): { fbp?: string; fbc?: string } {
|
||||
const fbp = cookies.get('_fbp') || undefined;
|
||||
const fbc = cookies.get('_fbc') || undefined;
|
||||
return { fbp, fbc };
|
||||
}
|
||||
|
||||
@@ -197,14 +197,15 @@ export class PixelControl {
|
||||
|
||||
/**
|
||||
* Sends a PageView event
|
||||
* @param disableCAPI If true, disables sending this event to the Conversion API
|
||||
* @throws Error if the Meta Pixel is not initialized.
|
||||
*/
|
||||
pageView() {
|
||||
pageView(disableCAPI: boolean = false) {
|
||||
if (!this.consentGuard()) return;
|
||||
|
||||
let eventID: string | undefined = undefined;
|
||||
// Optionally, send to conversion API endpoint if configured
|
||||
if (this._conversionClient) {
|
||||
if (this._conversionClient && !disableCAPI) {
|
||||
eventID = crypto.randomUUID();
|
||||
this._conversionClient
|
||||
.trackEvent('PageView', { eventID })
|
||||
@@ -241,13 +242,22 @@ export class PixelControl {
|
||||
|
||||
/**
|
||||
* Tracks a standard event for this pixel (uses `trackSingle` under the hood)
|
||||
* @param event Standard event name
|
||||
* @param params Event parameters
|
||||
* @param eventID Optional event ID for deduplication with Conversion API
|
||||
* @param disableCAPI If true, disables sending this event to the Conversion API
|
||||
* @throws Error if the Meta Pixel is not initialized.
|
||||
*/
|
||||
track<K extends StandardEventName>(event: K, params?: EventParamsByName[K], eventID?: string) {
|
||||
track<K extends StandardEventName>(
|
||||
event: K,
|
||||
params?: EventParamsByName[K],
|
||||
eventID?: string,
|
||||
disableCAPI: boolean = false
|
||||
) {
|
||||
if (!this.consentGuard()) return;
|
||||
|
||||
// Optionally, send to conversion API endpoint if configured
|
||||
if (this._conversionClient) {
|
||||
if (this._conversionClient && !disableCAPI) {
|
||||
eventID = eventID ?? crypto.randomUUID();
|
||||
this._conversionClient
|
||||
.trackEvent(event, { eventID: eventID, customData: pixelParamsToCustomData(params ?? {}) })
|
||||
|
||||
2
src/lib/types/conversion.d.ts
vendored
2
src/lib/types/conversion.d.ts
vendored
@@ -51,7 +51,7 @@ export type ConversionEventParams = {
|
||||
* Parameters for sending a conversion event to Meta Pixel.
|
||||
*/
|
||||
export type ConversionEventDetails = {
|
||||
eventID: string;
|
||||
eventID?: string;
|
||||
actionSource: 'website' | 'app' | 'offline' | 'other';
|
||||
userData: ConversionUserData;
|
||||
eventSourceURL?: string;
|
||||
|
||||
Reference in New Issue
Block a user