@customerio/cdp-analytics-browser 0.2.3 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/browser/index.js +3 -0
- package/dist/cjs/browser/index.js.map +1 -1
- package/dist/cjs/browser/standalone-analytics.js +7 -5
- package/dist/cjs/browser/standalone-analytics.js.map +1 -1
- package/dist/cjs/browser/standalone-interface.js +3 -0
- package/dist/cjs/browser/standalone-interface.js.map +1 -0
- package/dist/cjs/browser/standalone.js +7 -0
- package/dist/cjs/browser/standalone.js.map +1 -1
- package/dist/cjs/core/analytics/index.js +2 -1
- package/dist/cjs/core/analytics/index.js.map +1 -1
- package/dist/cjs/core/buffer/snippet.js +4 -2
- package/dist/cjs/core/buffer/snippet.js.map +1 -1
- package/dist/cjs/generated/version.js +1 -1
- package/dist/cjs/index.js +3 -0
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/lib/global-analytics-helper.js +34 -0
- package/dist/cjs/lib/global-analytics-helper.js.map +1 -0
- package/dist/cjs/lib/parse-cdn.js +19 -12
- package/dist/cjs/lib/parse-cdn.js.map +1 -1
- package/dist/cjs/plugins/in-app-plugin/index.js +28 -18
- package/dist/cjs/plugins/in-app-plugin/index.js.map +1 -1
- package/dist/pkg/browser/index.js +3 -0
- package/dist/pkg/browser/index.js.map +1 -1
- package/dist/pkg/browser/standalone-analytics.js +7 -5
- package/dist/pkg/browser/standalone-analytics.js.map +1 -1
- package/dist/pkg/browser/standalone-interface.js +2 -0
- package/dist/pkg/browser/standalone-interface.js.map +1 -0
- package/dist/pkg/browser/standalone.js +7 -0
- package/dist/pkg/browser/standalone.js.map +1 -1
- package/dist/pkg/core/analytics/index.js +2 -1
- package/dist/pkg/core/analytics/index.js.map +1 -1
- package/dist/pkg/core/buffer/snippet.js +4 -2
- package/dist/pkg/core/buffer/snippet.js.map +1 -1
- package/dist/pkg/generated/version.js +1 -1
- package/dist/pkg/index.js +1 -0
- package/dist/pkg/index.js.map +1 -1
- package/dist/pkg/lib/global-analytics-helper.js +28 -0
- package/dist/pkg/lib/global-analytics-helper.js.map +1 -0
- package/dist/pkg/lib/parse-cdn.js +19 -12
- package/dist/pkg/lib/parse-cdn.js.map +1 -1
- package/dist/pkg/plugins/in-app-plugin/index.js +28 -18
- package/dist/pkg/plugins/in-app-plugin/index.js.map +1 -1
- package/dist/types/browser/index.d.ts.map +1 -1
- package/dist/types/browser/standalone-analytics.d.ts +0 -14
- package/dist/types/browser/standalone-analytics.d.ts.map +1 -1
- package/dist/types/browser/standalone-interface.d.ts +10 -0
- package/dist/types/browser/standalone-interface.d.ts.map +1 -0
- package/dist/types/browser/standalone.d.ts.map +1 -1
- package/dist/types/core/analytics/index.d.ts +5 -0
- package/dist/types/core/analytics/index.d.ts.map +1 -1
- package/dist/types/core/buffer/index.d.ts +1 -1
- package/dist/types/core/buffer/snippet.d.ts +6 -1
- package/dist/types/core/buffer/snippet.d.ts.map +1 -1
- package/dist/types/generated/version.d.ts +1 -1
- package/dist/types/index.d.ts +2 -1
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/lib/global-analytics-helper.d.ts +18 -0
- package/dist/types/lib/global-analytics-helper.d.ts.map +1 -0
- package/dist/types/lib/parse-cdn.d.ts.map +1 -1
- package/dist/types/plugins/in-app-plugin/index.d.ts.map +1 -1
- package/dist/umd/646.js +1 -1
- package/dist/umd/646.js.gz +0 -0
- package/dist/umd/870.js +1 -1
- package/dist/umd/870.js.gz +0 -0
- package/dist/umd/inAppPlugin.js +1 -1
- package/dist/umd/inAppPlugin.js.gz +0 -0
- package/dist/umd/index.js +1 -1
- package/dist/umd/index.js.gz +0 -0
- package/dist/umd/standalone.js +1 -1
- package/dist/umd/standalone.js.gz +0 -0
- package/package.json +4 -4
- package/src/browser/index.ts +3 -0
- package/src/browser/standalone-analytics.ts +12 -24
- package/src/browser/standalone-interface.ts +11 -0
- package/src/browser/standalone.ts +12 -0
- package/src/core/analytics/index.ts +7 -1
- package/src/core/buffer/snippet.ts +6 -3
- package/src/generated/version.ts +1 -1
- package/src/index.ts +2 -2
- package/src/lib/global-analytics-helper.ts +31 -0
- package/src/lib/parse-cdn.ts +14 -7
- package/src/plugins/in-app-plugin/index.ts +28 -17
|
@@ -1,30 +1,19 @@
|
|
|
1
|
-
import { Analytics, InitOptions } from '../core/analytics'
|
|
2
1
|
import { AnalyticsBrowser } from '.'
|
|
3
2
|
import { embeddedWriteKey } from '../lib/embedded-write-key'
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
export interface AnalyticsStandalone extends Analytics {
|
|
10
|
-
_loadOptions?: InitOptions
|
|
11
|
-
_writeKey?: string
|
|
12
|
-
_cdn?: string
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
declare global {
|
|
16
|
-
interface Window {
|
|
17
|
-
analytics: AnalyticsSnippet
|
|
18
|
-
}
|
|
19
|
-
}
|
|
3
|
+
import { AnalyticsSnippet } from './standalone-interface'
|
|
4
|
+
import {
|
|
5
|
+
getGlobalAnalytics,
|
|
6
|
+
setGlobalAnalytics,
|
|
7
|
+
} from '../lib/global-analytics-helper'
|
|
20
8
|
|
|
21
9
|
function getWriteKey(): string | undefined {
|
|
22
10
|
if (embeddedWriteKey()) {
|
|
23
11
|
return embeddedWriteKey()
|
|
24
12
|
}
|
|
25
13
|
|
|
26
|
-
|
|
27
|
-
|
|
14
|
+
const analytics = getGlobalAnalytics()
|
|
15
|
+
if (analytics?._writeKey) {
|
|
16
|
+
return analytics._writeKey
|
|
28
17
|
}
|
|
29
18
|
|
|
30
19
|
const regex = /http.*\/analytics\.js\/v1\/([^/]*)(\/platform)?\/analytics.*/
|
|
@@ -59,7 +48,7 @@ function getWriteKey(): string | undefined {
|
|
|
59
48
|
|
|
60
49
|
export async function install(): Promise<void> {
|
|
61
50
|
const writeKey = getWriteKey()
|
|
62
|
-
const options =
|
|
51
|
+
const options = getGlobalAnalytics()?._loadOptions ?? {}
|
|
63
52
|
if (!writeKey) {
|
|
64
53
|
console.error(
|
|
65
54
|
'Failed to load Write Key. Make sure to use the latest version of the snippet, which can be found in your source settings.'
|
|
@@ -67,8 +56,7 @@ export async function install(): Promise<void> {
|
|
|
67
56
|
return
|
|
68
57
|
}
|
|
69
58
|
|
|
70
|
-
|
|
71
|
-
writeKey,
|
|
72
|
-
|
|
73
|
-
)) as AnalyticsSnippet
|
|
59
|
+
setGlobalAnalytics(
|
|
60
|
+
(await AnalyticsBrowser.standalone(writeKey, options)) as AnalyticsSnippet
|
|
61
|
+
)
|
|
74
62
|
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Analytics, InitOptions } from '../core/analytics'
|
|
2
|
+
|
|
3
|
+
export interface AnalyticsSnippet extends AnalyticsStandalone {
|
|
4
|
+
load: (writeKey: string, options?: InitOptions) => void
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export interface AnalyticsStandalone extends Analytics {
|
|
8
|
+
_loadOptions?: InitOptions
|
|
9
|
+
_writeKey?: string
|
|
10
|
+
_cdn?: string
|
|
11
|
+
}
|
|
@@ -2,6 +2,17 @@
|
|
|
2
2
|
import { getCDN, setGlobalCDNUrl } from '../lib/parse-cdn'
|
|
3
3
|
import { setVersionType } from '../plugins/customerio/normalize'
|
|
4
4
|
|
|
5
|
+
// The global analytics key must be set first so that subsequent calls to getCdn() fetch the CDN from the correct instance.
|
|
6
|
+
const globalAnalyticsKey = (
|
|
7
|
+
document.querySelector(
|
|
8
|
+
'script[data-global-customerio-analytics-key]'
|
|
9
|
+
) as HTMLScriptElement
|
|
10
|
+
)?.dataset.globalCustomerioAnalyticsKey
|
|
11
|
+
|
|
12
|
+
if (globalAnalyticsKey) {
|
|
13
|
+
setGlobalAnalyticsKey(globalAnalyticsKey)
|
|
14
|
+
}
|
|
15
|
+
|
|
5
16
|
if (process.env.ASSET_PATH) {
|
|
6
17
|
if (process.env.ASSET_PATH === '/dist/umd/') {
|
|
7
18
|
// @ts-ignore
|
|
@@ -25,6 +36,7 @@ import { shouldPolyfill } from '../lib/browser-polyfill'
|
|
|
25
36
|
import { RemoteMetrics } from '../core/stats/remote-metrics'
|
|
26
37
|
import { embeddedWriteKey } from '../lib/embedded-write-key'
|
|
27
38
|
import { onCSPError } from '../lib/csp-detection'
|
|
39
|
+
import { setGlobalAnalyticsKey } from '../lib/global-analytics-helper'
|
|
28
40
|
|
|
29
41
|
function onError(err?: unknown) {
|
|
30
42
|
console.error('[analytics.js]', 'Failed to load Analytics.js', err)
|
|
@@ -48,6 +48,7 @@ import { version } from '../../generated/version'
|
|
|
48
48
|
import { PriorityQueue } from '../../lib/priority-queue'
|
|
49
49
|
import { getGlobal } from '../../lib/get-global'
|
|
50
50
|
import { AnalyticsClassic, AnalyticsCore } from './interfaces'
|
|
51
|
+
import { setGlobalAnalytics } from '../../lib/global-analytics-helper'
|
|
51
52
|
|
|
52
53
|
const deprecationWarning =
|
|
53
54
|
'This is being deprecated and will be not be available in future releases of Analytics JS'
|
|
@@ -102,6 +103,11 @@ export interface InitOptions {
|
|
|
102
103
|
aid?: RegExp
|
|
103
104
|
uid?: RegExp
|
|
104
105
|
}
|
|
106
|
+
/**
|
|
107
|
+
* When using the snippet, this is the key that points to the global analytics instance (e.g. window.analytics).
|
|
108
|
+
* default: analytics
|
|
109
|
+
*/
|
|
110
|
+
globalAnalyticsKey?: string
|
|
105
111
|
}
|
|
106
112
|
|
|
107
113
|
/* analytics-classic stubs */
|
|
@@ -468,7 +474,7 @@ export class Analytics
|
|
|
468
474
|
|
|
469
475
|
noConflict(): Analytics {
|
|
470
476
|
console.warn(deprecationWarning)
|
|
471
|
-
|
|
477
|
+
setGlobalAnalytics(_analytics ?? this)
|
|
472
478
|
return this
|
|
473
479
|
}
|
|
474
480
|
|
|
@@ -3,6 +3,7 @@ import type {
|
|
|
3
3
|
PreInitMethodName,
|
|
4
4
|
PreInitMethodParams,
|
|
5
5
|
} from '.'
|
|
6
|
+
import { getGlobalAnalytics } from '../../lib/global-analytics-helper'
|
|
6
7
|
|
|
7
8
|
export function transformSnippetCall([
|
|
8
9
|
methodName,
|
|
@@ -29,14 +30,16 @@ type SnippetWindowBufferedMethodCall<
|
|
|
29
30
|
* A list of the method calls before initialization for snippet users
|
|
30
31
|
* For example, [["track", "foo", {bar: 123}], ["page"], ["on", "ready", function(){..}]
|
|
31
32
|
*/
|
|
32
|
-
type SnippetBuffer = SnippetWindowBufferedMethodCall[]
|
|
33
|
+
export type SnippetBuffer = SnippetWindowBufferedMethodCall[]
|
|
33
34
|
|
|
34
35
|
/**
|
|
35
36
|
* Fetch the buffered method calls from the window object and normalize them.
|
|
36
37
|
* This removes existing buffered calls from the window object.
|
|
37
38
|
*/
|
|
38
|
-
export const popSnippetWindowBuffer = (
|
|
39
|
-
|
|
39
|
+
export const popSnippetWindowBuffer = (
|
|
40
|
+
buffer: unknown = getGlobalAnalytics()
|
|
41
|
+
): PreInitMethodCall[] => {
|
|
42
|
+
const wa = buffer
|
|
40
43
|
if (!Array.isArray(wa)) return []
|
|
41
44
|
const buffered = wa.splice(0, wa.length)
|
|
42
45
|
return normalizeSnippetBuffer(buffered)
|
package/src/generated/version.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
// This file is generated.
|
|
2
|
-
export const version = '0.
|
|
2
|
+
export const version = '0.3.1'
|
package/src/index.ts
CHANGED
|
@@ -8,5 +8,5 @@ export * from './core/plugin'
|
|
|
8
8
|
export * from './core/user'
|
|
9
9
|
|
|
10
10
|
export * from './plugins/in-app-plugin'
|
|
11
|
-
|
|
12
|
-
export
|
|
11
|
+
export type { AnalyticsSnippet } from './browser/standalone-interface'
|
|
12
|
+
export { getGlobalAnalytics } from './lib/global-analytics-helper'
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { AnalyticsSnippet } from '../browser/standalone-interface'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Stores the global window analytics key
|
|
5
|
+
*/
|
|
6
|
+
let _globalAnalyticsKey = 'analytics'
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Gets the global analytics/buffer
|
|
10
|
+
* @param key name of the window property where the buffer is stored (default: analytics)
|
|
11
|
+
* @returns AnalyticsSnippet
|
|
12
|
+
*/
|
|
13
|
+
export function getGlobalAnalytics(): AnalyticsSnippet | undefined {
|
|
14
|
+
return (window as any)[_globalAnalyticsKey]
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Replaces the global window key for the analytics/buffer object
|
|
19
|
+
* @param key key name
|
|
20
|
+
*/
|
|
21
|
+
export function setGlobalAnalyticsKey(key: string) {
|
|
22
|
+
_globalAnalyticsKey = key
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Sets the global analytics object
|
|
27
|
+
* @param analytics analytics snippet
|
|
28
|
+
*/
|
|
29
|
+
export function setGlobalAnalytics(analytics: AnalyticsSnippet): void {
|
|
30
|
+
;(window as any)[_globalAnalyticsKey] = analytics
|
|
31
|
+
}
|
package/src/lib/parse-cdn.ts
CHANGED
|
@@ -1,33 +1,40 @@
|
|
|
1
|
+
import { getGlobalAnalytics } from './global-analytics-helper'
|
|
1
2
|
import { embeddedWriteKey } from './embedded-write-key'
|
|
2
3
|
|
|
3
4
|
const analyticsScriptRegex =
|
|
4
|
-
/(https
|
|
5
|
+
/(https?:\/\/[\w.\-:]+)\/(?:analytics\.js\/v1|v1\/analytics-js\/snippet)\/[\w\-:]+\/(analytics\.(?:min)\.js)/
|
|
5
6
|
|
|
6
7
|
const getCDNUrlFromScriptTag = (): string | undefined => {
|
|
7
8
|
let cdn: string | undefined
|
|
8
9
|
const scripts = Array.prototype.slice.call(
|
|
9
10
|
document.querySelectorAll('script')
|
|
10
11
|
)
|
|
11
|
-
|
|
12
|
+
for (const s of scripts) {
|
|
12
13
|
const src = s.getAttribute('src') ?? ''
|
|
13
14
|
const result = analyticsScriptRegex.exec(src)
|
|
14
15
|
|
|
15
16
|
if (result && result[1]) {
|
|
16
17
|
cdn = result[1]
|
|
17
18
|
}
|
|
18
|
-
|
|
19
|
+
|
|
20
|
+
// If the script tag has the globalCustomerioAnalyticsKey attribute, then this is a CDP script (not segment).
|
|
21
|
+
if (s.dataset?.globalCustomerioAnalyticsKey != null) {
|
|
22
|
+
break
|
|
23
|
+
}
|
|
24
|
+
}
|
|
19
25
|
return cdn
|
|
20
26
|
}
|
|
21
27
|
|
|
22
28
|
let _globalCDN: string | undefined // set globalCDN as in-memory singleton
|
|
23
29
|
const getGlobalCDNUrl = (): string | undefined => {
|
|
24
|
-
const result = _globalCDN ??
|
|
30
|
+
const result = _globalCDN ?? getGlobalAnalytics()?._cdn
|
|
25
31
|
return result
|
|
26
32
|
}
|
|
27
33
|
|
|
28
34
|
export const setGlobalCDNUrl = (cdn: string) => {
|
|
29
|
-
|
|
30
|
-
|
|
35
|
+
const globalAnalytics = getGlobalAnalytics()
|
|
36
|
+
if (globalAnalytics) {
|
|
37
|
+
globalAnalytics._cdn = cdn
|
|
31
38
|
}
|
|
32
39
|
_globalCDN = cdn
|
|
33
40
|
}
|
|
@@ -61,7 +68,7 @@ export const getNextIntegrationsURL = () => {
|
|
|
61
68
|
* @returns the path to Analytics JS 1.0
|
|
62
69
|
**/
|
|
63
70
|
export function getLegacyAJSPath(): string {
|
|
64
|
-
const writeKey = embeddedWriteKey() ??
|
|
71
|
+
const writeKey = embeddedWriteKey() ?? getGlobalAnalytics()?._writeKey
|
|
65
72
|
|
|
66
73
|
const scripts = Array.prototype.slice.call(
|
|
67
74
|
document.querySelectorAll('script')
|
|
@@ -36,7 +36,7 @@ export function InAppPlugin(
|
|
|
36
36
|
allEvents.forEach((event) => {
|
|
37
37
|
_eventTarget.addEventListener(event, settings?.events as EventListenerOrEventListenerObject);
|
|
38
38
|
});
|
|
39
|
-
['
|
|
39
|
+
['messageDismissed', 'messageError'].forEach((event) => {
|
|
40
40
|
Gist.events.on(event, (message: any) => {
|
|
41
41
|
_eventTarget.dispatchEvent(newEvent(gistToCIO(event), {
|
|
42
42
|
messageId: message.messageId,
|
|
@@ -48,6 +48,17 @@ export function InAppPlugin(
|
|
|
48
48
|
|
|
49
49
|
Gist.events.on('messageShown', (message: any) => {
|
|
50
50
|
const deliveryId:string = message?.properties?.gist?.campaignId;
|
|
51
|
+
if (settings.events) {
|
|
52
|
+
_eventTarget.dispatchEvent(newEvent(InAppEvents.MessageOpened, {
|
|
53
|
+
messageId: message?.messageId,
|
|
54
|
+
deliveryId: deliveryId,
|
|
55
|
+
message: {
|
|
56
|
+
dismiss: function() {
|
|
57
|
+
Gist.dismissMessage(message?.instanceId);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}));
|
|
61
|
+
}
|
|
51
62
|
if (typeof deliveryId != 'undefined' && deliveryId != '') {
|
|
52
63
|
_analytics.track(JourneysEvents.Metric, {
|
|
53
64
|
'deliveryId': deliveryId,
|
|
@@ -68,10 +79,25 @@ export function InAppPlugin(
|
|
|
68
79
|
});
|
|
69
80
|
|
|
70
81
|
Gist.events.on('messageAction', (params: any) => {
|
|
82
|
+
const deliveryId:string = params?.message?.properties?.gist?.campaignId;
|
|
83
|
+
if (settings.events) {
|
|
84
|
+
_eventTarget.dispatchEvent(newEvent(InAppEvents.MessageAction, {
|
|
85
|
+
messageId: params.message.messageId,
|
|
86
|
+
deliveryId: deliveryId,
|
|
87
|
+
action: params.action,
|
|
88
|
+
name: params.name,
|
|
89
|
+
actionName: params.name,
|
|
90
|
+
actionValue: params.action,
|
|
91
|
+
message: {
|
|
92
|
+
dismiss: function() {
|
|
93
|
+
Gist.dismissMessage(params.message.instanceId);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}));
|
|
97
|
+
}
|
|
71
98
|
if (params.action == 'gist://close') {
|
|
72
99
|
return;
|
|
73
100
|
}
|
|
74
|
-
const deliveryId:string = params?.message?.properties?.gist?.campaignId;
|
|
75
101
|
if (typeof deliveryId != 'undefined' && deliveryId != '') {
|
|
76
102
|
_analytics.track(JourneysEvents.Metric, {
|
|
77
103
|
'deliveryId': deliveryId,
|
|
@@ -79,21 +105,6 @@ export function InAppPlugin(
|
|
|
79
105
|
'actionName': params.name,
|
|
80
106
|
'actionValue': params.action,
|
|
81
107
|
});
|
|
82
|
-
if (settings.events) {
|
|
83
|
-
_eventTarget.dispatchEvent(newEvent(InAppEvents.MessageAction, {
|
|
84
|
-
messageId: params.message.messageId,
|
|
85
|
-
deliveryId: deliveryId,
|
|
86
|
-
action: params.action,
|
|
87
|
-
name: params.name,
|
|
88
|
-
actionName: params.name,
|
|
89
|
-
actionValue: params.action,
|
|
90
|
-
message:{
|
|
91
|
-
dismiss: function() {
|
|
92
|
-
Gist.dismissMessage(params?.message?.instanceId);
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
}));
|
|
96
|
-
}
|
|
97
108
|
return;
|
|
98
109
|
}
|
|
99
110
|
const broadcastId:Number = params?.message?.properties?.gist?.broadcast?.broadcastIdInt;
|