segmentio / consent-manager

Drop-in consent management plugin for analytics.js
https://segmentio.github.io/consent-manager/
MIT License
341 stars 144 forks source link

Standalone CM does not set cookie correctly when calling acceptAll #259

Closed seanlail closed 2 years ago

seanlail commented 2 years ago

I set the CM to not track by default, but create a custom "Allow" button that calls the internal "acceptAll" function.

Expected outcome: Cookie to be set with all keys accepted

Actual outcome: All declined.

window.consentManagerConfig = ({ doNotTrack, inEU, preferences, React }) => {
  return {
    bannerActionsBlock: (props) =>
      React.createElement("button", { onclick: () => props.acceptAll() }, "Allow"),  // clicking this button does not accept all cookies?
    bannerContent: React.createElement("span", null, "This site uses cookies"),
    bannerSubContent: "Preferences",
    bannerHideCloseButton: true,
    cancelDialogTitle: "Lorem Ipsum",
    cancelDialogContent: "Lorem Ipsum",
    container: "#segment-cookies",
    cookieName: "tracking-preferences",
    defaultDestinationBehavior: "enable",
    implyConsentOnInteraction: false,
    initialPreferences: {
      functional: false,
      marketingAndAnalytics: false,
      advertising: false,
    },
    preferencesDialogContent: "Lorem Ipsum",
    preferencesDialogTitle: "Lorem Ipsum",
    writeKey: "<segment-key-goes-here>",
  };
};
seanlail commented 2 years ago

@edsonjab Here's a repo to show the issue: https://github.com/seanlail/segment-cm-issue-259

Create .env.local and set the NEXT_PUBLIC_SEGMENT_APIKEY=<a valid key> to run it.

seanlail commented 2 years ago

@edsonjab I think this is where the bug is: https://github.com/segmentio/consent-manager/blob/a4633dfe3d9f6e6fb64816245b3e768e56b6d9bb/src/consent-manager/container.tsx#L96 This assumes that the initial preferences are all "enabled" / "true".

seanlail commented 2 years ago

My workaround is to call preferences.savePreferences manually and then reload the page. I don't know all the destinations dynamically though.

edsonjab commented 2 years ago

@seanlail I will make some test for initialPreferences, like you say maybe there is the bug