Skip to content
This repository has been archived by the owner on Apr 3, 2024. It is now read-only.

Usage with @segment/analytics-next #193

Open
Titou325 opened this issue Nov 21, 2021 · 11 comments
Open

Usage with @segment/analytics-next #193

Titou325 opened this issue Nov 21, 2021 · 11 comments
Labels
enhancement New feature or request

Comments

@Titou325
Copy link

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
Copy link
Contributor

pooyaj commented Nov 25, 2021

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.

@nd4p90x nd4p90x added the enhancement New feature or request label Jan 26, 2022
@pedrocarloto
Copy link

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
Copy link

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
Copy link

seanlail commented Sep 5, 2022

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
Copy link
Contributor

pooyaj commented Sep 6, 2022

@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
Copy link

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
Copy link

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
Copy link

kwent commented Oct 17, 2023

Bump!

@KabyleBOT
Copy link

Up!

@eddiesiegel
Copy link

Bump

@elliotaplant
Copy link

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests