-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pc/consent: Fides.meta integration function (#2217)
- Loading branch information
1 parent
c4c6ed9
commit 02a6af3
Showing
8 changed files
with
229 additions
and
8 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
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
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
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
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
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
103 changes: 103 additions & 0 deletions
103
clients/privacy-center/packages/fides-consent/src/integrations/meta.ts
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,103 @@ | ||
declare global { | ||
namespace Meta { | ||
type DataProcessingOption = "LDU"; | ||
|
||
/** | ||
* Facebook doesn't document the many types of arguments this function accepts, | ||
* they just provides some examples, mainly of the 'track' action. | ||
* | ||
* Consent and Limited Data Use have their own examples: | ||
* - https://developers.facebook.com/docs/meta-pixel/implementation/gdpr | ||
* - https://developers.facebook.com/docs/meta-pixel/implementation/ccpa | ||
* - https://developers.facebook.com/docs/marketing-apis/data-processing-options/ | ||
*/ | ||
type FBQMethod = { | ||
( | ||
method: "dataProcessingOptions", | ||
options: Array<DataProcessingOption>, | ||
country?: 0 | 1, | ||
state?: 0 | 1000 | ||
): void; | ||
(method: "consent", consent: "revoke" | "grant"): void; | ||
(method: "init", pixelId: string): void; | ||
(method: string, ...args: any[]): void; | ||
}; | ||
|
||
type FBQObj = { | ||
loaded: boolean; | ||
version: string; | ||
queue: any[]; | ||
|
||
callMethod?: FBQMethod; | ||
push: (...args: Parameters<FBQMethod>) => void; | ||
}; | ||
|
||
type FBQ = FBQMethod & FBQObj; | ||
} | ||
|
||
interface Window { | ||
fbq?: Meta.FBQ; | ||
_fbq?: Meta.FBQ; | ||
} | ||
} | ||
|
||
/** | ||
* Facebook handles the possibility of the API script being loaded after attempts to call | ||
* `fbq(args)` by shimming `window.fbq` with a function that just enqueues the args to be called | ||
* later. | ||
*/ | ||
const getFbq = (): Meta.FBQ => { | ||
if (window.fbq) { | ||
return window.fbq; | ||
} | ||
|
||
const shim: Meta.FBQObj = { | ||
queue: [], | ||
loaded: true, | ||
version: "2.0", | ||
|
||
push(...args) { | ||
const fbq = window.fbq!; | ||
if (fbq.callMethod) { | ||
fbq.callMethod(...args); | ||
} else { | ||
fbq.queue.push(args); | ||
} | ||
}, | ||
}; | ||
|
||
window.fbq = Object.assign(shim.push, shim); | ||
|
||
// eslint-disable-next-line no-underscore-dangle | ||
window._fbq = window.fbq; | ||
|
||
return window.fbq; | ||
}; | ||
|
||
type MetaOptions = { | ||
consent: boolean | undefined; | ||
dataUse: boolean | undefined; | ||
}; | ||
|
||
/** | ||
* Call Fides.meta to configure Meta Pixel tracking. | ||
* | ||
* @example | ||
* Fides.meta({ | ||
* consent: Fides.consent.data_sales, | ||
* dataUse: Fides.consent.data_sales, | ||
* }) | ||
*/ | ||
export const meta = (options: MetaOptions) => { | ||
const fbq = getFbq(); | ||
|
||
fbq("consent", options.consent ? "grant" : "revoke"); | ||
|
||
if (options.dataUse) { | ||
fbq("dataProcessingOptions", []); | ||
} else { | ||
// The integer arguments translate to "treat this user as if they are in California" which will | ||
// limit the use of their data. | ||
fbq("dataProcessingOptions", ["LDU"], 1, 1000); | ||
} | ||
}; |
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