Skip to content

Commit

Permalink
feat: added custom domain support in ga4 (#1697)
Browse files Browse the repository at this point in the history
* feat: added custom domain support in ga4

* fix: modified browser test case

* fix: update packages/analytics-js-integrations/src/integrations/GA4/browser.js

Co-authored-by: Gauravudia <[email protected]>

* fix: handling trailing slash in base url

---------

Co-authored-by: Gauravudia <[email protected]>
  • Loading branch information
manish339k and Gauravudia authored Apr 24, 2024
1 parent ba33b9e commit 3543cc1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ const destinationInfo = {
// jest.mock('this.analytics.getUserId', () => '1234');

describe('Google Analytics 4 init tests', () => {
test('Testing init call of Google Analytics 4 with MeasurementId', () => {
test('Testing init call of Google Analytics 4 with MeasurementId and SdkBaseUrl', () => {
const ga4 = new GA4(
{ measurementId: 'G-123456', debugView: true },
{ measurementId: 'G-123456', debugView: true, sdkBaseUrl: 'https://www.example.com//' },
{ getUserId: () => '1234', getUserTraits: () => {} },
destinationInfo,
);
ga4.init();
expect(typeof window.gtag).toBe('function');
expect(ga4.sdkBaseUrl).toEqual('https://www.example.com');
});
});

Expand Down Expand Up @@ -56,7 +57,11 @@ describe('Google Analytics 4 Page events tests', () => {
let ga4;
const { description, input, output } = event;
beforeEach(() => {
ga4 = new GA4(input.config, { getUserId: () => '1234', getUserTraits: () => {} }, destinationInfo);
ga4 = new GA4(
input.config,
{ getUserId: () => '1234', getUserTraits: () => {} },
destinationInfo,
);
ga4.init();
window.gtag = jest.fn();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
import { Cookie } from '@rudderstack/analytics-js-common/v1.1/utils/storage/cookie';
import Logger from '../../utils/logger';
import { eventsConfig } from './config';
import { constructPayload, flattenJsonPayload } from '../../utils/utils';
import { constructPayload, flattenJsonPayload, removeTrailingSlashes } from '../../utils/utils';
import {
shouldSendUserId,
prepareParamsAndEventName,
Expand Down Expand Up @@ -37,14 +37,16 @@ export default class GA4 {
this.piiPropertiesToIgnore = config.piiPropertiesToIgnore || [];
this.extendPageViewParams = config.extendPageViewParams || false;
this.overrideClientAndSessionId = config.overrideClientAndSessionId || false;
this.sdkBaseUrl =
removeTrailingSlashes(config.sdkBaseUrl) || 'https://www.googletagmanager.com';
({
shouldApplyDeviceModeTransformation: this.shouldApplyDeviceModeTransformation,
propagateEventsUntransformedOnError: this.propagateEventsUntransformedOnError,
destinationId: this.destinationId,
} = destinationInfo ?? {});
}

loadScript(measurementId) {
loadScript(measurementId, sdkBaseUrl) {
window.dataLayer = window.dataLayer || [];
window.gtag =
window.gtag ||
Expand Down Expand Up @@ -128,14 +130,11 @@ export default class GA4 {
this.sessionNumber = sessionNumber;
});

ScriptLoader(
'google-analytics 4',
`https://www.googletagmanager.com/gtag/js?id=${measurementId}`,
);
ScriptLoader('google-analytics 4', `${sdkBaseUrl}/gtag/js?id=${measurementId}`);
}

init() {
this.loadScript(this.measurementId);
this.loadScript(this.measurementId, this.sdkBaseUrl);
}

/**
Expand Down

0 comments on commit 3543cc1

Please sign in to comment.