-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added basic support for pixel img URLs sendanor/project-sendanor.fi#272
- Loading branch information
Jaakko Heusala
committed
Sep 22, 2024
1 parent
44692e0
commit 683c033
Showing
2 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)}`; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'])); | ||
} |