@ht-sdks/events-sdk-js-browser 1.3.0 → 1.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.
Files changed (38) hide show
  1. package/README.md +62 -17
  2. package/dist/cjs/generated/version.js +1 -1
  3. package/dist/cjs/plugins/destinations/google-tag-manager.js +8 -21
  4. package/dist/cjs/plugins/destinations/google-tag-manager.js.map +1 -1
  5. package/dist/cjs/plugins/destinations/gtag.js +44 -0
  6. package/dist/cjs/plugins/destinations/gtag.js.map +1 -0
  7. package/dist/cjs/plugins/destinations/index.js +4 -0
  8. package/dist/cjs/plugins/destinations/index.js.map +1 -1
  9. package/dist/pkg/generated/version.js +1 -1
  10. package/dist/pkg/plugins/destinations/google-tag-manager.js +8 -21
  11. package/dist/pkg/plugins/destinations/google-tag-manager.js.map +1 -1
  12. package/dist/pkg/plugins/destinations/gtag.js +42 -0
  13. package/dist/pkg/plugins/destinations/gtag.js.map +1 -0
  14. package/dist/pkg/plugins/destinations/index.js +4 -0
  15. package/dist/pkg/plugins/destinations/index.js.map +1 -1
  16. package/dist/types/core/buffer/index.d.ts +1 -1
  17. package/dist/types/generated/version.d.ts +1 -1
  18. package/dist/types/plugins/destinations/google-tag-manager.d.ts +1 -5
  19. package/dist/types/plugins/destinations/google-tag-manager.d.ts.map +1 -1
  20. package/dist/types/plugins/destinations/gtag.d.ts +27 -0
  21. package/dist/types/plugins/destinations/gtag.d.ts.map +1 -0
  22. package/dist/types/plugins/destinations/index.d.ts.map +1 -1
  23. package/dist/umd/events.min.js +1 -1
  24. package/dist/umd/events.min.js.map +1 -1
  25. package/dist/umd/google-tag-manager.bundle.238ad1d40bdf04d984ef.js +2 -0
  26. package/dist/umd/google-tag-manager.bundle.238ad1d40bdf04d984ef.js.map +1 -0
  27. package/dist/umd/gtag.bundle.a5e7c472f1c04f2b9ebd.js +2 -0
  28. package/dist/umd/gtag.bundle.a5e7c472f1c04f2b9ebd.js.map +1 -0
  29. package/dist/umd/index.js +1 -1
  30. package/dist/umd/index.js.map +1 -1
  31. package/package.json +1 -1
  32. package/src/core/http-cookies/server-examples/node-aws-lambda.md +1 -1
  33. package/src/generated/version.ts +1 -1
  34. package/src/plugins/destinations/google-tag-manager.ts +15 -36
  35. package/src/plugins/destinations/gtag.ts +93 -0
  36. package/src/plugins/destinations/index.ts +4 -0
  37. package/dist/umd/google-tag-manager.bundle.c27daab560c3298c04ca.js +0 -2
  38. package/dist/umd/google-tag-manager.bundle.c27daab560c3298c04ca.js.map +0 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ht-sdks/events-sdk-js-browser",
3
- "version": "1.3.0",
3
+ "version": "1.3.1",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/ht-sdks/events-sdk-js-mono",
@@ -86,7 +86,7 @@ export const handler = async (event, context) => {
86
86
  };
87
87
  ```
88
88
 
89
- After creating the above Lambda Function, you'll need to setup an [API Gateway](https://aws.amazon.com/api-gateway/). Specifically, create a new `Route` and input the value of `/ht/{proxy}`. Then attach an `integration` for this route, and select your Lambda. Your HTTPCookieService lambda will now be callable over HTTP at `${ApiGatewayURL}.com/default/ht/renew`.
89
+ After creating the above Lambda Function, you'll need to setup an [API Gateway](https://aws.amazon.com/api-gateway/). Specifically, create a new `Route` and input the value of `/ht/{proxy}`. Then attach an `integration` for this route, and select your Lambda. Be sure to select the "version 2" payload format. Your HTTPCookieService lambda will now be reachable via HTTP at `${ApiGatewayURL}.com/default/ht/renew`.
90
90
 
91
91
  However, the HTTPCookieService must live on the same domain and IP address as your website's HTML document.
92
92
 
@@ -1,2 +1,2 @@
1
1
  // This file is generated.
2
- export const version = '1.3.0'
2
+ export const version = '1.3.1'
@@ -1,19 +1,15 @@
1
1
  import type { Context } from '../../core/context'
2
+ import type { HightouchEvent } from '../../core/events/interfaces'
2
3
  import type { DestinationFactory } from './types'
3
4
  import { Destination } from './destination'
4
5
 
5
6
  declare global {
6
7
  interface Window {
7
- gtag: Function
8
+ dataLayer: Array<any>
8
9
  }
9
10
  }
10
11
 
11
12
  type GoogleTagManagerSettings = {
12
- /**
13
- * The Google measurement ID(s) to send events to (GA4, Ads)
14
- */
15
- measurementId?: string | string[]
16
-
17
13
  /**
18
14
  * If a `Viewed Page` event should be sent for all `htevents.page` calls
19
15
  */
@@ -34,42 +30,25 @@ type GoogleTagManagerSettings = {
34
30
  * https://github.com/segmentio/analytics.js-integrations/blob/master/integrations/google-tag-manager/lib/index.js
35
31
  */
36
32
  const googleTagManager: DestinationFactory<GoogleTagManagerSettings> = ({
37
- measurementId = [],
38
33
  trackAllPages = false,
39
34
  trackNamedPages = true,
40
35
  trackCategorizedPages = true,
41
36
  }) => {
42
- const measurementIds = (
43
- Array.isArray(measurementId) ? measurementId : [measurementId]
44
- ).filter(Boolean)
45
-
46
- const baseEvent = ({ event }: Context) => {
47
- return {
37
+ const pushEvent = (event: HightouchEvent) => {
38
+ window.dataLayer.push({
48
39
  ...(event.userId && {
49
- user_id: event.userId,
40
+ userId: event.userId,
50
41
  }),
51
42
  ...(event.anonymousId && {
52
- hightouch_anonymous_id: event.anonymousId,
53
- }),
54
- ...(measurementIds.length > 0 && {
55
- send_to: measurementIds,
43
+ hightouchAnonymousId: event.anonymousId,
56
44
  }),
45
+ event: event.event,
57
46
  ...event.properties,
58
- }
47
+ })
59
48
  }
60
49
 
61
50
  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) => {
51
+ page: (ctx: Context) => {
73
52
  if (
74
53
  trackAllPages ||
75
54
  (trackNamedPages && ctx.event.name) ||
@@ -79,16 +58,16 @@ const googleTagManager: DestinationFactory<GoogleTagManagerSettings> = ({
79
58
  .filter(Boolean)
80
59
  .join(' ')
81
60
 
82
- window.gtag('event', eventName, {
83
- ...baseEvent(ctx),
61
+ pushEvent({
62
+ ...ctx.event,
63
+ type: 'track',
64
+ event: eventName,
84
65
  })
85
66
  }
86
67
  },
87
68
 
88
- track: (ctx) => {
89
- window.gtag('event', ctx.event.event, {
90
- ...baseEvent(ctx),
91
- })
69
+ track: (ctx: Context) => {
70
+ pushEvent(ctx.event)
92
71
  },
93
72
  })
94
73
  }
@@ -0,0 +1,93 @@
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 GtagSettings = {
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
+ const gtag: DestinationFactory<GtagSettings> = ({
34
+ measurementId = [],
35
+ trackAllPages = false,
36
+ trackNamedPages = true,
37
+ trackCategorizedPages = true,
38
+ }) => {
39
+ const measurementIds = (
40
+ Array.isArray(measurementId) ? measurementId : [measurementId]
41
+ ).filter(Boolean)
42
+
43
+ const baseEvent = ({ event }: Context) => {
44
+ return {
45
+ ...(event.userId && {
46
+ user_id: event.userId,
47
+ }),
48
+ ...(event.anonymousId && {
49
+ hightouch_anonymous_id: event.anonymousId,
50
+ }),
51
+ ...(measurementIds.length > 0 && {
52
+ send_to: measurementIds,
53
+ }),
54
+ ...event.properties,
55
+ }
56
+ }
57
+
58
+ return new Destination('gtag', '0.0.1', {
59
+ identify: (ctx) => {
60
+ if (ctx.event.userId) {
61
+ measurementIds.forEach((measurementId) => {
62
+ window.gtag('config', measurementId, {
63
+ user_id: ctx.event.userId,
64
+ })
65
+ })
66
+ }
67
+ },
68
+
69
+ page: (ctx) => {
70
+ if (
71
+ trackAllPages ||
72
+ (trackNamedPages && ctx.event.name) ||
73
+ (trackCategorizedPages && ctx.event.category)
74
+ ) {
75
+ const eventName = ['Viewed', ctx.event.category, ctx.event.name, 'Page']
76
+ .filter(Boolean)
77
+ .join(' ')
78
+
79
+ window.gtag('event', eventName, {
80
+ ...baseEvent(ctx),
81
+ })
82
+ }
83
+ },
84
+
85
+ track: (ctx) => {
86
+ window.gtag('event', ctx.event.event, {
87
+ ...baseEvent(ctx),
88
+ })
89
+ },
90
+ })
91
+ }
92
+
93
+ export default gtag
@@ -9,6 +9,10 @@ export async function createDestination(
9
9
  settings: DestinationSettings
10
10
  ): Promise<Destination | undefined> {
11
11
  switch (name) {
12
+ case 'gtag':
13
+ return import(/* webpackChunkName: "gtag" */ './gtag').then((mod) =>
14
+ mod.default(settings as any)
15
+ )
12
16
  case 'Google Tag Manager':
13
17
  return import(
14
18
  /* webpackChunkName: "google-tag-manager" */ './google-tag-manager'
@@ -1,2 +0,0 @@
1
- "use strict";(self.webpackChunk_ht_sdks_events_sdk_js_browser=self.webpackChunk_ht_sdks_events_sdk_js_browser||[]).push([[579],{4557:function(e,t,n){n.r(t),n.d(t,{default:function(){return d}});var i=n(5163),r=n(1494),o=n(3710),s=n(6338),a=function(){function e(e,t,n){this.name=e,this.version=t,this.actions=n,this.type="destination",this.middleware=[],this.alias=this._createMethod("alias"),this.group=this._createMethod("group"),this.identify=this._createMethod("identify"),this.page=this._createMethod("page"),this.screen=this._createMethod("screen"),this.track=this._createMethod("track")}return e.prototype.isLoaded=function(){return!0},e.prototype.load=function(){return console.debug("loaded destination plugin: ".concat(this.name," v").concat(this.version)),Promise.resolve()},e.prototype.addMiddleware=function(){for(var e,t=[],n=0;n<arguments.length;n++)t[n]=arguments[n];(e=this.middleware).push.apply(e,t)},e.prototype.transform=function(e){return(0,i.mG)(this,void 0,Promise,(function(){var t;return(0,i.Jh)(this,(function(n){switch(n.label){case 0:return[4,(0,s.applyDestinationMiddleware)(this.name,e.event,this.middleware)];case 1:return null==(t=n.sent())&&e.cancel(new r.Y({retry:!1,reason:"dropped by destination middleware"})),[2,new o._(t)]}}))}))},e.prototype._createMethod=function(e){var t=this;return function(n){return(0,i.mG)(t,void 0,Promise,(function(){var t;return(0,i.Jh)(this,(function(i){switch(i.label){case 0:return this.actions[e]?[4,this.transform(n)]:[2,n];case 1:return t=i.sent(),[4,this.actions[e](t)];case 2:return i.sent(),[2,n]}}))}))}},e}(),d=function(e){var t=e.measurementId,n=void 0===t?[]:t,r=e.trackAllPages,o=void 0!==r&&r,s=e.trackNamedPages,d=void 0===s||s,c=e.trackCategorizedPages,u=void 0===c||c,h=(Array.isArray(n)?n:[n]).filter(Boolean),p=function(e){var t=e.event;return(0,i.pi)((0,i.pi)((0,i.pi)((0,i.pi)({},t.userId&&{user_id:t.userId}),t.anonymousId&&{hightouch_anonymous_id:t.anonymousId}),h.length>0&&{send_to:h}),t.properties)};return new a("Google Tag Manager","0.0.1",{identify:function(e){e.event.userId&&h.forEach((function(t){window.gtag("config",t,{user_id:e.event.userId})}))},page:function(e){if(o||d&&e.event.name||u&&e.event.category){var t=["Viewed",e.event.category,e.event.name,"Page"].filter(Boolean).join(" ");window.gtag("event",t,(0,i.pi)({},p(e)))}},track:function(e){window.gtag("event",e.event.event,(0,i.pi)({},p(e)))}})}}}]);
2
- //# sourceMappingURL=google-tag-manager.bundle.c27daab560c3298c04ca.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"google-tag-manager.bundle.c27daab560c3298c04ca.js","mappings":"8OAwBA,aAIE,WACWA,EACAC,EACAC,GAFA,KAAAF,KAAAA,EACA,KAAAC,QAAAA,EACA,KAAAC,QAAAA,EANF,KAAAC,KAAO,cACP,KAAAC,WAA8C,GAqBvD,KAAAC,MAAQC,KAAKC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EAAMiB,QAAU,CAClBC,QAASlB,EAAMiB,SAEbjB,EAAMmB,aAAe,CACvBC,uBAAwBpB,EAAMmB,cAE5BR,EAAeU,OAAS,GAAK,CAC/BC,QAASX,IAERX,EAAMuB,WAEb,EAEA,OAAO,IAAIC,EAAY,qBAAsB,QAAS,CACpDxC,SAAU,SAACc,GACLA,EAAIE,MAAMiB,QACZN,EAAec,SAAQ,SAAClB,GACtBmB,OAAOC,KAAK,SAAUpB,EAAe,CACnCW,QAASpB,EAAIE,MAAMiB,QAEvB,GAEJ,EAEAhC,KAAM,SAACa,GACL,GACEU,GACCC,GAAmBX,EAAIE,MAAMzB,MAC7BmC,GAAyBZ,EAAIE,MAAM4B,SACpC,CACA,IAAMC,EAAY,CAAC,SAAU/B,EAAIE,MAAM4B,SAAU9B,EAAIE,MAAMzB,KAAM,QAC9DuC,OAAOC,SACPe,KAAK,KAERJ,OAAOC,KAAK,QAASE,GAAW,WAC3Bb,EAAUlB,I,CAGnB,EAEAX,MAAO,SAACW,GACN4B,OAAOC,KAAK,QAAS7B,EAAIE,MAAMA,OAAO,WACjCgB,EAAUlB,IAEjB,GAEJ,C","sources":["webpack://@ht-sdks/events-sdk-js-browser/./src/plugins/destinations/destination.ts","webpack://@ht-sdks/events-sdk-js-browser/./src/plugins/destinations/google-tag-manager.ts"],"sourcesContent":["import type { DestinationPlugin, Plugin } from '../../core/plugin'\nimport { Context, ContextCancelation } from '../../core/context'\nimport {\n applyDestinationMiddleware,\n DestinationMiddlewareFunction,\n} from '../middleware'\n\n// removes the return type from a function\ntype NoReturn<T> = T extends (...args: any[]) => any\n ? (...args: Parameters<T>) => void | Promise<void>\n : T\n\ntype PluginActions = Pick<\n Plugin,\n 'alias' | 'group' | 'identify' | 'page' | 'screen' | 'track'\n>\n\ntype DestinationActions = {\n [K in keyof PluginActions]: NoReturn<PluginActions[K]>\n}\n\n/**\n * Convenience class for writing 3rd party destination plugins\n */\nexport class Destination implements DestinationPlugin {\n readonly type = 'destination'\n readonly middleware: DestinationMiddlewareFunction[] = []\n\n constructor(\n readonly name: string,\n readonly version: string,\n readonly actions: DestinationActions\n ) {}\n\n isLoaded() {\n return true\n }\n\n load() {\n console.debug(`loaded destination plugin: ${this.name} v${this.version}`)\n return Promise.resolve()\n }\n\n addMiddleware(...fn: DestinationMiddlewareFunction[]) {\n this.middleware.push(...fn)\n }\n\n alias = this._createMethod('alias')\n group = this._createMethod('group')\n identify = this._createMethod('identify')\n page = this._createMethod('page')\n screen = this._createMethod('screen')\n track = this._createMethod('track')\n\n private async transform(ctx: Context): Promise<Context> {\n const modifiedEvent = await applyDestinationMiddleware(\n this.name,\n ctx.event,\n this.middleware\n )\n\n if (modifiedEvent == null) {\n ctx.cancel(\n new ContextCancelation({\n retry: false,\n reason: 'dropped by destination middleware',\n })\n )\n }\n\n return new Context(modifiedEvent)\n }\n\n private _createMethod(action: keyof DestinationActions) {\n return async (ctx: Context): Promise<Context> => {\n if (!this.actions[action]) return ctx\n\n const transformedCtx = await this.transform(ctx)\n\n await this.actions[action]!(transformedCtx)\n\n return ctx\n }\n }\n}\n","import type { Context } from '../../core/context'\nimport type { DestinationFactory } from './types'\nimport { Destination } from './destination'\n\ndeclare global {\n interface Window {\n gtag: Function\n }\n}\n\ntype GoogleTagManagerSettings = {\n /**\n * The Google measurement ID(s) to send events to (GA4, Ads)\n */\n measurementId?: string | string[]\n\n /**\n * If a `Viewed Page` event should be sent for all `htevents.page` calls\n */\n trackAllPages?: boolean\n\n /**\n * If a `Viewed <name> Page` event should be sent for `htevents.page('Name')` calls\n */\n trackNamedPages?: boolean\n\n /**\n * If a `Viewed <category> <name> Page` event should be sent for `htevents.page('Category', 'Name')` calls\n */\n trackCategorizedPages?: boolean\n}\n\n/**\n * https://github.com/segmentio/analytics.js-integrations/blob/master/integrations/google-tag-manager/lib/index.js\n */\nconst googleTagManager: DestinationFactory<GoogleTagManagerSettings> = ({\n measurementId = [],\n trackAllPages = false,\n trackNamedPages = true,\n trackCategorizedPages = true,\n}) => {\n const measurementIds = (\n Array.isArray(measurementId) ? measurementId : [measurementId]\n ).filter(Boolean)\n\n const baseEvent = ({ event }: Context) => {\n return {\n ...(event.userId && {\n user_id: event.userId,\n }),\n ...(event.anonymousId && {\n hightouch_anonymous_id: event.anonymousId,\n }),\n ...(measurementIds.length > 0 && {\n send_to: measurementIds,\n }),\n ...event.properties,\n }\n }\n\n return new Destination('Google Tag Manager', '0.0.1', {\n identify: (ctx) => {\n if (ctx.event.userId) {\n measurementIds.forEach((measurementId) => {\n window.gtag('config', measurementId, {\n user_id: ctx.event.userId,\n })\n })\n }\n },\n\n page: (ctx) => {\n if (\n trackAllPages ||\n (trackNamedPages && ctx.event.name) ||\n (trackCategorizedPages && ctx.event.category)\n ) {\n const eventName = ['Viewed', ctx.event.category, ctx.event.name, 'Page']\n .filter(Boolean)\n .join(' ')\n\n window.gtag('event', eventName, {\n ...baseEvent(ctx),\n })\n }\n },\n\n track: (ctx) => {\n window.gtag('event', ctx.event.event, {\n ...baseEvent(ctx),\n })\n },\n })\n}\n\nexport default googleTagManager\n"],"names":["name","version","actions","type","middleware","alias","this","_createMethod","group","identify","page","screen","track","isLoaded","load","console","debug","Promise","resolve","addMiddleware","push","fn","transform","ctx","applyDestinationMiddleware","event","modifiedEvent","cancel","retry","reason","action","transformedCtx","measurementId","trackAllPages","trackNamedPages","trackCategorizedPages","measurementIds","Array","isArray","filter","Boolean","baseEvent","userId","user_id","anonymousId","hightouch_anonymous_id","length","send_to","properties","Destination","forEach","window","gtag","category","eventName","join"],"sourceRoot":""}