-
Notifications
You must be signed in to change notification settings - Fork 1
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
[TF-633] Feature: migrate to @segment/analytics-next
package
#1
Changes from all commits
84f78e6
2a9247d
5e11a5d
8f5317d
fa8305f
2585a45
b20f7f4
3c5b94f
6208808
39db226
3833d08
75c28d7
0b327a4
b7f9f96
1234462
7025d74
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,17 @@ | ||
import type { Newsroom, Story } from '@prezly/sdk'; | ||
import { TrackingPolicy } from '@prezly/sdk'; | ||
import type { Analytics, Plugin } from '@segment/analytics-next'; | ||
import { AnalyticsBrowser } from '@segment/analytics-next'; | ||
import Head from 'next/head'; | ||
import Script from 'next/script'; | ||
import type { PropsWithChildren } from 'react'; | ||
import { createContext, useContext, useEffect, useState } from 'react'; | ||
|
||
import { | ||
createAnalyticsStub, | ||
getAnalyticsJsUrl, | ||
getConsentCookie, | ||
isPrezlyTrackingAllowed, | ||
setConsentCookie, | ||
} from './lib'; | ||
import { getConsentCookie, isPrezlyTrackingAllowed, setConsentCookie } from './lib'; | ||
import { injectPrezlyMetaPlugin, sendEventToPrezlyPlugin } from './plugins'; | ||
|
||
interface Context { | ||
analytics: Analytics | undefined; | ||
consent: boolean | null; | ||
isAnalyticsReady: boolean; | ||
isEnabled: boolean; | ||
isTrackingAllowed: boolean | null; | ||
newsroom: Newsroom; | ||
|
@@ -27,6 +23,7 @@ interface Props { | |
isEnabled?: boolean; | ||
newsroom: Newsroom; | ||
story: Story | undefined; | ||
plugins?: Plugin[]; | ||
} | ||
|
||
export const AnalyticsContext = createContext<Context | undefined>(undefined); | ||
|
@@ -45,16 +42,55 @@ export function AnalyticsContextProvider({ | |
isEnabled = true, | ||
newsroom, | ||
story, | ||
plugins, | ||
}: PropsWithChildren<Props>) { | ||
const { uuid, tracking_policy: trackingPolicy } = newsroom; | ||
const [isAnalyticsReady, setAnalyticsReady] = useState(false); | ||
const { | ||
tracking_policy: trackingPolicy, | ||
segment_analytics_id: segmentWriteKey, | ||
uuid, | ||
} = newsroom; | ||
const [consent, setConsent] = useState(getConsentCookie()); | ||
const isTrackingAllowed = isEnabled && isPrezlyTrackingAllowed(consent, newsroom); | ||
|
||
const [analytics, setAnalytics] = useState<Analytics | undefined>(undefined); | ||
|
||
useEffect(() => { | ||
if (trackingPolicy === TrackingPolicy.DISABLED) { | ||
window.analytics = createAnalyticsStub(); | ||
async function loadAnalytics(writeKey: string) { | ||
const [response] = await AnalyticsBrowser.load( | ||
{ | ||
writeKey, | ||
// If no Segment Write Key is provided, we initialize the library settings manually | ||
...(!writeKey && { | ||
cdnSettings: { | ||
integrations: {}, | ||
}, | ||
}), | ||
Comment on lines
+62
to
+67
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This code allows the library to initialize without a working Segment Write Key (it is used to fetch the source configuration from Segment, which we don't need to Prezly-only tracking). |
||
plugins: [ | ||
injectPrezlyMetaPlugin(), | ||
sendEventToPrezlyPlugin(uuid), | ||
...(plugins || []), | ||
], | ||
}, | ||
{ | ||
// By default, the analytics.js library plants its cookies on the top-level domain. | ||
// We need to completely isolate tracking between any Prezly newsroom hosted on a .prezly.com subdomain. | ||
cookie: { | ||
domain: document.location.host, | ||
}, | ||
// Disable calls to Segment API completely if no Write Key is provided | ||
...(!writeKey && { | ||
// eslint-disable-next-line @typescript-eslint/naming-convention | ||
integrations: { 'Segment.io': false }, | ||
}), | ||
Comment on lines
+80
to
+84
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. After digging through |
||
}, | ||
); | ||
setAnalytics(response); | ||
} | ||
|
||
if (isTrackingAllowed) { | ||
loadAnalytics(segmentWriteKey || ''); | ||
} | ||
}); | ||
}, [segmentWriteKey, isTrackingAllowed, uuid, plugins]); | ||
|
||
useEffect(() => { | ||
if (typeof consent === 'boolean') { | ||
|
@@ -65,29 +101,22 @@ export function AnalyticsContextProvider({ | |
return ( | ||
<AnalyticsContext.Provider | ||
value={{ | ||
analytics, | ||
consent, | ||
isAnalyticsReady, | ||
isEnabled, | ||
isTrackingAllowed: isEnabled && isPrezlyTrackingAllowed(consent, newsroom), | ||
isTrackingAllowed, | ||
newsroom, | ||
setConsent, | ||
trackingPolicy: newsroom.tracking_policy, | ||
trackingPolicy, | ||
}} | ||
> | ||
<Head> | ||
<meta name="prezly:newsroom" content={newsroom.uuid} /> | ||
{story && <meta name="prezly:story" content={story.uuid} />} | ||
{newsroom.tracking_policy !== TrackingPolicy.DEFAULT && ( | ||
<meta name="prezly:tracking_policy" content={newsroom.tracking_policy} /> | ||
{trackingPolicy !== TrackingPolicy.DEFAULT && ( | ||
<meta name="prezly:tracking_policy" content={trackingPolicy} /> | ||
)} | ||
</Head> | ||
{trackingPolicy !== TrackingPolicy.DISABLED && ( | ||
<Script | ||
key="prezly-analytics" | ||
onLoad={() => setAnalyticsReady(true)} | ||
src={getAnalyticsJsUrl(uuid)} | ||
/> | ||
)} | ||
{children} | ||
</AnalyticsContext.Provider> | ||
); | ||
|
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought it would be a nice feature to allow the package consumers to pass their own plugins, that way they would be able to extend the features without the need to fork the library.