Skip to content

Commit

Permalink
Added basic support for pixel img URLs sendanor/project-sendanor.fi#272
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaakko Heusala committed Sep 22, 2024
1 parent 44692e0 commit 683c033
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 0 deletions.
56 changes: 56 additions & 0 deletions facebook/FacebookPixel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright (c) 2024. Heusala Group Oy <info@heusalagroup.fi>. All rights reserved.

import { Currency } from "../types/Currency";
import { FacebookPixelEvent } from "./types/FacebookPixelEvent";

export class FacebookPixel {

public static getURL (
pixelId: string,
ev: FacebookPixelEvent,
) : string {
return `https://www.facebook.com/tr?id=${pixelId}&ev=${ev}`;
}

public static getURLWithValue (
pixelId: string,
ev: FacebookPixelEvent,
value: number,
currency : Currency = Currency.EUR,
) : string {
return `https://www.facebook.com/tr?id=${pixelId}&ev=${ev}&cd[currency]=${currency}&cd[value]=${value.toFixed(2)}`;
}

public static getPurchaseURL (
pixelId: string,
value: number,
currency : Currency = Currency.EUR,
) : string {
return `https://www.facebook.com/tr?id=${pixelId}&ev=${FacebookPixelEvent.Purchase}&cd[currency]=${currency}&cd[value]=${value.toFixed(2)}`;
}

public static getInitiateCheckoutURL (
pixelId: string,
value: number,
currency : Currency = Currency.EUR,
) : string {
return `https://www.facebook.com/tr?id=${pixelId}&ev=${FacebookPixelEvent.InitiateCheckout}&cd[currency]=${currency}&cd[value]=${value.toFixed(2)}`;
}

public static getViewContentURL (
pixelId: string,
value: number,
currency : Currency = Currency.EUR,
) : string {
return `https://www.facebook.com/tr?id=${pixelId}&ev=${FacebookPixelEvent.ViewContent}&cd[currency]=${currency}&cd[value]=${value.toFixed(2)}`;
}

public static getAddToCartURL (
pixelId: string,
value: number,
currency : Currency = Currency.EUR,
) : string {
return `https://www.facebook.com/tr?id=${pixelId}&ev=${FacebookPixelEvent.AddToCart}&cd[currency]=${currency}&cd[value]=${value.toFixed(2)}`;
}

}
53 changes: 53 additions & 0 deletions facebook/types/FacebookPixelEvent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright (c) 2024. Heusala Group Oy <info@heusalagroup.fi>. All rights reserved.

import {
explainEnum,
isEnum,
parseEnum,
stringifyEnum,
} from "../../types/Enum";
import {
explainNot,
explainOk,
explainOr,
} from "../../types/explain";
import { isUndefined } from "../../types/undefined";

export enum FacebookPixelEvent {
AddPaymentInfo = "ADD_PAYMENT_INFO",
AddToCart = "ADD_TO_CART",
CompleteRegistration = "COMPLETE_REGISTRATION",
InitiateCheckout = "INITIATE_CHECKOUT",
Lead = "LEAD",
Purchase = "PURCHASE",
Schedule = "SCHEDULE",
Search = "SEARCH",
StartTrial = "START_TRIAL",
SubmitApplication = "SUBMIT_APPLICATION",
Subscribe = "SUBSCRIBE",
ViewContent = "VIEW_CONTENT",
}

export function isFacebookPixelEvent (value: unknown) : value is FacebookPixelEvent {
return isEnum(FacebookPixelEvent, value);
}

export function explainFacebookPixelEvent (value : unknown) : string {
return explainEnum("FacebookPixelEvent", FacebookPixelEvent, isFacebookPixelEvent, value);
}

export function stringifyFacebookPixelEvent (value : FacebookPixelEvent) : string {
return stringifyEnum(FacebookPixelEvent, value);
}

export function parseFacebookPixelEvent (value: any) : FacebookPixelEvent | undefined {
return parseEnum(FacebookPixelEvent, value) as FacebookPixelEvent | undefined;
}

export function isFacebookPixelEventOrUndefined (value: unknown): value is FacebookPixelEvent | undefined {
return isUndefined(value) || isFacebookPixelEvent(value);
}

export function explainFacebookPixelEventOrUndefined (value: unknown): string {
return isFacebookPixelEventOrUndefined(value) ? explainOk() : explainNot(explainOr(['FacebookPixelEvent', 'undefined']));
}

0 comments on commit 683c033

Please sign in to comment.