From a6df7d90358b605ea017c6b92ac23a16d2539cdf Mon Sep 17 00:00:00 2001 From: Jerome Covington Date: Tue, 24 Oct 2023 10:27:18 -0400 Subject: [PATCH 1/2] Support titleCase option --- src/ga4.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/ga4.js b/src/ga4.js index cf6a597..9318c64 100644 --- a/src/ga4.js +++ b/src/ga4.js @@ -177,9 +177,11 @@ export class GA4 { gtagOptions, nonce, testMode = false, + titleCase = true, gtagUrl, } = options; this._testMode = testMode; + this._titleCase = titleCase; if (!testMode) { this._loadGA(this._currentMeasurementId, nonce, gtagUrl); @@ -417,13 +419,13 @@ export class GA4 { // Required Fields const fieldObject = { hitType: "event", - eventCategory: format(category), - eventAction: format(action), + eventCategory: format(category, this._titleCase), + eventAction: format(action, this._titleCase), }; // Optional Fields if (label) { - fieldObject.eventLabel = format(label); + fieldObject.eventLabel = format(label, this._titleCase); } if (typeof value !== "undefined") { From e534fc5a2c6391b36ca51ee8e791e8a6a610d2c9 Mon Sep 17 00:00:00 2001 From: Jerome Covington Date: Tue, 24 Oct 2023 11:00:02 -0400 Subject: [PATCH 2/2] Expand tests for titleCase --- src/ga4.test.js | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/src/ga4.test.js b/src/ga4.test.js index a9042d5..24786cc 100644 --- a/src/ga4.test.js +++ b/src/ga4.test.js @@ -60,6 +60,50 @@ describe("GA4", () => { expect(gtag).toHaveBeenCalledTimes(0); }); + it("initialize() with default titleCase true", () => { + // Given + const options = {}; + + // When + GA4.initialize(GA_MEASUREMENT_ID, options); + GA4.event({ + action: "action_name", + category: "category_name", + label: "label_name", + }); + + // Then + expect(gtag).toHaveBeenNthCalledWith(1, "js", newDate); + expect(gtag).toHaveBeenNthCalledWith(2, "config", GA_MEASUREMENT_ID); + expect(gtag).toHaveBeenNthCalledWith(3, "event", "Action_name", { + "event_category": "Category_name", + "event_label": "Label_name", + }); + }); + + it("initialize() with titleCase false", () => { + // Given + const options = { + titleCase: false, + }; + + // When + GA4.initialize(GA_MEASUREMENT_ID, options); + GA4.event({ + action: "action_name", + category: "category_name", + label: "label_name", + }); + + // Then + expect(gtag).toHaveBeenNthCalledWith(1, "js", newDate); + expect(gtag).toHaveBeenNthCalledWith(2, "config", GA_MEASUREMENT_ID); + expect(gtag).toHaveBeenNthCalledWith(3, "event", "action_name", { + "event_category": "category_name", + "event_label": "label_name", + }); + }); + it("initialize() multiple products", () => { // Given const GA_MEASUREMENT_ID2 = "GA_MEASUREMENT_ID2";