@backstage-community/plugin-analytics-module-ga4 0.18.0 → 0.19.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/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # @backstage-community/plugin-analytics-module-ga4
2
2
 
3
+ ## 0.19.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 3fee6eb: Backstage version bump to v1.50.3
8
+
3
9
  ## 0.18.0
4
10
 
5
11
  ### Minor Changes
@@ -1 +1 @@
1
- {"version":3,"file":"GoogleAnalytics4.esm.js","sources":["../../../../src/apis/implementations/AnalyticsApi/GoogleAnalytics4.ts"],"sourcesContent":["/*\n * Copyright 2023 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport ReactGA from 'react-ga4';\nimport {\n AnalyticsApi,\n AnalyticsContextValue,\n AnalyticsEventAttributes,\n AnalyticsEvent,\n IdentityApi,\n} from '@backstage/core-plugin-api';\nimport {\n AnalyticsApi as NewAnalyticsApi,\n AnalyticsEvent as NewAnalyticsEvent,\n AnalyticsContextValue as NewAnalyticsContextValue,\n} from '@backstage/frontend-plugin-api';\nimport { Config } from '@backstage/config';\nimport { DeferredCapture } from '../../../util/DeferredCapture';\n\n/**\n * Google Analytics API provider for the Backstage Analytics API.\n * @public\n */\nexport class GoogleAnalytics4 implements AnalyticsApi, NewAnalyticsApi {\n private readonly customUserIdTransform?: (\n userEntityRef: string,\n ) => Promise<string>;\n private readonly capture: DeferredCapture;\n private readonly contentGroupBy?: string;\n private readonly allowedContexts?: string[];\n private readonly allowedAttributes?: string[];\n /**\n * Instantiate the implementation and initialize ReactGA.\n * @param options - initializes Google Analytics module with the config\n */\n private constructor(options: {\n identityApi?: IdentityApi;\n userIdTransform?: 'sha-256' | ((userEntityRef: string) => Promise<string>);\n identity: string;\n measurementId: string;\n testMode: boolean;\n debug: boolean;\n cookieFlags: string;\n enableSendPageView: boolean;\n contentGroupBy?: string;\n allowedContexts?: string[];\n allowedAttributes?: string[];\n }) {\n const {\n identity,\n measurementId,\n identityApi,\n userIdTransform = 'sha-256',\n testMode,\n debug,\n cookieFlags,\n enableSendPageView,\n contentGroupBy,\n allowedContexts,\n allowedAttributes,\n } = options;\n // Initialize Google Analytics.\n ReactGA.initialize(measurementId, {\n testMode,\n gaOptions: {\n cookieFlags: cookieFlags,\n debug_mode: debug,\n },\n gtagOptions: {\n cookie_flags: cookieFlags,\n debug_mode: debug,\n send_page_view: enableSendPageView,\n },\n });\n\n this.contentGroupBy = contentGroupBy;\n this.allowedAttributes = allowedAttributes;\n this.allowedContexts = allowedContexts;\n // If identity is required, defer event capture until identity is known.\n this.capture = new DeferredCapture({ defer: identity === 'required' });\n\n // Allow custom userId transformation.\n this.customUserIdTransform =\n typeof userIdTransform === 'function' ? userIdTransform : undefined;\n\n // Capture user only when explicitly enabled and provided.\n if (identity !== 'disabled') {\n if (identityApi) {\n this.setUserFrom(identityApi).then(() => {\n return;\n });\n }\n }\n }\n\n /**\n * Instantiate a fully configured GA Analytics API implementation.\n * @param config - Config object from app config\n * @param options - options with identityApi and userIdTransform config\n */\n static fromConfig(\n config: Config,\n options: {\n identityApi?: IdentityApi;\n userIdTransform?:\n | 'sha-256'\n | ((userEntityRef: string) => Promise<string>);\n } = {},\n ) {\n // Get all necessary configuration.\n const measurementId = config.getString('app.analytics.ga4.measurementId');\n const identity =\n config.getOptionalString('app.analytics.ga4.identity') || 'disabled';\n const debug = config.getOptionalBoolean('app.analytics.ga4.debug') ?? false;\n const cookieFlags =\n config.getOptionalString('app.analytics.ga4.cookieFlags') ??\n 'SameSite=Lax';\n const enableSendPageView =\n config.getOptionalBoolean('app.analytics.ga4.enableSendPageView') ??\n false;\n const testMode =\n config.getOptionalBoolean('app.analytics.ga4.testMode') ?? false;\n\n const contentGroupBy = config.getOptionalString(\n 'app.analytics.ga4.contentGrouping',\n );\n const allowedContexts = config.getOptionalStringArray(\n 'app.analytics.ga4.allowedContexts',\n );\n const allowedAttributes = config.getOptionalStringArray(\n 'app.analytics.ga4.allowedAttributes',\n );\n\n if (identity === 'required' && !options.identityApi) {\n throw new Error(\n 'Invalid config: identity API must be provided to deps when ga4.identity is required',\n );\n }\n\n // Return an implementation instance.\n return new GoogleAnalytics4({\n ...options,\n identity,\n measurementId: measurementId,\n testMode,\n debug,\n cookieFlags,\n enableSendPageView,\n contentGroupBy,\n allowedContexts,\n allowedAttributes,\n });\n }\n\n /**\n * Primary event capture implementation. Handles core navigate event as a\n * pageview and the rest as custom events. All custom dimensions/metrics are\n * applied as they should be (set on pageview, merged object on events).\n * @param event - AnalyticsEvent type captured\n */\n captureEvent(event: AnalyticsEvent | NewAnalyticsEvent) {\n const { context, action, subject, value, attributes } = event;\n const customEventData = this.setEventParameters(context, attributes);\n if (this.contentGroupBy) {\n customEventData.content_group = context[this.contentGroupBy]!;\n }\n\n const extensionId = context.extensionId || context.extension;\n const category = extensionId ? String(extensionId) : 'App';\n\n // The legacy default extension was 'App' and the new one is 'app'\n if (\n action === 'navigate' &&\n category.toLocaleLowerCase('en-US').startsWith('app')\n ) {\n this.capture.event(\n {\n category,\n action: 'page_view',\n label: subject,\n value,\n },\n customEventData,\n );\n return;\n }\n\n if (action === 'search') {\n customEventData.search_term = subject;\n }\n\n this.capture.event(\n {\n category,\n action,\n label: subject,\n value,\n },\n customEventData,\n );\n }\n\n /**\n * Returns an object of dimensions/metrics given an Analytics Context and an\n * Event Attributes, e.g. `{ c_pluginId: \"some value\", a_attribute1: 42 }`\n * @param context - analytics context object\n * @param attributes- additional analytics event attributes\n */\n private setEventParameters(\n context: AnalyticsContextValue | NewAnalyticsContextValue,\n attributes: AnalyticsEventAttributes = {},\n ) {\n const customEventParameters: {\n [x: string]: string | number | boolean | undefined;\n } = {};\n\n const contextKeys =\n this.allowedContexts?.join('') === '*'\n ? Object.keys(context)\n : this.allowedContexts;\n\n contextKeys?.forEach(ctx => {\n if (\n context[ctx] !== undefined &&\n context[ctx] !== null &&\n context[ctx] !== ''\n ) {\n customEventParameters[`c_${ctx}`] = context[ctx];\n }\n });\n\n const attrKeys =\n this.allowedAttributes?.join('') === '*'\n ? Object.keys(attributes)\n : this.allowedAttributes;\n attrKeys?.forEach(attr => {\n if (\n attributes[attr] !== undefined &&\n attributes[attr] !== null &&\n attributes[attr] !== ''\n ) {\n customEventParameters[`a_${attr}`] = attributes[attr];\n }\n });\n\n return customEventParameters;\n }\n\n /**\n * Sets the GA userId, based on the `userEntityRef` set on the backstage\n * identity loaded from a given Backstage Identity API instance. Because\n * Google forbids sending any PII (including on the userId field), we hash\n * the entire `userEntityRef` on behalf of integrators:\n *\n * - With value `User:default/name`, userId becomes `sha256(User:default/name)`\n *\n * If an integrator wishes to use an alternative hashing mechanism or an\n * entirely different value, they may do so by passing a `userIdTransform`\n * function alongside the `identityApi` to `GoogleAnalytics.fromConfig()`.\n * This function receives the `userEntityRef` as an argument and should\n * resolve to a hashed version of whatever identifier they choose.\n *\n * Note: this feature requires that an integrator has set up a Google\n * Analytics User ID view in the property used to track Backstage.\n * @param identityApi - IdentityApi object\n */\n private async setUserFrom(identityApi: IdentityApi) {\n const { userEntityRef } = await identityApi.getBackstageIdentity();\n\n // Prevent PII from being passed to Google Analytics.\n const userId = await this.getPrivateUserId(userEntityRef);\n\n // Set the user ID.\n ReactGA.set({ user_id: userId });\n\n // Notify the deferred capture mechanism that it may proceed.\n this.capture.setReady();\n }\n\n /**\n * Returns a PII-free (according to Google's terms of service) user ID for\n * use in Google Analytics.\n * @param userEntityRef - user entity as string\n */\n private getPrivateUserId(userEntityRef: string): Promise<string> {\n // Allow integrators to provide their own hashing transformer.\n if (this.customUserIdTransform) {\n return this.customUserIdTransform(userEntityRef);\n }\n\n return this.hash(userEntityRef);\n }\n\n /**\n * Simple hash function; relies on web cryptography + the sha-256 algorithm.\n * @param value - value to be hashed\n */\n private async hash(value: string): Promise<string> {\n const digest = await window.crypto.subtle.digest(\n 'sha-256',\n new TextEncoder().encode(value),\n );\n const hashArray = Array.from(new Uint8Array(digest));\n return hashArray.map(b => b.toString(16).padStart(2, '0')).join('');\n }\n}\n"],"names":[],"mappings":";;;AAoCO,MAAM,gBAA0D,CAAA;AAAA,EACpD,qBAAA;AAAA,EAGA,OAAA;AAAA,EACA,cAAA;AAAA,EACA,eAAA;AAAA,EACA,iBAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAKT,YAAY,OAYjB,EAAA;AACD,IAAM,MAAA;AAAA,MACJ,QAAA;AAAA,MACA,aAAA;AAAA,MACA,WAAA;AAAA,MACA,eAAkB,GAAA,SAAA;AAAA,MAClB,QAAA;AAAA,MACA,KAAA;AAAA,MACA,WAAA;AAAA,MACA,kBAAA;AAAA,MACA,cAAA;AAAA,MACA,eAAA;AAAA,MACA;AAAA,KACE,GAAA,OAAA;AAEJ,IAAA,OAAA,CAAQ,WAAW,aAAe,EAAA;AAAA,MAChC,QAAA;AAAA,MACA,SAAW,EAAA;AAAA,QACT,WAAA;AAAA,QACA,UAAY,EAAA;AAAA,OACd;AAAA,MACA,WAAa,EAAA;AAAA,QACX,YAAc,EAAA,WAAA;AAAA,QACd,UAAY,EAAA,KAAA;AAAA,QACZ,cAAgB,EAAA;AAAA;AAClB,KACD,CAAA;AAED,IAAA,IAAA,CAAK,cAAiB,GAAA,cAAA;AACtB,IAAA,IAAA,CAAK,iBAAoB,GAAA,iBAAA;AACzB,IAAA,IAAA,CAAK,eAAkB,GAAA,eAAA;AAEvB,IAAA,IAAA,CAAK,UAAU,IAAI,eAAA,CAAgB,EAAE,KAAO,EAAA,QAAA,KAAa,YAAY,CAAA;AAGrE,IAAA,IAAA,C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A,MAAc,YAAY,WAA0B,EAAA;AAClD,IAAA,MAAM,EAAE,aAAA,EAAkB,GAAA,MAAM,YAAY,oBAAqB,EAAA;AAGjE,IAAA,MAAM,MAAS,GAAA,MAAM,IAAK,CAAA,gBAAA,CAAiB,aAAa,CAAA;AAGxD,IAAA,OAAA,CAAQ,GAAI,CAAA,EAAE,OAAS,EAAA,MAAA,EAAQ,CAAA;AAG/B,IAAA,IAAA,CAAK,QAAQ,QAAS,EAAA;AAAA;AACxB;AAAA;AAAA;AAAA;AAAA;AAAA,EAOQ,iBAAiB,aAAwC,EAAA;AAE/D,IAAA,IAAI,KAAK,qBAAuB,EAAA;AAC9B,MAAO,OAAA,IAAA,CAAK,sBAAsB,aAAa,CAAA;AAAA;AAGjD,IAAO,OAAA,IAAA,CAAK,KAAK,aAAa,CAAA;AAAA;AAChC;AAAA;AAAA;AAAA;AAAA,EAMA,MAAc,KAAK,KAAgC,EAAA;AACjD,IAAA,MAAM,MAAS,GAAA,MAAM,MAAO,CAAA,MAAA,CAAO,MAAO,CAAA,MAAA;AAAA,MACxC,SAAA;AAAA,MACA,IAAI,WAAA,EAAc,CAAA,MAAA,CAAO,KAAK;AAAA,KAChC;AACA,IAAA,MAAM,YAAY,KAAM,CAAA,IAAA,CAAK,IAAI,UAAA,CAAW,MAAM,CAAC,CAAA;AACnD,IAAA,OAAO,SAAU,CAAA,GAAA,CAAI,CAAK,CAAA,KAAA,CAAA,CAAE,QAAS,CAAA,EAAE,CAAE,CAAA,QAAA,CAAS,CAAG,EAAA,GAAG,CAAC,CAAA,CAAE,KAAK,EAAE,CAAA;AAAA;AAEtE;;;;"}
1
+ {"version":3,"file":"GoogleAnalytics4.esm.js","sources":["../../../../src/apis/implementations/AnalyticsApi/GoogleAnalytics4.ts"],"sourcesContent":["/*\n * Copyright 2023 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport ReactGA from 'react-ga4';\nimport {\n AnalyticsApi,\n AnalyticsContextValue,\n AnalyticsEventAttributes,\n AnalyticsEvent,\n IdentityApi,\n} from '@backstage/core-plugin-api';\nimport {\n AnalyticsApi as NewAnalyticsApi,\n AnalyticsEvent as NewAnalyticsEvent,\n AnalyticsContextValue as NewAnalyticsContextValue,\n} from '@backstage/frontend-plugin-api';\nimport { Config } from '@backstage/config';\nimport { DeferredCapture } from '../../../util/DeferredCapture';\n\n/**\n * Google Analytics API provider for the Backstage Analytics API.\n * @public\n */\nexport class GoogleAnalytics4 implements AnalyticsApi, NewAnalyticsApi {\n private readonly customUserIdTransform?: (\n userEntityRef: string,\n ) => Promise<string>;\n private readonly capture: DeferredCapture;\n private readonly contentGroupBy?: string;\n private readonly allowedContexts?: string[];\n private readonly allowedAttributes?: string[];\n /**\n * Instantiate the implementation and initialize ReactGA.\n * @param options - initializes Google Analytics module with the config\n */\n private constructor(options: {\n identityApi?: IdentityApi;\n userIdTransform?: 'sha-256' | ((userEntityRef: string) => Promise<string>);\n identity: string;\n measurementId: string;\n testMode: boolean;\n debug: boolean;\n cookieFlags: string;\n enableSendPageView: boolean;\n contentGroupBy?: string;\n allowedContexts?: string[];\n allowedAttributes?: string[];\n }) {\n const {\n identity,\n measurementId,\n identityApi,\n userIdTransform = 'sha-256',\n testMode,\n debug,\n cookieFlags,\n enableSendPageView,\n contentGroupBy,\n allowedContexts,\n allowedAttributes,\n } = options;\n // Initialize Google Analytics.\n ReactGA.initialize(measurementId, {\n testMode,\n gaOptions: {\n cookieFlags: cookieFlags,\n debug_mode: debug,\n },\n gtagOptions: {\n cookie_flags: cookieFlags,\n debug_mode: debug,\n send_page_view: enableSendPageView,\n },\n });\n\n this.contentGroupBy = contentGroupBy;\n this.allowedAttributes = allowedAttributes;\n this.allowedContexts = allowedContexts;\n // If identity is required, defer event capture until identity is known.\n this.capture = new DeferredCapture({ defer: identity === 'required' });\n\n // Allow custom userId transformation.\n this.customUserIdTransform =\n typeof userIdTransform === 'function' ? userIdTransform : undefined;\n\n // Capture user only when explicitly enabled and provided.\n if (identity !== 'disabled') {\n if (identityApi) {\n this.setUserFrom(identityApi).then(() => {\n return;\n });\n }\n }\n }\n\n /**\n * Instantiate a fully configured GA Analytics API implementation.\n * @param config - Config object from app config\n * @param options - options with identityApi and userIdTransform config\n */\n static fromConfig(\n config: Config,\n options: {\n identityApi?: IdentityApi;\n userIdTransform?:\n | 'sha-256'\n | ((userEntityRef: string) => Promise<string>);\n } = {},\n ) {\n // Get all necessary configuration.\n const measurementId = config.getString('app.analytics.ga4.measurementId');\n const identity =\n config.getOptionalString('app.analytics.ga4.identity') || 'disabled';\n const debug = config.getOptionalBoolean('app.analytics.ga4.debug') ?? false;\n const cookieFlags =\n config.getOptionalString('app.analytics.ga4.cookieFlags') ??\n 'SameSite=Lax';\n const enableSendPageView =\n config.getOptionalBoolean('app.analytics.ga4.enableSendPageView') ??\n false;\n const testMode =\n config.getOptionalBoolean('app.analytics.ga4.testMode') ?? false;\n\n const contentGroupBy = config.getOptionalString(\n 'app.analytics.ga4.contentGrouping',\n );\n const allowedContexts = config.getOptionalStringArray(\n 'app.analytics.ga4.allowedContexts',\n );\n const allowedAttributes = config.getOptionalStringArray(\n 'app.analytics.ga4.allowedAttributes',\n );\n\n if (identity === 'required' && !options.identityApi) {\n throw new Error(\n 'Invalid config: identity API must be provided to deps when ga4.identity is required',\n );\n }\n\n // Return an implementation instance.\n return new GoogleAnalytics4({\n ...options,\n identity,\n measurementId: measurementId,\n testMode,\n debug,\n cookieFlags,\n enableSendPageView,\n contentGroupBy,\n allowedContexts,\n allowedAttributes,\n });\n }\n\n /**\n * Primary event capture implementation. Handles core navigate event as a\n * pageview and the rest as custom events. All custom dimensions/metrics are\n * applied as they should be (set on pageview, merged object on events).\n * @param event - AnalyticsEvent type captured\n */\n captureEvent(event: AnalyticsEvent | NewAnalyticsEvent) {\n const { context, action, subject, value, attributes } = event;\n const customEventData = this.setEventParameters(context, attributes);\n if (this.contentGroupBy) {\n customEventData.content_group = context[this.contentGroupBy]!;\n }\n\n const extensionId = context.extensionId || context.extension;\n const category = extensionId ? String(extensionId) : 'App';\n\n // The legacy default extension was 'App' and the new one is 'app'\n if (\n action === 'navigate' &&\n category.toLocaleLowerCase('en-US').startsWith('app')\n ) {\n this.capture.event(\n {\n category,\n action: 'page_view',\n label: subject,\n value,\n },\n customEventData,\n );\n return;\n }\n\n if (action === 'search') {\n customEventData.search_term = subject;\n }\n\n this.capture.event(\n {\n category,\n action,\n label: subject,\n value,\n },\n customEventData,\n );\n }\n\n /**\n * Returns an object of dimensions/metrics given an Analytics Context and an\n * Event Attributes, e.g. `{ c_pluginId: \"some value\", a_attribute1: 42 }`\n * @param context - analytics context object\n * @param attributes- additional analytics event attributes\n */\n private setEventParameters(\n context: AnalyticsContextValue | NewAnalyticsContextValue,\n attributes: AnalyticsEventAttributes = {},\n ) {\n const customEventParameters: {\n [x: string]: string | number | boolean | undefined;\n } = {};\n\n const contextKeys =\n this.allowedContexts?.join('') === '*'\n ? Object.keys(context)\n : this.allowedContexts;\n\n contextKeys?.forEach(ctx => {\n if (\n context[ctx] !== undefined &&\n context[ctx] !== null &&\n context[ctx] !== ''\n ) {\n customEventParameters[`c_${ctx}`] = context[ctx];\n }\n });\n\n const attrKeys =\n this.allowedAttributes?.join('') === '*'\n ? Object.keys(attributes)\n : this.allowedAttributes;\n attrKeys?.forEach(attr => {\n if (\n attributes[attr] !== undefined &&\n attributes[attr] !== null &&\n attributes[attr] !== ''\n ) {\n customEventParameters[`a_${attr}`] = attributes[attr];\n }\n });\n\n return customEventParameters;\n }\n\n /**\n * Sets the GA userId, based on the `userEntityRef` set on the backstage\n * identity loaded from a given Backstage Identity API instance. Because\n * Google forbids sending any PII (including on the userId field), we hash\n * the entire `userEntityRef` on behalf of integrators:\n *\n * - With value `User:default/name`, userId becomes `sha256(User:default/name)`\n *\n * If an integrator wishes to use an alternative hashing mechanism or an\n * entirely different value, they may do so by passing a `userIdTransform`\n * function alongside the `identityApi` to `GoogleAnalytics.fromConfig()`.\n * This function receives the `userEntityRef` as an argument and should\n * resolve to a hashed version of whatever identifier they choose.\n *\n * Note: this feature requires that an integrator has set up a Google\n * Analytics User ID view in the property used to track Backstage.\n * @param identityApi - IdentityApi object\n */\n private async setUserFrom(identityApi: IdentityApi) {\n const { userEntityRef } = await identityApi.getBackstageIdentity();\n\n // Prevent PII from being passed to Google Analytics.\n const userId = await this.getPrivateUserId(userEntityRef);\n\n // Set the user ID.\n ReactGA.set({ user_id: userId });\n\n // Notify the deferred capture mechanism that it may proceed.\n this.capture.setReady();\n }\n\n /**\n * Returns a PII-free (according to Google's terms of service) user ID for\n * use in Google Analytics.\n * @param userEntityRef - user entity as string\n */\n private getPrivateUserId(userEntityRef: string): Promise<string> {\n // Allow integrators to provide their own hashing transformer.\n if (this.customUserIdTransform) {\n return this.customUserIdTransform(userEntityRef);\n }\n\n return this.hash(userEntityRef);\n }\n\n /**\n * Simple hash function; relies on web cryptography + the sha-256 algorithm.\n * @param value - value to be hashed\n */\n private async hash(value: string): Promise<string> {\n const digest = await window.crypto.subtle.digest(\n 'sha-256',\n new TextEncoder().encode(value),\n );\n const hashArray = Array.from(new Uint8Array(digest));\n return hashArray.map(b => b.toString(16).padStart(2, '0')).join('');\n }\n}\n"],"names":[],"mappings":";;;AAoCO,MAAM,gBAAA,CAA0D;AAAA,EACpD,qBAAA;AAAA,EAGA,OAAA;AAAA,EACA,cAAA;AAAA,EACA,eAAA;AAAA,EACA,iBAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAKT,YAAY,OAAA,EAYjB;AACD,IAAA,MAAM;AAAA,MACJ,QAAA;AAAA,MACA,aAAA;AAAA,MACA,WAAA;AA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IAAA,CAAK,KAAK,aAAa,CAAA;AAAA,EAChC;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,MAAc,KAAK,KAAA,EAAgC;AACjD,IAAA,MAAM,MAAA,GAAS,MAAM,MAAA,CAAO,MAAA,CAAO,MAAA,CAAO,MAAA;AAAA,MACxC,SAAA;AAAA,MACA,IAAI,WAAA,EAAY,CAAE,MAAA,CAAO,KAAK;AAAA,KAChC;AACA,IAAA,MAAM,YAAY,KAAA,CAAM,IAAA,CAAK,IAAI,UAAA,CAAW,MAAM,CAAC,CAAA;AACnD,IAAA,OAAO,SAAA,CAAU,GAAA,CAAI,CAAA,CAAA,KAAK,CAAA,CAAE,QAAA,CAAS,EAAE,CAAA,CAAE,QAAA,CAAS,CAAA,EAAG,GAAG,CAAC,CAAA,CAAE,KAAK,EAAE,CAAA;AAAA,EACpE;AACF;;;;"}
@@ -1,4 +1,4 @@
1
- import { configApiRef, identityApiRef, createFrontendModule } from '@backstage/frontend-plugin-api';
1
+ import { identityApiRef, configApiRef, createFrontendModule } from '@backstage/frontend-plugin-api';
2
2
  import { AnalyticsImplementationBlueprint } from '@backstage/plugin-app-react';
3
3
  import { GoogleAnalytics4 } from './apis/implementations/AnalyticsApi/GoogleAnalytics4.esm.js';
4
4
 
@@ -1 +1 @@
1
- {"version":3,"file":"module.esm.js","sources":["../src/module.ts"],"sourcesContent":["/*\n * Copyright 2025 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\nimport {\n configApiRef,\n identityApiRef,\n createFrontendModule,\n} from '@backstage/frontend-plugin-api';\nimport { AnalyticsImplementationBlueprint } from '@backstage/plugin-app-react';\n\nimport { GoogleAnalytics4 } from './apis/implementations/AnalyticsApi';\n\nconst ga4Implementation = AnalyticsImplementationBlueprint.make({\n name: 'ga4',\n params: defineParams =>\n defineParams({\n deps: { configApi: configApiRef, identityApi: identityApiRef },\n factory: ({ configApi, identityApi }) =>\n GoogleAnalytics4.fromConfig(configApi, { identityApi }),\n }),\n});\n\n/**\n * @public\n */\nexport const ga4Module = createFrontendModule({\n pluginId: 'app',\n extensions: [ga4Implementation],\n});\n"],"names":[],"mappings":";;;;AAwBA,MAAM,iBAAA,GAAoB,iCAAiC,IAAK,CAAA;AAAA,EAC9D,IAAM,EAAA,KAAA;AAAA,EACN,MAAA,EAAQ,kBACN,YAAa,CAAA;AAAA,IACX,IAAM,EAAA,EAAE,SAAW,EAAA,YAAA,EAAc,aAAa,cAAe,EAAA;AAAA,IAC7D,OAAA,EAAS,CAAC,EAAE,SAAW,EAAA,WAAA,EACrB,KAAA,gBAAA,CAAiB,UAAW,CAAA,SAAA,EAAW,EAAE,WAAA,EAAa;AAAA,GACzD;AACL,CAAC,CAAA;AAKM,MAAM,YAAY,oBAAqB,CAAA;AAAA,EAC5C,QAAU,EAAA,KAAA;AAAA,EACV,UAAA,EAAY,CAAC,iBAAiB;AAChC,CAAC;;;;"}
1
+ {"version":3,"file":"module.esm.js","sources":["../src/module.ts"],"sourcesContent":["/*\n * Copyright 2025 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\nimport {\n configApiRef,\n identityApiRef,\n createFrontendModule,\n} from '@backstage/frontend-plugin-api';\nimport { AnalyticsImplementationBlueprint } from '@backstage/plugin-app-react';\n\nimport { GoogleAnalytics4 } from './apis/implementations/AnalyticsApi';\n\nconst ga4Implementation = AnalyticsImplementationBlueprint.make({\n name: 'ga4',\n params: defineParams =>\n defineParams({\n deps: { configApi: configApiRef, identityApi: identityApiRef },\n factory: ({ configApi, identityApi }) =>\n GoogleAnalytics4.fromConfig(configApi, { identityApi }),\n }),\n});\n\n/**\n * @public\n */\nexport const ga4Module = createFrontendModule({\n pluginId: 'app',\n extensions: [ga4Implementation],\n});\n"],"names":[],"mappings":";;;;AAwBA,MAAM,iBAAA,GAAoB,iCAAiC,IAAA,CAAK;AAAA,EAC9D,IAAA,EAAM,KAAA;AAAA,EACN,MAAA,EAAQ,kBACN,YAAA,CAAa;AAAA,IACX,IAAA,EAAM,EAAE,SAAA,EAAW,YAAA,EAAc,aAAa,cAAA,EAAe;AAAA,IAC7D,OAAA,EAAS,CAAC,EAAE,SAAA,EAAW,WAAA,EAAY,KACjC,gBAAA,CAAiB,UAAA,CAAW,SAAA,EAAW,EAAE,WAAA,EAAa;AAAA,GACzD;AACL,CAAC,CAAA;AAKM,MAAM,YAAY,oBAAA,CAAqB;AAAA,EAC5C,QAAA,EAAU,KAAA;AAAA,EACV,UAAA,EAAY,CAAC,iBAAiB;AAChC,CAAC;;;;"}
@@ -1 +1 @@
1
- {"version":3,"file":"DeferredCapture.esm.js","sources":["../../src/util/DeferredCapture.ts"],"sourcesContent":["/*\n * Copyright 2023 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\nimport ReactGA from 'react-ga4';\n\nimport { UaEventOptions } from 'react-ga4/types/ga4';\n\ntype Hit = {\n hitType: string;\n data: {\n [x: string]: any;\n };\n};\n\n/**\n * A wrapper around ReactGA that can optionally handle latent capture logic.\n *\n * - When defer is `false`, event data is sent directly to GA.\n * - When defer is `true`, event data is queued (with a timestamp), so that it\n * can be sent to GA once externally indicated to be ready. This relies on\n * the `qt` or `queueTime` parameter of the Measurement Protocol.\n *\n * @see https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#qt\n */\nexport class DeferredCapture {\n /**\n * Queue of deferred hits to be processed when ready. When undefined, hits\n * can safely be sent without delay.\n */\n private queue: Hit[] | undefined;\n\n /**\n * constructor for creating the DeferredCapture object\n * @param defer type of {defer: boolean}\n */\n constructor({ defer = false }: { defer: boolean }) {\n this.queue = defer ? [] : undefined;\n }\n\n /**\n * Indicates that deferred capture may now proceed.\n */\n setReady() {\n if (this.queue) {\n this.queue.forEach(this.sendDeferred);\n this.queue = undefined;\n }\n }\n\n /**\n * Either forwards the event directly to GA, or (if configured) enqueues the\n * event hit to be captured when ready.\n * @param eventDetails type of UaEventOptions object\n * @param metadata any object that can be passed as additional parameter to the event\n */\n event(eventDetails: UaEventOptions, metadata: any = {}) {\n const data = {\n ...eventDetails,\n ...metadata,\n };\n if (this.queue) {\n this.queue.push({\n hitType: eventDetails.action,\n data: {\n ...data,\n timestamp_micros: Date.now() * 1000,\n },\n });\n return;\n }\n ReactGA.event(eventDetails.action, data);\n }\n\n /**\n * Sends a given hit to GA, decorated with the correct queue time.\n * @param hit Hit object\n */\n private sendDeferred(hit: Hit) {\n // Send the hit with the appropriate queue time (`qt`).\n ReactGA.event(hit.hitType, {\n ...hit.data,\n });\n }\n}\n"],"names":[],"mappings":";;AAoCO,MAAM,eAAgB,CAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAKnB,KAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAMR,WAAY,CAAA,EAAE,KAAQ,GAAA,KAAA,EAA6B,EAAA;AACjD,IAAK,IAAA,CAAA,KAAA,GAAQ,KAAQ,GAAA,EAAK,GAAA,KAAA,CAAA;AAAA;AAC5B;AAAA;AAAA;AAAA,EAKA,QAAW,GAAA;AACT,IAAA,IAAI,KAAK,KAAO,EAAA;AACd,MAAK,IAAA,CAAA,KAAA,CAAM,OAAQ,CAAA,IAAA,CAAK,YAAY,CAAA;AACpC,MAAA,IAAA,CAAK,KAAQ,GAAA,KAAA,CAAA;AAAA;AACf;AACF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,KAAM,CAAA,YAAA,EAA8B,QAAgB,GAAA,EAAI,EAAA;AACtD,IAAA,MAAM,IAAO,GAAA;AAAA,MACX,GAAG,YAAA;AAAA,MACH,GAAG;AAAA,KACL;AACA,IAAA,IAAI,KAAK,KAAO,EAAA;AACd,MAAA,IAAA,CAAK,MAAM,IAAK,CAAA;AAAA,QACd,SAAS,YAAa,CAAA,MAAA;AAAA,QACtB,IAAM,EAAA;AAAA,UACJ,GAAG,IAAA;AAAA,UACH,gBAAA,EAAkB,IAAK,CAAA,GAAA,EAAQ,GAAA;AAAA;AACjC,OACD,CAAA;AACD,MAAA;AAAA;AAEF,IAAQ,OAAA,CAAA,KAAA,CAAM,YAAa,CAAA,MAAA,EAAQ,IAAI,CAAA;AAAA;AACzC;AAAA;AAAA;AAAA;AAAA,EAMQ,aAAa,GAAU,EAAA;AAE7B,IAAQ,OAAA,CAAA,KAAA,CAAM,IAAI,OAAS,EAAA;AAAA,MACzB,GAAG,GAAI,CAAA;AAAA,KACR,CAAA;AAAA;AAEL;;;;"}
1
+ {"version":3,"file":"DeferredCapture.esm.js","sources":["../../src/util/DeferredCapture.ts"],"sourcesContent":["/*\n * Copyright 2023 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\nimport ReactGA from 'react-ga4';\n\nimport { UaEventOptions } from 'react-ga4/types/ga4';\n\ntype Hit = {\n hitType: string;\n data: {\n [x: string]: any;\n };\n};\n\n/**\n * A wrapper around ReactGA that can optionally handle latent capture logic.\n *\n * - When defer is `false`, event data is sent directly to GA.\n * - When defer is `true`, event data is queued (with a timestamp), so that it\n * can be sent to GA once externally indicated to be ready. This relies on\n * the `qt` or `queueTime` parameter of the Measurement Protocol.\n *\n * @see https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#qt\n */\nexport class DeferredCapture {\n /**\n * Queue of deferred hits to be processed when ready. When undefined, hits\n * can safely be sent without delay.\n */\n private queue: Hit[] | undefined;\n\n /**\n * constructor for creating the DeferredCapture object\n * @param defer type of {defer: boolean}\n */\n constructor({ defer = false }: { defer: boolean }) {\n this.queue = defer ? [] : undefined;\n }\n\n /**\n * Indicates that deferred capture may now proceed.\n */\n setReady() {\n if (this.queue) {\n this.queue.forEach(this.sendDeferred);\n this.queue = undefined;\n }\n }\n\n /**\n * Either forwards the event directly to GA, or (if configured) enqueues the\n * event hit to be captured when ready.\n * @param eventDetails type of UaEventOptions object\n * @param metadata any object that can be passed as additional parameter to the event\n */\n event(eventDetails: UaEventOptions, metadata: any = {}) {\n const data = {\n ...eventDetails,\n ...metadata,\n };\n if (this.queue) {\n this.queue.push({\n hitType: eventDetails.action,\n data: {\n ...data,\n timestamp_micros: Date.now() * 1000,\n },\n });\n return;\n }\n ReactGA.event(eventDetails.action, data);\n }\n\n /**\n * Sends a given hit to GA, decorated with the correct queue time.\n * @param hit Hit object\n */\n private sendDeferred(hit: Hit) {\n // Send the hit with the appropriate queue time (`qt`).\n ReactGA.event(hit.hitType, {\n ...hit.data,\n });\n }\n}\n"],"names":[],"mappings":";;AAoCO,MAAM,eAAA,CAAgB;AAAA;AAAA;AAAA;AAAA;AAAA,EAKnB,KAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAMR,WAAA,CAAY,EAAE,KAAA,GAAQ,KAAA,EAAM,EAAuB;AACjD,IAAA,IAAA,CAAK,KAAA,GAAQ,KAAA,GAAQ,EAAC,GAAI,MAAA;AAAA,EAC5B;AAAA;AAAA;AAAA;AAAA,EAKA,QAAA,GAAW;AACT,IAAA,IAAI,KAAK,KAAA,EAAO;AACd,MAAA,IAAA,CAAK,KAAA,CAAM,OAAA,CAAQ,IAAA,CAAK,YAAY,CAAA;AACpC,MAAA,IAAA,CAAK,KAAA,GAAQ,MAAA;AAAA,IACf;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,KAAA,CAAM,YAAA,EAA8B,QAAA,GAAgB,EAAC,EAAG;AACtD,IAAA,MAAM,IAAA,GAAO;AAAA,MACX,GAAG,YAAA;AAAA,MACH,GAAG;AAAA,KACL;AACA,IAAA,IAAI,KAAK,KAAA,EAAO;AACd,MAAA,IAAA,CAAK,MAAM,IAAA,CAAK;AAAA,QACd,SAAS,YAAA,CAAa,MAAA;AAAA,QACtB,IAAA,EAAM;AAAA,UACJ,GAAG,IAAA;AAAA,UACH,gBAAA,EAAkB,IAAA,CAAK,GAAA,EAAI,GAAI;AAAA;AACjC,OACD,CAAA;AACD,MAAA;AAAA,IACF;AACA,IAAA,OAAA,CAAQ,KAAA,CAAM,YAAA,CAAa,MAAA,EAAQ,IAAI,CAAA;AAAA,EACzC;AAAA;AAAA;AAAA;AAAA;AAAA,EAMQ,aAAa,GAAA,EAAU;AAE7B,IAAA,OAAA,CAAQ,KAAA,CAAM,IAAI,OAAA,EAAS;AAAA,MACzB,GAAG,GAAA,CAAI;AAAA,KACR,CAAA;AAAA,EACH;AACF;;;;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@backstage-community/plugin-analytics-module-ga4",
3
- "version": "0.18.0",
3
+ "version": "0.19.0",
4
4
  "backstage": {
5
5
  "role": "frontend-plugin-module",
6
6
  "pluginId": "app",
@@ -60,16 +60,16 @@
60
60
  "test": "backstage-cli package test"
61
61
  },
62
62
  "dependencies": {
63
- "@backstage/config": "^1.3.6",
64
- "@backstage/core-components": "^0.18.8",
65
- "@backstage/core-plugin-api": "^1.12.4",
66
- "@backstage/frontend-plugin-api": "^0.15.1",
67
- "@backstage/plugin-app-react": "^0.2.1",
63
+ "@backstage/config": "^1.3.7",
64
+ "@backstage/core-components": "^0.18.9",
65
+ "@backstage/core-plugin-api": "^1.12.5",
66
+ "@backstage/frontend-plugin-api": "^0.16.2",
67
+ "@backstage/plugin-app-react": "^0.2.2",
68
68
  "react-ga4": "^2.0.0"
69
69
  },
70
70
  "devDependencies": {
71
- "@backstage/cli": "^0.36.0",
72
- "@backstage/dev-utils": "^1.1.21",
71
+ "@backstage/cli": "^0.36.1",
72
+ "@backstage/dev-utils": "^1.1.22",
73
73
  "@testing-library/dom": "^10.0.0",
74
74
  "@testing-library/jest-dom": "^6.0.0",
75
75
  "@testing-library/react": "^16.0.0",