Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions src/ga4.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down Expand Up @@ -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 {
Expand All @@ -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") {
Expand Down
77 changes: 76 additions & 1 deletion src/ga4.test.js
Original file line number Diff line number Diff line change
@@ -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");
Expand Down Expand Up @@ -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()", () => {
Expand Down