Skip to content

Commit f7ad9ac

Browse files
committed
fix(analytics): replace gtag calls with dataLayer.push for GTM compatibility
GTM does not expose window.gtag — it only reads from window.dataLayer. Replace gtag("set", { user_id }) with dataLayer.push({ user_id }) so user identification works correctly with the new GTM container.
1 parent fe823bc commit f7ad9ac

2 files changed

Lines changed: 6 additions & 13 deletions

File tree

apps/deploy-web/src/services/analytics/analytics.service.spec.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,10 @@ describe(AnalyticsService.name, () => {
9595
it("should only identify in enabled services", () => {
9696
const identify = vi.fn();
9797
const setUserId = vi.fn();
98-
const gtag = vi.fn();
98+
const dataLayer: Record<string, unknown>[] = [];
9999
const service = setup({
100100
amplitude: { identify, setUserId },
101-
gtag,
101+
dataLayer,
102102
options: {
103103
amplitude: { enabled: false, apiKey: mockAmplitudeApiKey },
104104
ga: { enabled: true, measurementId: mockGaMeasurementId }
@@ -108,7 +108,7 @@ describe(AnalyticsService.name, () => {
108108
const user = { id: faker.string.uuid() };
109109
service.identify(user);
110110

111-
expect(gtag).toHaveBeenCalledWith("set", { user_id: user.id });
111+
expect(dataLayer).toContainEqual({ user_id: user.id });
112112
expect(identify).not.toHaveBeenCalled();
113113
expect(setUserId).not.toHaveBeenCalled();
114114
});
@@ -157,7 +157,7 @@ describe(AnalyticsService.name, () => {
157157
service.track("connect_wallet", properties, "Amplitude");
158158

159159
expect(track).toHaveBeenCalledWith("connect_wallet", properties);
160-
expect(dataLayer).toHaveLength(0);
160+
expect(dataLayer).not.toContainEqual(expect.objectContaining({ event: "connect_wallet" }));
161161
});
162162

163163
it("should transform GA event names correctly", () => {
@@ -218,7 +218,6 @@ describe(AnalyticsService.name, () => {
218218
function setup(params: {
219219
amplitude?: Mocked<Amplitude>;
220220
dataLayer?: Record<string, unknown>[];
221-
gtag?: Gtag.Gtag;
222221
options?: AnalyticsOptions;
223222
storage?: Pick<Storage, "getItem" | "setItem">;
224223
}) {
@@ -245,7 +244,6 @@ describe(AnalyticsService.name, () => {
245244
ga: { enabled: false, measurementId: mockGaMeasurementId }
246245
},
247246
amplitude,
248-
() => params.gtag ?? vi.fn(),
249247
() => dataLayer,
250248
storage
251249
);

apps/deploy-web/src/services/analytics/analytics.service.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -150,14 +150,9 @@ export class AnalyticsService {
150150
private readonly isAmplitudeEnabled: boolean;
151151
private amplitudeInitialized = false;
152152

153-
private get gtag() {
154-
return this.getGtag();
155-
}
156-
157153
constructor(
158154
private readonly options: AnalyticsOptions,
159155
private readonly amplitudeClient: Amplitude = amplitude,
160-
private readonly getGtag: () => Gtag.Gtag | undefined = () => (isBrowser ? window.gtag : undefined),
161156
private readonly getDataLayer: () => Record<string, unknown>[] | undefined = () => (isBrowser ? window.dataLayer : undefined),
162157
private readonly storage: Pick<Storage, "getItem" | "setItem"> | undefined = isBrowser ? window.localStorage : undefined
163158
) {
@@ -181,8 +176,8 @@ export class AnalyticsService {
181176
return;
182177
}
183178

184-
if (this.options.ga.enabled && this.gtag && user.id) {
185-
this.gtag("set", { user_id: user.id });
179+
if (this.options.ga.enabled && user.id) {
180+
this.getDataLayer()?.push({ user_id: user.id });
186181
}
187182

188183
if (!this.isAmplitudeEnabled) {

0 commit comments

Comments
 (0)