@ht-sdks/events-sdk-js-browser 1.2.0 → 1.3.0
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/README.md +72 -4
- package/dist/cjs/browser/index.js +57 -31
- package/dist/cjs/browser/index.js.map +1 -1
- package/dist/cjs/core/analytics/index.js.map +1 -1
- package/dist/cjs/core/http-cookies/index.js +11 -3
- package/dist/cjs/core/http-cookies/index.js.map +1 -1
- package/dist/cjs/core/user/index.js +7 -1
- package/dist/cjs/core/user/index.js.map +1 -1
- package/dist/cjs/core/user/tld.js +7 -3
- package/dist/cjs/core/user/tld.js.map +1 -1
- package/dist/cjs/generated/version.js +1 -1
- package/dist/cjs/index.js +3 -1
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/plugins/destinations/destination.js +81 -0
- package/dist/cjs/plugins/destinations/destination.js.map +1 -0
- package/dist/cjs/plugins/destinations/google-tag-manager.js +47 -0
- package/dist/cjs/plugins/destinations/google-tag-manager.js.map +1 -0
- package/dist/cjs/plugins/destinations/index.js +22 -0
- package/dist/cjs/plugins/destinations/index.js.map +1 -0
- package/dist/cjs/plugins/destinations/types.js +3 -0
- package/dist/cjs/plugins/destinations/types.js.map +1 -0
- package/dist/pkg/browser/index.js +57 -31
- package/dist/pkg/browser/index.js.map +1 -1
- package/dist/pkg/core/analytics/index.js.map +1 -1
- package/dist/pkg/core/http-cookies/index.js +11 -3
- package/dist/pkg/core/http-cookies/index.js.map +1 -1
- package/dist/pkg/core/user/index.js +7 -1
- package/dist/pkg/core/user/index.js.map +1 -1
- package/dist/pkg/core/user/tld.js +7 -3
- package/dist/pkg/core/user/tld.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/plugins/destinations/destination.js +78 -0
- package/dist/pkg/plugins/destinations/destination.js.map +1 -0
- package/dist/pkg/plugins/destinations/google-tag-manager.js +45 -0
- package/dist/pkg/plugins/destinations/google-tag-manager.js.map +1 -0
- package/dist/pkg/plugins/destinations/index.js +18 -0
- package/dist/pkg/plugins/destinations/index.js.map +1 -0
- package/dist/pkg/plugins/destinations/types.js +2 -0
- package/dist/pkg/plugins/destinations/types.js.map +1 -0
- package/dist/types/browser/index.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/http-cookies/index.d.ts +4 -0
- package/dist/types/core/http-cookies/index.d.ts.map +1 -1
- package/dist/types/core/user/index.d.ts.map +1 -1
- package/dist/types/core/user/tld.d.ts.map +1 -1
- package/dist/types/generated/version.d.ts +1 -1
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/plugins/destinations/destination.d.ts +32 -0
- package/dist/types/plugins/destinations/destination.d.ts.map +1 -0
- package/dist/types/plugins/destinations/google-tag-manager.d.ts +30 -0
- package/dist/types/plugins/destinations/google-tag-manager.d.ts.map +1 -0
- package/dist/types/plugins/destinations/index.d.ts +6 -0
- package/dist/types/plugins/destinations/index.d.ts.map +1 -0
- package/dist/types/plugins/destinations/types.d.ts +5 -0
- package/dist/types/plugins/destinations/types.d.ts.map +1 -0
- package/dist/umd/events.min.js +1 -1
- package/dist/umd/events.min.js.map +1 -1
- package/dist/umd/google-tag-manager.bundle.c27daab560c3298c04ca.js +2 -0
- package/dist/umd/google-tag-manager.bundle.c27daab560c3298c04ca.js.map +1 -0
- package/dist/umd/index.js +1 -1
- package/dist/umd/index.js.map +1 -1
- package/package.json +13 -18
- package/src/browser/index.ts +14 -1
- package/src/core/analytics/index.ts +6 -0
- package/src/core/http-cookies/README.md +9 -106
- package/src/core/http-cookies/index.ts +16 -3
- package/src/core/http-cookies/server-examples/node-aws-lambda.md +167 -0
- package/src/core/http-cookies/server-examples/node-express-js.md +103 -0
- package/src/core/http-cookies/server-examples/node-next-js.md +75 -0
- package/src/core/user/index.ts +8 -0
- package/src/core/user/tld.ts +8 -4
- package/src/generated/version.ts +1 -1
- package/src/index.ts +1 -0
- package/src/plugins/destinations/destination.ts +85 -0
- package/src/plugins/destinations/google-tag-manager.ts +96 -0
- package/src/plugins/destinations/index.ts +19 -0
- package/src/plugins/destinations/types.ts +8 -0
package/src/index.ts
CHANGED
|
@@ -9,5 +9,6 @@ export * from './core/user'
|
|
|
9
9
|
|
|
10
10
|
export type { HtEventsSnippet } from './browser/standalone-interface'
|
|
11
11
|
export type { MiddlewareFunction } from './plugins/middleware'
|
|
12
|
+
export { Destination } from './plugins/destinations'
|
|
12
13
|
export { getGlobalAnalytics } from './lib/global-analytics-helper'
|
|
13
14
|
export { UniversalStorage, Store, StorageObject } from './core/storage'
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import type { DestinationPlugin, Plugin } from '../../core/plugin'
|
|
2
|
+
import { Context, ContextCancelation } from '../../core/context'
|
|
3
|
+
import {
|
|
4
|
+
applyDestinationMiddleware,
|
|
5
|
+
DestinationMiddlewareFunction,
|
|
6
|
+
} from '../middleware'
|
|
7
|
+
|
|
8
|
+
// removes the return type from a function
|
|
9
|
+
type NoReturn<T> = T extends (...args: any[]) => any
|
|
10
|
+
? (...args: Parameters<T>) => void | Promise<void>
|
|
11
|
+
: T
|
|
12
|
+
|
|
13
|
+
type PluginActions = Pick<
|
|
14
|
+
Plugin,
|
|
15
|
+
'alias' | 'group' | 'identify' | 'page' | 'screen' | 'track'
|
|
16
|
+
>
|
|
17
|
+
|
|
18
|
+
type DestinationActions = {
|
|
19
|
+
[K in keyof PluginActions]: NoReturn<PluginActions[K]>
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Convenience class for writing 3rd party destination plugins
|
|
24
|
+
*/
|
|
25
|
+
export class Destination implements DestinationPlugin {
|
|
26
|
+
readonly type = 'destination'
|
|
27
|
+
readonly middleware: DestinationMiddlewareFunction[] = []
|
|
28
|
+
|
|
29
|
+
constructor(
|
|
30
|
+
readonly name: string,
|
|
31
|
+
readonly version: string,
|
|
32
|
+
readonly actions: DestinationActions
|
|
33
|
+
) {}
|
|
34
|
+
|
|
35
|
+
isLoaded() {
|
|
36
|
+
return true
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
load() {
|
|
40
|
+
console.debug(`loaded destination plugin: ${this.name} v${this.version}`)
|
|
41
|
+
return Promise.resolve()
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
addMiddleware(...fn: DestinationMiddlewareFunction[]) {
|
|
45
|
+
this.middleware.push(...fn)
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
alias = this._createMethod('alias')
|
|
49
|
+
group = this._createMethod('group')
|
|
50
|
+
identify = this._createMethod('identify')
|
|
51
|
+
page = this._createMethod('page')
|
|
52
|
+
screen = this._createMethod('screen')
|
|
53
|
+
track = this._createMethod('track')
|
|
54
|
+
|
|
55
|
+
private async transform(ctx: Context): Promise<Context> {
|
|
56
|
+
const modifiedEvent = await applyDestinationMiddleware(
|
|
57
|
+
this.name,
|
|
58
|
+
ctx.event,
|
|
59
|
+
this.middleware
|
|
60
|
+
)
|
|
61
|
+
|
|
62
|
+
if (modifiedEvent == null) {
|
|
63
|
+
ctx.cancel(
|
|
64
|
+
new ContextCancelation({
|
|
65
|
+
retry: false,
|
|
66
|
+
reason: 'dropped by destination middleware',
|
|
67
|
+
})
|
|
68
|
+
)
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return new Context(modifiedEvent)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
private _createMethod(action: keyof DestinationActions) {
|
|
75
|
+
return async (ctx: Context): Promise<Context> => {
|
|
76
|
+
if (!this.actions[action]) return ctx
|
|
77
|
+
|
|
78
|
+
const transformedCtx = await this.transform(ctx)
|
|
79
|
+
|
|
80
|
+
await this.actions[action]!(transformedCtx)
|
|
81
|
+
|
|
82
|
+
return ctx
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import type { Context } from '../../core/context'
|
|
2
|
+
import type { DestinationFactory } from './types'
|
|
3
|
+
import { Destination } from './destination'
|
|
4
|
+
|
|
5
|
+
declare global {
|
|
6
|
+
interface Window {
|
|
7
|
+
gtag: Function
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
type GoogleTagManagerSettings = {
|
|
12
|
+
/**
|
|
13
|
+
* The Google measurement ID(s) to send events to (GA4, Ads)
|
|
14
|
+
*/
|
|
15
|
+
measurementId?: string | string[]
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* If a `Viewed Page` event should be sent for all `htevents.page` calls
|
|
19
|
+
*/
|
|
20
|
+
trackAllPages?: boolean
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* If a `Viewed <name> Page` event should be sent for `htevents.page('Name')` calls
|
|
24
|
+
*/
|
|
25
|
+
trackNamedPages?: boolean
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* If a `Viewed <category> <name> Page` event should be sent for `htevents.page('Category', 'Name')` calls
|
|
29
|
+
*/
|
|
30
|
+
trackCategorizedPages?: boolean
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* https://github.com/segmentio/analytics.js-integrations/blob/master/integrations/google-tag-manager/lib/index.js
|
|
35
|
+
*/
|
|
36
|
+
const googleTagManager: DestinationFactory<GoogleTagManagerSettings> = ({
|
|
37
|
+
measurementId = [],
|
|
38
|
+
trackAllPages = false,
|
|
39
|
+
trackNamedPages = true,
|
|
40
|
+
trackCategorizedPages = true,
|
|
41
|
+
}) => {
|
|
42
|
+
const measurementIds = (
|
|
43
|
+
Array.isArray(measurementId) ? measurementId : [measurementId]
|
|
44
|
+
).filter(Boolean)
|
|
45
|
+
|
|
46
|
+
const baseEvent = ({ event }: Context) => {
|
|
47
|
+
return {
|
|
48
|
+
...(event.userId && {
|
|
49
|
+
user_id: event.userId,
|
|
50
|
+
}),
|
|
51
|
+
...(event.anonymousId && {
|
|
52
|
+
hightouch_anonymous_id: event.anonymousId,
|
|
53
|
+
}),
|
|
54
|
+
...(measurementIds.length > 0 && {
|
|
55
|
+
send_to: measurementIds,
|
|
56
|
+
}),
|
|
57
|
+
...event.properties,
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return new Destination('Google Tag Manager', '0.0.1', {
|
|
62
|
+
identify: (ctx) => {
|
|
63
|
+
if (ctx.event.userId) {
|
|
64
|
+
measurementIds.forEach((measurementId) => {
|
|
65
|
+
window.gtag('config', measurementId, {
|
|
66
|
+
user_id: ctx.event.userId,
|
|
67
|
+
})
|
|
68
|
+
})
|
|
69
|
+
}
|
|
70
|
+
},
|
|
71
|
+
|
|
72
|
+
page: (ctx) => {
|
|
73
|
+
if (
|
|
74
|
+
trackAllPages ||
|
|
75
|
+
(trackNamedPages && ctx.event.name) ||
|
|
76
|
+
(trackCategorizedPages && ctx.event.category)
|
|
77
|
+
) {
|
|
78
|
+
const eventName = ['Viewed', ctx.event.category, ctx.event.name, 'Page']
|
|
79
|
+
.filter(Boolean)
|
|
80
|
+
.join(' ')
|
|
81
|
+
|
|
82
|
+
window.gtag('event', eventName, {
|
|
83
|
+
...baseEvent(ctx),
|
|
84
|
+
})
|
|
85
|
+
}
|
|
86
|
+
},
|
|
87
|
+
|
|
88
|
+
track: (ctx) => {
|
|
89
|
+
window.gtag('event', ctx.event.event, {
|
|
90
|
+
...baseEvent(ctx),
|
|
91
|
+
})
|
|
92
|
+
},
|
|
93
|
+
})
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export default googleTagManager
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { DestinationSettings } from './types'
|
|
2
|
+
import { Destination } from './destination'
|
|
3
|
+
|
|
4
|
+
export { Destination }
|
|
5
|
+
export type { DestinationSettings }
|
|
6
|
+
|
|
7
|
+
export async function createDestination(
|
|
8
|
+
name: string,
|
|
9
|
+
settings: DestinationSettings
|
|
10
|
+
): Promise<Destination | undefined> {
|
|
11
|
+
switch (name) {
|
|
12
|
+
case 'Google Tag Manager':
|
|
13
|
+
return import(
|
|
14
|
+
/* webpackChunkName: "google-tag-manager" */ './google-tag-manager'
|
|
15
|
+
).then((mod) => mod.default(settings as any))
|
|
16
|
+
default:
|
|
17
|
+
return undefined
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { JSONObject } from '@ht-sdks/events-sdk-js-core'
|
|
2
|
+
import type { Destination } from './destination'
|
|
3
|
+
|
|
4
|
+
export type DestinationSettings = JSONObject
|
|
5
|
+
|
|
6
|
+
export type DestinationFactory<TSettings extends DestinationSettings> = (
|
|
7
|
+
settings: TSettings
|
|
8
|
+
) => Destination
|