Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dispatch Fides.js lifecycle events on window (FidesInitialized, FidesUpdated) and cross-publish to Fides.gtm() integration #3454

Merged
merged 17 commits into from
Jun 6, 2023

Conversation

NevilleS
Copy link
Contributor

@NevilleS NevilleS commented Jun 4, 2023

Closes #3350
Closes #3348

Code Changes

  • Dispatch a "FidesInitialized" event on window when Fides.init() completes successfully
  • Dispatch a "FidesUpdated" event on window whenever preferences are updated
  • Update Fides.gtm() Google Tag Manager integration to listen for lifecycle events and publish to GTM whenever consent preferences are initialized/updated
  • Add Cypress tests to clients/privacy-center to listen for Fides.js events
  • Add "FidesInitialized" event name to GTM, once we confirm that wouldn't be a breaking change

Steps to Confirm

  • Run privacy center Cypress tests
  • Run sample app locally with Google Tag Manager configured

Pre-Merge Checklist

Description Of Changes

This exposes two new custom events on the window object to allow a user to listen for Fides.js lifecycle events and integrate consent state changes into their own custom Javascript handling. This is needed now that we allow preferences to change at runtime via the banner/modal components!

For example, you might integrate Fides.js into your website like:

<script src="privacy.example.com.fides.js" />
<script>
  window.addEventListener("FidesUpdated", (event) => {
    if (event.detail.consent.data_sales_and_sharing) {
      // enable data sales code
    }
  });
</script>

This PR also updates the Fides.gtm() integration to listen to the same lifecycle events and cross-publish them to GTM. This ensures GTM stays up to date if consent preferences are updated by the banner/modal.

@NevilleS
Copy link
Contributor Author

NevilleS commented Jun 4, 2023

This should be good to go. Just want to triple-check the event publishing to GTM. I'd also like to add the event: "FidesInitialized" to the existing dataLayer.push() we do, but just want to confirm that wouldn't mess up any existing GTM integrations with some manual testing.

@cypress
Copy link

cypress bot commented Jun 4, 2023

Passing run #2436 ↗︎

0 4 0 0 Flakiness 0
⚠️ You've recorded test results over your free plan limit.
Upgrade your plan to view test results.

Details:

Merge f52d09c into 67db31e...
Project: fides Commit: 58a22243b4 ℹ️
Status: Passed Duration: 00:49 💡
Started: Jun 5, 2023 10:30 PM Ended: Jun 5, 2023 10:31 PM

This comment has been generated by cypress-bot as a result of this project's GitHub integration settings.

@NevilleS
Copy link
Contributor Author

NevilleS commented Jun 5, 2023

Did some more research on GTM this morning and I'm quite confident that I can safely add event: "FidesInitialized" to the current .push call without any negative consequences and it'll just make it easier to use and debug 👍

Still want to do some manual testing before merging this, but going to adjust that now and open up for review...

Copy link
Contributor Author

@NevilleS NevilleS left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Few comments to help explain what's going on

@@ -82,6 +82,8 @@ type MetaOptions = {
/**
* Call Fides.meta to configure Meta Pixel tracking.
*
* DEFER: Update this integration to support async Fides events
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add follow-up issue

@@ -33,6 +33,8 @@ const applyOptions = (options: ShopifyOptions) => {
* Call Fides.shopify to configure Shopify customer privacy. Currently the only consent option
* Shopify allows to be configured is user tracking.
*
* DEFER: Update this integration to support async Fides events
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add follow-up issue

/**
* Defines the properties available on event.detail. As of now, these are an
* explicit subset of properties from the Fides cookie
* TODO: add identity and meta?
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Decide on this - I think it makes sense to include these in the event, I could see a listener wanting a bit of information about the identity when processing the preferences, etc.

Copy link
Contributor

@eastandwestwind eastandwestwind Jun 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will facebook / shopify need identity or other metadata?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing needs it yet, but I'm wondering what a developer might need when integrating consent into their app via these events...

Comment on lines +53 to +55
// Update the cookie object
// eslint-disable-next-line no-param-reassign
cookie.consent = consentCookieKey;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This mutates the provided cookie param, which might be a bit surprising... but I think it makes sense to do this as we don't store that state anywhere else super declaratively. This is especially true since we call saveFidesCookie below which persists the updated state to the browser - it'd be surprising to me to find that the cookie object was actually more stale than the version saved to browser since we weren't updating that object before!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is fine. It's the same pattern we use in the init method of fides.ts

Copy link
Contributor Author

@NevilleS NevilleS left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Few comments to help explain what's going on

@NevilleS NevilleS marked this pull request as ready for review June 5, 2023 13:58
// Pick a subset of the Fides cookie properties to provide as event detail
const { consent }: FidesEventDetail = cookie;
const detail: FidesEventDetail = { consent };
const event = new CustomEvent(type, { detail });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah this is cool, learned about CustomEvent native obj today

Copy link
Contributor

@eastandwestwind eastandwestwind left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good so far.

Main thing I'd like to see updated is in fides-js-components-demo.html and fides-js-demo.html, we are currently using a timer to check when fides is initialized. We can now replace this with the addEventListener you've added. Feel free to create a new ticket, and I can take care of it, either way.

@NevilleS
Copy link
Contributor Author

NevilleS commented Jun 5, 2023

I've figured out how to use the GTM preview mode to debug the firing of events locally:
image

Everything works nicely here. The only issue is the async nature of FidesInitialized; it'd be more convenient for it to fire faster... but that's just the reality of how this overlay is configured now.

@NevilleS
Copy link
Contributor Author

NevilleS commented Jun 5, 2023

Blergh - I need to edit these tests to work still.

@NevilleS NevilleS merged commit 384301e into feature/privacy-components Jun 6, 2023
@NevilleS NevilleS deleted the ns-add-events-api branch June 6, 2023 01:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants