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

Usage with @segment/analytics-next #193

Open Titou325 opened 3 years ago

Titou325 commented 3 years ago

Hi, thanks a lot for this plugin which is very useful!

We are trying to move to @segment/analytics-next as part of our integrated React codebase and as such are now using webpack to bundle the analytics. It thus seems that they are no longer available at window.analytics and we can't use react hooks on the consent response.

Here is an example of the code that we have :

export const SegmentTrackingProvider = ({
  writeKey,
  children,
  ...props
}: ISegmentTrackingProviderProps) => {
  const [analytics, setAnalytics] = useState<Analytics | undefined>(undefined)

  useEffect(() => {
    const loadAnalytics = async () => {
      const [response] = await AnalyticsBrowser.load({ writeKey })
      setAnalytics(response)
    }
    loadAnalytics()
  }, [writeKey])

  const track = useCallback(
    (event: ITrackingEvent) => {
      if (event.type === 'page') {
        analytics?.page(...event)
      } else {
        analytics?.track(event.name, event.properties)
      }
    },
    [analytics]
  )

  return (
    <TrackingContext.Provider
      value={{
        ...defaultTrackingContext,
        ...props,
        track
      }}
    >
      {children}
    </TrackingContext.Provider>
  )
}

How could we easily adapt this to work with the consent manager API, especially the custom consent manager? I guess it should be documented if feasible. Let us know if we can be of any help.

Have a nice day!

pooyaj commented 3 years ago

hey @Titou325, unfortunately this is not possible with the existing implementation of consent manager. The consent manager assumes that analytics is available on the window object ( in this method ) , and therefore, using the npm package is not compatible. This can be a good future improvement for consent manager, so I'll take a note and discuss with the team internally.

pedrocarloto commented 2 years ago

This feature would be very useful. In our implementation we would like to share the methods to call segment in the frotend and in our firebase functions. Due to this restriction we need to have the snippet on the frontend and then the @segment/analytics-next on the backend(functions) to also use it there.

Nightapes commented 2 years ago

Are there any news? When will it be possible to use it with @segment/analytics-next? We already using the new library as written in your documentation.

Also the old analytics.js is soon out of support.

On August 31, 2022, Segment will end support and maintenance for Analytics.js Classic https://segment.com/docs/connections/sources/catalog/libraries/website/javascript/upgrade-to-ajs2/

seanlail commented 2 years ago

The consent manager assumes that analytics is available on the window object ( in this method ) , and therefore, using the npm package is not compatible.

I don't understand - can we not use this?

There is documentation that says V2 is backwards compatible with V1... but then this Consent Manager doesn't work so it's not backwards compatible.

Can someone explain what is missing / incompatible and what is needed to support V2? Perhaps the community can help upgrading it.

pooyaj commented 2 years ago

@seanlail consent manager is only incompatible when using AJS 2.0 as an npm package. If using V2, but also using Segment Snippet to load AJS, consent manager still works as expected.

jayoutdoorsy commented 2 years ago

Is there a timeline for future support? If the library is backwards compatible with AJS 1.0 why can we simply not do:

window.analytics = AnalyticsBrowser.load({ writeKey }) // import from analytics-next

before ConsentManager is loaded?

MirrorJP commented 1 year ago

Any movement on this? Wanted to move to using the @segment/analytics-next package in our project alongside ConsentManagerBuilder, but found out from looking through the code that it wasn't possible before finding this issue. I wanted to try @jayoutdoorsy's approach to see what would happen, but apparently the AnalyticsBrowser has completely different typings from AnalyticsSnippet

kwent commented 1 year ago

Bump!

KabyleBOT commented 1 year ago

Up!

eddiesiegel commented 1 year ago

Bump

elliotaplant commented 9 months ago

It looks like Segment included a consent-management tool inside of their @segment/analytics-next package: https://github.com/segmentio/analytics-next/tree/master/packages/consent

I haven't used it yet, but I'm confident it's compatible with the rest of analytics-next