From c1fe9821b81db5b0cfc8e072a098a638d81c4e05 Mon Sep 17 00:00:00 2001 From: hassen Date: Thu, 11 Jan 2024 23:31:27 +0100 Subject: [PATCH] support overriding titleCase and redactEmail at global level and event() method level --- src/ga4.js | 28 ++++++++++++++---- src/ga4.test.js | 77 ++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 99 insertions(+), 6 deletions(-) diff --git a/src/ga4.js b/src/ga4.js index cf6a597..638393c 100644 --- a/src/ga4.js +++ b/src/ga4.js @@ -47,8 +47,19 @@ https://developers.google.com/tag-platform/gtagjs/reference * @property {Object} [gtagOptions] New parameter */ +/** + * @typedef {Object} Options + * @property {boolean} [titleCase=true] + * @property {boolean} [redactingEmail=true] + */ + export class GA4 { - constructor() { + /** + * @param {Options} [options] + */ + constructor(options = {}) { + this._titleCase = options.titleCase; + this._redactingEmail = options.redactingEmail; this.reset(); } @@ -401,8 +412,11 @@ export class GA4 { /** * @param {UaEventOptions|string} optionsOrName * @param {Object} [params] + * @param {Object} [formattingOptions] + * @param {boolean} [formattingOptions.titleCase] + * @param {boolean} [formattingOptions.redactingEmail] */ - event = (optionsOrName, params) => { + event = (optionsOrName, params, formattingOptions) => { if (typeof optionsOrName === "string") { this._gtag("event", optionsOrName, this._toGtagOptions(params)); } else { @@ -414,16 +428,20 @@ export class GA4 { return; } + const shouldTitleCase = formattingOptions?.titleCase ?? this._titleCase; + const shouldRedactEmail = + formattingOptions?.redactingEmail ?? this._redactingEmail; + // Required Fields const fieldObject = { hitType: "event", - eventCategory: format(category), - eventAction: format(action), + eventCategory: format(category, shouldTitleCase, shouldRedactEmail), + eventAction: format(action, shouldTitleCase, shouldRedactEmail), }; // Optional Fields if (label) { - fieldObject.eventLabel = format(label); + fieldObject.eventLabel = format(label, shouldTitleCase, shouldRedactEmail); } if (typeof value !== "undefined") { diff --git a/src/ga4.test.js b/src/ga4.test.js index a9042d5..3848bd7 100644 --- a/src/ga4.test.js +++ b/src/ga4.test.js @@ -1,5 +1,5 @@ import gtag from "./gtag"; -import GA4 from "./ga4"; +import GA4, { GA4 as GA4Implementation} from "./ga4"; const newDate = new Date("2020-01-01"); jest.mock("./gtag"); @@ -254,6 +254,81 @@ describe("GA4", () => { non_interaction: true, }); }); + + it("event() with formatting options", () => { + // Given + const object = { + category: "category value", + action: "action value", + label: "label value", + nonInteraction: true, + }; + const options = { + titleCase: false, + redactingEmail: false, + }; + + // When + GA4.event(object, undefined, options); + + // Then + expect(gtag).toHaveBeenNthCalledWith(1, "event", "action value", { + event_category: "category value", + event_label: "label value", + non_interaction: true, + }); + }); + + it("event() with global formatting options", () => { + // Given + const object = { + category: "category value", + action: "action value", + label: "label value", + nonInteraction: true, + }; + const options = { + titleCase: false, + redactingEmail: false, + }; + + // When + const ga4 = new GA4Implementation(options); + ga4.initialize(GA_MEASUREMENT_ID); + ga4.event(object); + + // Then + expect(gtag).toHaveBeenCalledWith("event", "action value", { + event_category: "category value", + event_label: "label value", + non_interaction: true, + }); + }); + + it("event() local formatting options take precedence on global formatting options", () => { + // Given + const object = { + category: "category value", + action: "action value", + label: "label value", + nonInteraction: true, + }; + const options = { + titleCase: false, + }; + + // When + const ga4 = new GA4Implementation(options); + ga4.initialize(GA_MEASUREMENT_ID); + ga4.event(object, undefined, { titleCase: true }); + + // Then + expect(gtag).toHaveBeenCalledWith("event", "Action Value", { + event_category: "Category Value", + event_label: "Label Value", + non_interaction: true, + }); + }); }); describe("GA4.set()", () => {