-
Notifications
You must be signed in to change notification settings - Fork 73
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.shopify integration function (#2152)
- Loading branch information
1 parent
8ea5e8b
commit 34c3da3
Showing
7 changed files
with
150 additions
and
20 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
18 changes: 18 additions & 0 deletions
18
clients/privacy-center/packages/fides-consent/src/integrations/gtm.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,18 @@ | ||
declare global { | ||
interface Window { | ||
dataLayer?: any[]; | ||
} | ||
} | ||
|
||
/** | ||
* Call Fides.gtm to configure Google Tag Manager. The user's consent choices will be | ||
* pushed into GTM's `dataLayer` under `Fides.consent`. | ||
*/ | ||
export const gtm = () => { | ||
const dataLayer = window.dataLayer ?? []; | ||
dataLayer.push({ | ||
Fides: { | ||
consent: window.Fides.consent, | ||
}, | ||
}); | ||
}; |
68 changes: 68 additions & 0 deletions
68
clients/privacy-center/packages/fides-consent/src/integrations/shopify.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,68 @@ | ||
declare global { | ||
interface Window { | ||
Shopify?: { | ||
/** https://shopify.dev/api/consent-tracking#loading-pattern-for-visitor-tracking */ | ||
loadFeatures( | ||
features: Array<{ name: string; version: string }>, | ||
callback: (error: Error) => void | ||
): void; | ||
customerPrivacy?: { | ||
/** https://shopify.dev/api/consent-tracking#settrackingconsent-consent-boolean-callback-function */ | ||
setTrackingConsent(consent: boolean, callback: () => void): void; | ||
}; | ||
}; | ||
} | ||
} | ||
|
||
type ShopifyOptions = { | ||
tracking: boolean | undefined; | ||
}; | ||
|
||
const applyOptions = (options: ShopifyOptions) => { | ||
if (!window.Shopify?.customerPrivacy) { | ||
throw Error("Fides could not access Shopify's customerPrivacy API"); | ||
} | ||
|
||
window.Shopify.customerPrivacy.setTrackingConsent( | ||
Boolean(options.tracking), | ||
() => {} | ||
); | ||
}; | ||
|
||
/** | ||
* Call Fides.shopify to configure Shopify customer privacy. Currently the only consent option | ||
* Shopify allows to be configured is user tracking. | ||
* | ||
* @example | ||
* Fides.shopify({ tracking: Fides.consent.data_sales }) | ||
*/ | ||
export const shopify = (options: ShopifyOptions) => { | ||
if (!window.Shopify) { | ||
throw Error( | ||
"Fides.shopify was called but Shopify is not present in the page." | ||
); | ||
} | ||
|
||
// If the API is already present, simply call it. | ||
if (window.Shopify.customerPrivacy) { | ||
applyOptions(options); | ||
return; | ||
} | ||
|
||
// Otherwise we need to load the feature before applying the options. | ||
window.Shopify.loadFeatures( | ||
[ | ||
{ | ||
name: "consent-tracking-api", | ||
version: "0.1", | ||
}, | ||
], | ||
(error) => { | ||
if (error) { | ||
throw Error("Fides could not load Shopify's consent-tracking-api"); | ||
} | ||
|
||
applyOptions(options); | ||
} | ||
); | ||
}; |