forked from segmentio/ajs-multiple-writekeys-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSegment.js
29 lines (23 loc) · 1.25 KB
/
Segment.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import axios from 'axios'
import Analytics from '@segment/analytics.js-core/lib/analytics'
import SegmentIntegration from '@segment/analytics.js-integration-segmentio'
// This asynchronously returns an initialized analytics.js instance that can send data to the given writeKey.
// It is dynamically configured with data from the Segment API for the given writeKey, though some settings have to
// still be set manually (e.g. metrics).
async function Segment(writeKey) {
const settingsResponse = await axios.get(`https://cdn.segment.com/v1/projects/${writeKey}/settings`)
const integrations = await axios.get(`https://cdn.segment.com/v1/projects/${writeKey}/integrations`)
const enabledIntegrations = integrations.data.map( it => it.name )
const trackingPlanSettings = settingsResponse.data.plan
const integrationSettings = settingsResponse.data.integrations
integrationSettings['Segment.io'].addBundledMetadata = true
integrationSettings['Segment.io'].unbundledIntegrations = enabledIntegrations
const analytics = new Analytics()
analytics.use(SegmentIntegration)
analytics.initialize(integrationSettings, {
plan: trackingPlanSettings,
metrics: { sampleRate : 0.1 },
})
return analytics
}
export default Segment;