diff --git a/facebook/FacebookPixel.ts b/facebook/FacebookPixel.ts new file mode 100644 index 0000000..2d3aa77 --- /dev/null +++ b/facebook/FacebookPixel.ts @@ -0,0 +1,56 @@ +// Copyright (c) 2024. Heusala Group Oy . 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)}`; + } + +} diff --git a/facebook/types/FacebookPixelEvent.ts b/facebook/types/FacebookPixelEvent.ts new file mode 100644 index 0000000..30aedd8 --- /dev/null +++ b/facebook/types/FacebookPixelEvent.ts @@ -0,0 +1,53 @@ +// Copyright (c) 2024. Heusala Group Oy . 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'])); +}