diff --git a/src/app/components/ATIAnalytics/beacon/index.test.ts b/src/app/components/ATIAnalytics/beacon/index.test.ts index db1f8ab828b..df2e8dca97b 100644 --- a/src/app/components/ATIAnalytics/beacon/index.test.ts +++ b/src/app/components/ATIAnalytics/beacon/index.test.ts @@ -99,6 +99,22 @@ describe('beacon', () => { true, ); }); + + it('should resolve reverbParams to null when Reverb is disabled for a service', () => { + sendEventBeacon({ + type: 'click', + service: 'news', + componentName: 'component', + pageIdentifier: 'pageIdentifier', + detailedPlacement: 'detailedPlacement', + useReverb: false, + }); + + const reverbParams = sendBeaconSpy.mock.calls[0][1]; + + expect(sendBeaconSpy).toHaveBeenCalledTimes(1); + expect(reverbParams).toBeNull(); + }); }); }); }); diff --git a/src/app/components/ATIAnalytics/index.test.tsx b/src/app/components/ATIAnalytics/index.test.tsx index b185487e641..0b5f2ace134 100644 --- a/src/app/components/ATIAnalytics/index.test.tsx +++ b/src/app/components/ATIAnalytics/index.test.tsx @@ -1201,5 +1201,29 @@ describe('ATI Analytics Container', () => { }, }); }); + + it('should set reverbParams to null when Reverb is disabled', () => { + const mockCanonical = jest.fn().mockReturnValue('canonical-return-value'); + // @ts-expect-error - we need to mock these functions to ensure tests are deterministic + canonical.default = mockCanonical; + + const { + metadata: { atiAnalytics }, + } = articleDataNews; + + render(, { + ...defaultRenderProps, + atiData: atiAnalytics, + isAmp: false, + pageData: articleDataNews, + pageType: ARTICLE_PAGE, + service: 'mundo', + isUK: true, + }); + + const { reverbParams } = mockCanonical.mock.calls[0][0]; + + expect(reverbParams).toBeNull(); + }); }); }); diff --git a/src/app/lib/analyticsUtils/sendBeacon/index.js b/src/app/lib/analyticsUtils/sendBeacon/index.js index a8078dfbfd2..466423bf40f 100644 --- a/src/app/lib/analyticsUtils/sendBeacon/index.js +++ b/src/app/lib/analyticsUtils/sendBeacon/index.js @@ -1,4 +1,3 @@ -import isLive from '../../utilities/isLive'; import onClient from '../../utilities/onClient'; import nodeLogger from '../../logger.node'; import { ATI_LOGGING_ERROR } from '../../logger.const'; @@ -107,7 +106,7 @@ const callReverb = async eventDetails => { const sendBeacon = async (url, reverbBeaconConfig) => { if (onClient()) { try { - if (!isLive() && reverbBeaconConfig) { + if (reverbBeaconConfig) { const { params: { page, user }, eventDetails, diff --git a/src/app/lib/analyticsUtils/sendBeacon/index.test.js b/src/app/lib/analyticsUtils/sendBeacon/index.test.js index faac7b9eef6..9fa7395914b 100644 --- a/src/app/lib/analyticsUtils/sendBeacon/index.test.js +++ b/src/app/lib/analyticsUtils/sendBeacon/index.test.js @@ -1,6 +1,8 @@ /* eslint-disable global-require */ import loggerMock from '#testHelpers/loggerMock'; import { ATI_LOGGING_ERROR } from '#app/lib/logger.const'; +import sendBeacon from './index'; +import * as onClient from '../../utilities/onClient'; let fetchResponse; let isOnClient; @@ -17,13 +19,12 @@ window.__reverb = { __reverbLoadedPromise: Promise.resolve(reverbMock), }; +jest.spyOn(onClient, 'default').mockImplementation(() => isOnClient); + describe('sendBeacon', () => { beforeEach(() => { isOnClient = true; fetch.mockImplementation(() => fetchResponse); - jest.mock('../../utilities/onClient', () => jest.fn()); - const onClient = require('../../utilities/onClient'); - onClient.mockImplementation(() => isOnClient); }); afterEach(() => { @@ -31,8 +32,6 @@ describe('sendBeacon', () => { }); it(`should fetch`, () => { - const sendBeacon = require('./index').default; - sendBeacon('https://foobar.com'); expect(fetch).toHaveBeenCalledWith('https://foobar.com', { @@ -43,8 +42,6 @@ describe('sendBeacon', () => { it(`should not fetch when not on client`, () => { isOnClient = false; - const sendBeacon = require('./index').default; - sendBeacon('https://foobar.com'); expect(fetch).not.toHaveBeenCalled(); @@ -61,6 +58,10 @@ describe('sendBeacon', () => { }, }; + // Simulates reverbBeaconConfig set to null in ATIAnalytics and sendEventBeacon + // in the event useReverb resolves to 'false' + const reverbConfigWhenReverbIsDisabled = null; + const originalProcessEnv = process.env; afterEach(() => { @@ -73,16 +74,18 @@ describe('sendBeacon', () => { }); it('should call Reverb viewEvent if Reverb config is passed', async () => { - const sendBeacon = require('./index').default; - await sendBeacon('https://foobar.com', reverbConfig); expect(reverbMock.viewEvent).toHaveBeenCalledTimes(1); }); - it('should not call "fetch" if Reverb config is passed', async () => { - const sendBeacon = require('./index').default; + it('should not call Reverb viewEvent if Reverb is not enabled for a service', async () => { + await sendBeacon('https://foobar.com', null); + expect(reverbMock.viewEvent).not.toHaveBeenCalled(); + }); + + it('should not call "fetch" if Reverb config is passed', async () => { await sendBeacon('https://foobar.com', reverbConfig); expect(fetch).not.toHaveBeenCalled(); @@ -95,16 +98,18 @@ describe('sendBeacon', () => { }); it('should call Reverb viewEvent if Reverb config is passed', async () => { - const sendBeacon = require('./index').default; - await sendBeacon('https://foobar.com', reverbConfig); expect(reverbMock.viewEvent).toHaveBeenCalledTimes(1); }); - it('should not call "fetch" if Reverb config is passed', async () => { - const sendBeacon = require('./index').default; + it('should not call Reverb viewEvent if Reverb is not enabled for a service', async () => { + await sendBeacon('https://foobar.com', null); + expect(reverbMock.viewEvent).not.toHaveBeenCalled(); + }); + + it('should not call "fetch" if Reverb config is passed', async () => { await sendBeacon('https://foobar.com', reverbConfig); expect(fetch).not.toHaveBeenCalled(); @@ -116,20 +121,25 @@ describe('sendBeacon', () => { process.env.SIMORGH_APP_ENV = 'live'; }); - it('should not call Reverb viewEvent if Reverb config is passed', async () => { - const sendBeacon = require('./index').default; - + it('should call Reverb viewEvent if Reverb config is passed', async () => { await sendBeacon('https://foobar.com', reverbConfig); - expect(reverbMock.viewEvent).not.toHaveBeenCalled(); + expect(reverbMock.viewEvent).toHaveBeenCalled(); }); - it('should call "fetch" when Reverb config is passed', async () => { - const sendBeacon = require('./index').default; + it('should not call Reverb viewEvent if Reverb is not enabled for a service', async () => { + await sendBeacon( + 'https://foobar.com', + reverbConfigWhenReverbIsDisabled, + ); + expect(reverbMock.viewEvent).not.toHaveBeenCalled(); + }); + + it('should not call "fetch" when Reverb config is passed', async () => { await sendBeacon('https://foobar.com', reverbConfig); - expect(fetch).toHaveBeenCalledTimes(1); + expect(fetch).not.toHaveBeenCalledTimes(1); }); }); }); @@ -143,8 +153,6 @@ describe('sendBeacon', () => { }); it(`should send error to logger`, async () => { - const sendBeacon = require('./index').default; - await sendBeacon('https://foobar.com'); expect(loggerMock.error).toHaveBeenCalledWith(ATI_LOGGING_ERROR, {