From 46d0223125d646c50caa09926936f95cba2f927a Mon Sep 17 00:00:00 2001
From: Sarah Dayan <5370675+sarahdayan@users.noreply.github.com>
Date: Fri, 17 Mar 2023 14:12:28 +0100
Subject: [PATCH 1/2] feat(createInsightsMiddleware): always pass Algolia
 credentials locally

---
 .../__tests__/createInsightsMiddleware.ts     | 56 +++++++++++++++++--
 .../middlewares/createInsightsMiddleware.ts   | 20 ++++++-
 2 files changed, 68 insertions(+), 8 deletions(-)

diff --git a/packages/instantsearch.js/src/middlewares/__tests__/createInsightsMiddleware.ts b/packages/instantsearch.js/src/middlewares/__tests__/createInsightsMiddleware.ts
index 55d747c990..f9cce2e767 100644
--- a/packages/instantsearch.js/src/middlewares/__tests__/createInsightsMiddleware.ts
+++ b/packages/instantsearch.js/src/middlewares/__tests__/createInsightsMiddleware.ts
@@ -795,11 +795,19 @@ See documentation: https://www.algolia.com/doc/guides/building-search-ui/going-f
       });
 
       expect(analytics.viewedObjectIDs).toHaveBeenCalledTimes(1);
-      expect(analytics.viewedObjectIDs).toHaveBeenCalledWith({
-        index: 'my-index',
-        eventName: 'My Hits Viewed',
-        objectIDs: ['obj1'],
-      });
+      expect(analytics.viewedObjectIDs).toHaveBeenCalledWith(
+        {
+          index: 'my-index',
+          eventName: 'My Hits Viewed',
+          objectIDs: ['obj1'],
+        },
+        {
+          headers: {
+            'X-Algolia-Application-Id': 'myAppId',
+            'X-Algolia-API-Key': 'myApiKey',
+          },
+        }
+      );
     });
 
     it('calls onEvent when given', () => {
@@ -807,6 +815,7 @@ See documentation: https://www.algolia.com/doc/guides/building-search-ui/going-f
         createTestEnvironment();
 
       const onEvent = jest.fn();
+
       instantSearchInstance.use(
         createInsightsMiddleware({
           insightsClient,
@@ -833,7 +842,42 @@ See documentation: https://www.algolia.com/doc/guides/building-search-ui/going-f
             hello: 'world',
           },
         },
-        insightsClient
+        expect.any(Function)
+      );
+    });
+
+    it('sends events using onEvent', () => {
+      const { insightsClient, instantSearchInstance } = createTestEnvironment();
+
+      const onEvent = jest.fn((event, aa) => {
+        aa(event.insightsMethod, event.payload);
+      });
+
+      instantSearchInstance.use(
+        createInsightsMiddleware({
+          insightsClient,
+          onEvent,
+        })
+      );
+
+      instantSearchInstance.sendEventToInsights({
+        insightsMethod: 'viewedObjectIDs',
+        widgetType: 'ais.customWidget',
+        eventType: 'click',
+        payload: {
+          hello: 'world',
+        },
+      });
+
+      expect(insightsClient).toHaveBeenLastCalledWith(
+        'viewedObjectIDs',
+        { hello: 'world' },
+        {
+          headers: {
+            'X-Algolia-API-Key': 'myApiKey',
+            'X-Algolia-Application-Id': 'myAppId',
+          },
+        }
       );
     });
 
diff --git a/packages/instantsearch.js/src/middlewares/createInsightsMiddleware.ts b/packages/instantsearch.js/src/middlewares/createInsightsMiddleware.ts
index 53c2b869e6..17c2b88d0f 100644
--- a/packages/instantsearch.js/src/middlewares/createInsightsMiddleware.ts
+++ b/packages/instantsearch.js/src/middlewares/createInsightsMiddleware.ts
@@ -225,11 +225,27 @@ export function createInsightsMiddleware<
           immediate: true,
         });
 
+        // @ts-ignore
+        const insightsClientWithLocalCredentials = (method, payload) => {
+          return insightsClient(method, payload, {
+            headers: {
+              'X-Algolia-Application-Id': appId,
+              'X-Algolia-API-Key': apiKey,
+            },
+          });
+        };
+
         instantSearchInstance.sendEventToInsights = (event: InsightsEvent) => {
           if (onEvent) {
-            onEvent(event, _insightsClient as TInsightsClient);
+            onEvent(
+              event,
+              insightsClientWithLocalCredentials as TInsightsClient
+            );
           } else if (event.insightsMethod) {
-            insightsClient(event.insightsMethod, event.payload);
+            insightsClientWithLocalCredentials(
+              event.insightsMethod,
+              event.payload
+            );
 
             warning(
               Boolean((helper.state as PlainSearchParameters).userToken),

From 0b73dfd221674355fd5d7b13a8484d55161bb480 Mon Sep 17 00:00:00 2001
From: Sarah Dayan <5370675+sarahdayan@users.noreply.github.com>
Date: Fri, 17 Mar 2023 14:36:52 +0100
Subject: [PATCH 2/2] test(insights): update tests with local credentials

---
 .../src/lib/__tests__/InstantSearch-test.tsx  |   8 +-
 .../__tests__/hierarchical-menu.test.tsx      |   4 +-
 .../hits/__tests__/hits-integration-test.ts   |   4 +-
 .../infinite-hits-integration-test.ts         |   4 +-
 tests/common/widgets/hits/insights.ts         | 282 ++++++++++++------
 .../common/widgets/infinite-hits/insights.ts  | 282 ++++++++++++------
 6 files changed, 409 insertions(+), 175 deletions(-)

diff --git a/packages/instantsearch.js/src/lib/__tests__/InstantSearch-test.tsx b/packages/instantsearch.js/src/lib/__tests__/InstantSearch-test.tsx
index d8ad7f9f27..84a26aa91d 100644
--- a/packages/instantsearch.js/src/lib/__tests__/InstantSearch-test.tsx
+++ b/packages/instantsearch.js/src/lib/__tests__/InstantSearch-test.tsx
@@ -529,7 +529,13 @@ See https://www.algolia.com/doc/api-reference/widgets/configure/js/`);
       expect(insightsClient).toHaveBeenCalledTimes(1);
       expect(insightsClient).toHaveBeenCalledWith(
         'clickedObjectIDsAfterSearch',
-        { eventName: 'Add to cart' }
+        { eventName: 'Add to cart' },
+        {
+          headers: {
+            'X-Algolia-API-Key': 'apiKey',
+            'X-Algolia-Application-Id': 'appId',
+          },
+        }
       );
     });
 
diff --git a/packages/instantsearch.js/src/widgets/hierarchical-menu/__tests__/hierarchical-menu.test.tsx b/packages/instantsearch.js/src/widgets/hierarchical-menu/__tests__/hierarchical-menu.test.tsx
index f857cd9db6..b209eeccf8 100644
--- a/packages/instantsearch.js/src/widgets/hierarchical-menu/__tests__/hierarchical-menu.test.tsx
+++ b/packages/instantsearch.js/src/widgets/hierarchical-menu/__tests__/hierarchical-menu.test.tsx
@@ -370,7 +370,7 @@ describe('refinementList', () => {
           },
           widgetType: 'ais.hierarchicalMenu',
         },
-        null
+        expect.any(Function)
       );
 
       await wait(0);
@@ -393,7 +393,7 @@ describe('refinementList', () => {
           },
           widgetType: 'ais.hierarchicalMenu',
         },
-        null
+        expect.any(Function)
       );
     });
   });
diff --git a/packages/instantsearch.js/src/widgets/hits/__tests__/hits-integration-test.ts b/packages/instantsearch.js/src/widgets/hits/__tests__/hits-integration-test.ts
index 33333074b8..437c754d97 100644
--- a/packages/instantsearch.js/src/widgets/hits/__tests__/hits-integration-test.ts
+++ b/packages/instantsearch.js/src/widgets/hits/__tests__/hits-integration-test.ts
@@ -119,7 +119,7 @@ describe('hits', () => {
           },
           widgetType: 'ais.hits',
         },
-        null
+        expect.any(Function)
       );
     });
 
@@ -157,7 +157,7 @@ describe('hits', () => {
           },
           widgetType: 'ais.hits',
         },
-        null
+        expect.any(Function)
       );
     });
 
diff --git a/packages/instantsearch.js/src/widgets/infinite-hits/__tests__/infinite-hits-integration-test.ts b/packages/instantsearch.js/src/widgets/infinite-hits/__tests__/infinite-hits-integration-test.ts
index cccea1420b..ef0610caf8 100644
--- a/packages/instantsearch.js/src/widgets/infinite-hits/__tests__/infinite-hits-integration-test.ts
+++ b/packages/instantsearch.js/src/widgets/infinite-hits/__tests__/infinite-hits-integration-test.ts
@@ -367,7 +367,7 @@ describe('infiniteHits', () => {
           },
           widgetType: 'ais.infiniteHits',
         },
-        null
+        expect.any(Function)
       );
     });
 
@@ -405,7 +405,7 @@ describe('infiniteHits', () => {
           },
           widgetType: 'ais.infiniteHits',
         },
-        null
+        expect.any(Function)
       );
     });
 
diff --git a/tests/common/widgets/hits/insights.ts b/tests/common/widgets/hits/insights.ts
index 84502fe6dc..2b3d2f9d85 100644
--- a/tests/common/widgets/hits/insights.ts
+++ b/tests/common/widgets/hits/insights.ts
@@ -77,22 +77,40 @@ export function createInsightsTests(setup: HitsSetup, act: Act) {
       // View event called for each index once
       {
         expect(window.aa).toHaveBeenCalledTimes(2);
-        expect(window.aa).toHaveBeenCalledWith('viewedObjectIDs', {
-          eventName: 'Hits Viewed',
-          index: 'indexName',
-          objectIDs: Array.from(
-            { length: hitsPerPage },
-            (_, index) => `indexName-${index}`
-          ),
-        });
-        expect(window.aa).toHaveBeenCalledWith('viewedObjectIDs', {
-          eventName: 'Hits Viewed',
-          index: 'nested',
-          objectIDs: Array.from(
-            { length: hitsPerPage },
-            (_, index) => `nested-${index}`
-          ),
-        });
+        expect(window.aa).toHaveBeenCalledWith(
+          'viewedObjectIDs',
+          {
+            eventName: 'Hits Viewed',
+            index: 'indexName',
+            objectIDs: Array.from(
+              { length: hitsPerPage },
+              (_, index) => `indexName-${index}`
+            ),
+          },
+          {
+            headers: {
+              'X-Algolia-API-Key': 'apiKey',
+              'X-Algolia-Application-Id': 'appId',
+            },
+          }
+        );
+        expect(window.aa).toHaveBeenCalledWith(
+          'viewedObjectIDs',
+          {
+            eventName: 'Hits Viewed',
+            index: 'nested',
+            objectIDs: Array.from(
+              { length: hitsPerPage },
+              (_, index) => `nested-${index}`
+            ),
+          },
+          {
+            headers: {
+              'X-Algolia-API-Key': 'apiKey',
+              'X-Algolia-Application-Id': 'appId',
+            },
+          }
+        );
         window.aa.mockClear();
       }
 
@@ -170,39 +188,75 @@ export function createInsightsTests(setup: HitsSetup, act: Act) {
       // View event called for each index, batched in chunks of 20
       {
         expect(window.aa).toHaveBeenCalledTimes(4);
-        expect(window.aa).toHaveBeenCalledWith('viewedObjectIDs', {
-          eventName: 'Hits Viewed',
-          index: 'indexName',
-          objectIDs: Array.from(
-            { length: 20 },
-            (_, index) => `indexName-${index}`
-          ),
-        });
-        expect(window.aa).toHaveBeenCalledWith('viewedObjectIDs', {
-          eventName: 'Hits Viewed',
-          index: 'indexName',
-          objectIDs: Array.from(
-            { length: 5 },
-            (_, index) => `indexName-${20 + index}`
-          ),
-        });
+        expect(window.aa).toHaveBeenCalledWith(
+          'viewedObjectIDs',
+          {
+            eventName: 'Hits Viewed',
+            index: 'indexName',
+            objectIDs: Array.from(
+              { length: 20 },
+              (_, index) => `indexName-${index}`
+            ),
+          },
+          {
+            headers: {
+              'X-Algolia-API-Key': 'apiKey',
+              'X-Algolia-Application-Id': 'appId',
+            },
+          }
+        );
+        expect(window.aa).toHaveBeenCalledWith(
+          'viewedObjectIDs',
+          {
+            eventName: 'Hits Viewed',
+            index: 'indexName',
+            objectIDs: Array.from(
+              { length: 5 },
+              (_, index) => `indexName-${20 + index}`
+            ),
+          },
+          {
+            headers: {
+              'X-Algolia-API-Key': 'apiKey',
+              'X-Algolia-Application-Id': 'appId',
+            },
+          }
+        );
 
-        expect(window.aa).toHaveBeenCalledWith('viewedObjectIDs', {
-          eventName: 'Hits Viewed',
-          index: 'nested',
-          objectIDs: Array.from(
-            { length: 20 },
-            (_, index) => `nested-${index}`
-          ),
-        });
-        expect(window.aa).toHaveBeenCalledWith('viewedObjectIDs', {
-          eventName: 'Hits Viewed',
-          index: 'nested',
-          objectIDs: Array.from(
-            { length: 5 },
-            (_, index) => `nested-${20 + index}`
-          ),
-        });
+        expect(window.aa).toHaveBeenCalledWith(
+          'viewedObjectIDs',
+          {
+            eventName: 'Hits Viewed',
+            index: 'nested',
+            objectIDs: Array.from(
+              { length: 20 },
+              (_, index) => `nested-${index}`
+            ),
+          },
+          {
+            headers: {
+              'X-Algolia-API-Key': 'apiKey',
+              'X-Algolia-Application-Id': 'appId',
+            },
+          }
+        );
+        expect(window.aa).toHaveBeenCalledWith(
+          'viewedObjectIDs',
+          {
+            eventName: 'Hits Viewed',
+            index: 'nested',
+            objectIDs: Array.from(
+              { length: 5 },
+              (_, index) => `nested-${20 + index}`
+            ),
+          },
+          {
+            headers: {
+              'X-Algolia-API-Key': 'apiKey',
+              'X-Algolia-Application-Id': 'appId',
+            },
+          }
+        );
       }
     });
 
@@ -268,12 +322,21 @@ export function createInsightsTests(setup: HitsSetup, act: Act) {
         userEvent.click(screen.getByTestId('main-hits-top-level-1'));
 
         expect(window.aa).toHaveBeenCalledTimes(1);
-        expect(window.aa).toHaveBeenCalledWith('clickedObjectIDsAfterSearch', {
-          eventName: 'Hit Clicked',
-          index: 'indexName',
-          objectIDs: ['indexName-0'],
-          positions: [1],
-        });
+        expect(window.aa).toHaveBeenCalledWith(
+          'clickedObjectIDsAfterSearch',
+          {
+            eventName: 'Hit Clicked',
+            index: 'indexName',
+            objectIDs: ['indexName-0'],
+            positions: [1],
+          },
+          {
+            headers: {
+              'X-Algolia-API-Key': 'apiKey',
+              'X-Algolia-Application-Id': 'appId',
+            },
+          }
+        );
       }
     });
 
@@ -343,12 +406,21 @@ export function createInsightsTests(setup: HitsSetup, act: Act) {
         userEvent.click(screen.getByTestId('main-hits-top-level-1'));
 
         expect(window.aa).toHaveBeenCalledTimes(2);
-        expect(window.aa).toHaveBeenCalledWith('clickedObjectIDsAfterSearch', {
-          eventName: 'Hit Clicked',
-          index: 'indexName',
-          objectIDs: ['indexName-0'],
-          positions: [1],
-        });
+        expect(window.aa).toHaveBeenCalledWith(
+          'clickedObjectIDsAfterSearch',
+          {
+            eventName: 'Hit Clicked',
+            index: 'indexName',
+            objectIDs: ['indexName-0'],
+            positions: [1],
+          },
+          {
+            headers: {
+              'X-Algolia-API-Key': 'apiKey',
+              'X-Algolia-Application-Id': 'appId',
+            },
+          }
+        );
       }
     });
 
@@ -420,14 +492,29 @@ export function createInsightsTests(setup: HitsSetup, act: Act) {
             eventName: 'Converted',
             index: 'indexName',
             objectIDs: ['indexName-0'],
+          },
+          {
+            headers: {
+              'X-Algolia-API-Key': 'apiKey',
+              'X-Algolia-Application-Id': 'appId',
+            },
+          }
+        );
+        expect(window.aa).toHaveBeenCalledWith(
+          'clickedObjectIDsAfterSearch',
+          {
+            eventName: 'Hit Clicked',
+            index: 'indexName',
+            objectIDs: ['indexName-0'],
+            positions: [1],
+          },
+          {
+            headers: {
+              'X-Algolia-API-Key': 'apiKey',
+              'X-Algolia-Application-Id': 'appId',
+            },
           }
         );
-        expect(window.aa).toHaveBeenCalledWith('clickedObjectIDsAfterSearch', {
-          eventName: 'Hit Clicked',
-          index: 'indexName',
-          objectIDs: ['indexName-0'],
-          positions: [1],
-        });
       }
     });
 
@@ -493,12 +580,21 @@ export function createInsightsTests(setup: HitsSetup, act: Act) {
         userEvent.click(screen.getByTestId('main-hits-click-1'));
 
         expect(window.aa).toHaveBeenCalledTimes(1);
-        expect(window.aa).toHaveBeenCalledWith('clickedObjectIDsAfterSearch', {
-          eventName: 'Clicked',
-          index: 'indexName',
-          objectIDs: ['indexName-0'],
-          positions: [1],
-        });
+        expect(window.aa).toHaveBeenCalledWith(
+          'clickedObjectIDsAfterSearch',
+          {
+            eventName: 'Clicked',
+            index: 'indexName',
+            objectIDs: ['indexName-0'],
+            positions: [1],
+          },
+          {
+            headers: {
+              'X-Algolia-API-Key': 'apiKey',
+              'X-Algolia-Application-Id': 'appId',
+            },
+          }
+        );
       }
     });
 
@@ -566,18 +662,36 @@ export function createInsightsTests(setup: HitsSetup, act: Act) {
         userEvent.click(screen.getByTestId('main-hits-top-level-1'));
 
         expect(window.aa).toHaveBeenCalledTimes(2);
-        expect(window.aa).toHaveBeenCalledWith('clickedObjectIDsAfterSearch', {
-          eventName: 'Clicked nested',
-          index: 'nested',
-          objectIDs: ['nested-0'],
-          positions: [1],
-        });
-        expect(window.aa).toHaveBeenCalledWith('clickedObjectIDsAfterSearch', {
-          eventName: 'Hit Clicked',
-          index: 'indexName',
-          objectIDs: ['indexName-0'],
-          positions: [1],
-        });
+        expect(window.aa).toHaveBeenCalledWith(
+          'clickedObjectIDsAfterSearch',
+          {
+            eventName: 'Clicked nested',
+            index: 'nested',
+            objectIDs: ['nested-0'],
+            positions: [1],
+          },
+          {
+            headers: {
+              'X-Algolia-API-Key': 'apiKey',
+              'X-Algolia-Application-Id': 'appId',
+            },
+          }
+        );
+        expect(window.aa).toHaveBeenCalledWith(
+          'clickedObjectIDsAfterSearch',
+          {
+            eventName: 'Hit Clicked',
+            index: 'indexName',
+            objectIDs: ['indexName-0'],
+            positions: [1],
+          },
+          {
+            headers: {
+              'X-Algolia-API-Key': 'apiKey',
+              'X-Algolia-Application-Id': 'appId',
+            },
+          }
+        );
       }
     });
   });
diff --git a/tests/common/widgets/infinite-hits/insights.ts b/tests/common/widgets/infinite-hits/insights.ts
index e8b72c866c..02f3323a29 100644
--- a/tests/common/widgets/infinite-hits/insights.ts
+++ b/tests/common/widgets/infinite-hits/insights.ts
@@ -77,22 +77,40 @@ export function createInsightsTests(setup: InfiniteHitsSetup, act: Act) {
       // View event called for each index once
       {
         expect(window.aa).toHaveBeenCalledTimes(2);
-        expect(window.aa).toHaveBeenCalledWith('viewedObjectIDs', {
-          eventName: 'Hits Viewed',
-          index: 'indexName',
-          objectIDs: Array.from(
-            { length: hitsPerPage },
-            (_, index) => `indexName-${index}`
-          ),
-        });
-        expect(window.aa).toHaveBeenCalledWith('viewedObjectIDs', {
-          eventName: 'Hits Viewed',
-          index: 'nested',
-          objectIDs: Array.from(
-            { length: hitsPerPage },
-            (_, index) => `nested-${index}`
-          ),
-        });
+        expect(window.aa).toHaveBeenCalledWith(
+          'viewedObjectIDs',
+          {
+            eventName: 'Hits Viewed',
+            index: 'indexName',
+            objectIDs: Array.from(
+              { length: hitsPerPage },
+              (_, index) => `indexName-${index}`
+            ),
+          },
+          {
+            headers: {
+              'X-Algolia-API-Key': 'apiKey',
+              'X-Algolia-Application-Id': 'appId',
+            },
+          }
+        );
+        expect(window.aa).toHaveBeenCalledWith(
+          'viewedObjectIDs',
+          {
+            eventName: 'Hits Viewed',
+            index: 'nested',
+            objectIDs: Array.from(
+              { length: hitsPerPage },
+              (_, index) => `nested-${index}`
+            ),
+          },
+          {
+            headers: {
+              'X-Algolia-API-Key': 'apiKey',
+              'X-Algolia-Application-Id': 'appId',
+            },
+          }
+        );
         window.aa.mockClear();
       }
 
@@ -170,39 +188,75 @@ export function createInsightsTests(setup: InfiniteHitsSetup, act: Act) {
       // View event called for each index, batched in chunks of 20
       {
         expect(window.aa).toHaveBeenCalledTimes(4);
-        expect(window.aa).toHaveBeenCalledWith('viewedObjectIDs', {
-          eventName: 'Hits Viewed',
-          index: 'indexName',
-          objectIDs: Array.from(
-            { length: 20 },
-            (_, index) => `indexName-${index}`
-          ),
-        });
-        expect(window.aa).toHaveBeenCalledWith('viewedObjectIDs', {
-          eventName: 'Hits Viewed',
-          index: 'indexName',
-          objectIDs: Array.from(
-            { length: 5 },
-            (_, index) => `indexName-${20 + index}`
-          ),
-        });
+        expect(window.aa).toHaveBeenCalledWith(
+          'viewedObjectIDs',
+          {
+            eventName: 'Hits Viewed',
+            index: 'indexName',
+            objectIDs: Array.from(
+              { length: 20 },
+              (_, index) => `indexName-${index}`
+            ),
+          },
+          {
+            headers: {
+              'X-Algolia-API-Key': 'apiKey',
+              'X-Algolia-Application-Id': 'appId',
+            },
+          }
+        );
+        expect(window.aa).toHaveBeenCalledWith(
+          'viewedObjectIDs',
+          {
+            eventName: 'Hits Viewed',
+            index: 'indexName',
+            objectIDs: Array.from(
+              { length: 5 },
+              (_, index) => `indexName-${20 + index}`
+            ),
+          },
+          {
+            headers: {
+              'X-Algolia-API-Key': 'apiKey',
+              'X-Algolia-Application-Id': 'appId',
+            },
+          }
+        );
 
-        expect(window.aa).toHaveBeenCalledWith('viewedObjectIDs', {
-          eventName: 'Hits Viewed',
-          index: 'nested',
-          objectIDs: Array.from(
-            { length: 20 },
-            (_, index) => `nested-${index}`
-          ),
-        });
-        expect(window.aa).toHaveBeenCalledWith('viewedObjectIDs', {
-          eventName: 'Hits Viewed',
-          index: 'nested',
-          objectIDs: Array.from(
-            { length: 5 },
-            (_, index) => `nested-${20 + index}`
-          ),
-        });
+        expect(window.aa).toHaveBeenCalledWith(
+          'viewedObjectIDs',
+          {
+            eventName: 'Hits Viewed',
+            index: 'nested',
+            objectIDs: Array.from(
+              { length: 20 },
+              (_, index) => `nested-${index}`
+            ),
+          },
+          {
+            headers: {
+              'X-Algolia-API-Key': 'apiKey',
+              'X-Algolia-Application-Id': 'appId',
+            },
+          }
+        );
+        expect(window.aa).toHaveBeenCalledWith(
+          'viewedObjectIDs',
+          {
+            eventName: 'Hits Viewed',
+            index: 'nested',
+            objectIDs: Array.from(
+              { length: 5 },
+              (_, index) => `nested-${20 + index}`
+            ),
+          },
+          {
+            headers: {
+              'X-Algolia-API-Key': 'apiKey',
+              'X-Algolia-Application-Id': 'appId',
+            },
+          }
+        );
       }
     });
 
@@ -268,12 +322,21 @@ export function createInsightsTests(setup: InfiniteHitsSetup, act: Act) {
         userEvent.click(screen.getByTestId('main-hits-top-level-1'));
 
         expect(window.aa).toHaveBeenCalledTimes(1);
-        expect(window.aa).toHaveBeenCalledWith('clickedObjectIDsAfterSearch', {
-          eventName: 'Hit Clicked',
-          index: 'indexName',
-          objectIDs: ['indexName-0'],
-          positions: [1],
-        });
+        expect(window.aa).toHaveBeenCalledWith(
+          'clickedObjectIDsAfterSearch',
+          {
+            eventName: 'Hit Clicked',
+            index: 'indexName',
+            objectIDs: ['indexName-0'],
+            positions: [1],
+          },
+          {
+            headers: {
+              'X-Algolia-API-Key': 'apiKey',
+              'X-Algolia-Application-Id': 'appId',
+            },
+          }
+        );
       }
     });
 
@@ -343,12 +406,21 @@ export function createInsightsTests(setup: InfiniteHitsSetup, act: Act) {
         userEvent.click(screen.getByTestId('main-hits-top-level-1'));
 
         expect(window.aa).toHaveBeenCalledTimes(2);
-        expect(window.aa).toHaveBeenCalledWith('clickedObjectIDsAfterSearch', {
-          eventName: 'Hit Clicked',
-          index: 'indexName',
-          objectIDs: ['indexName-0'],
-          positions: [1],
-        });
+        expect(window.aa).toHaveBeenCalledWith(
+          'clickedObjectIDsAfterSearch',
+          {
+            eventName: 'Hit Clicked',
+            index: 'indexName',
+            objectIDs: ['indexName-0'],
+            positions: [1],
+          },
+          {
+            headers: {
+              'X-Algolia-API-Key': 'apiKey',
+              'X-Algolia-Application-Id': 'appId',
+            },
+          }
+        );
       }
     });
 
@@ -420,14 +492,29 @@ export function createInsightsTests(setup: InfiniteHitsSetup, act: Act) {
             eventName: 'Converted',
             index: 'indexName',
             objectIDs: ['indexName-0'],
+          },
+          {
+            headers: {
+              'X-Algolia-API-Key': 'apiKey',
+              'X-Algolia-Application-Id': 'appId',
+            },
+          }
+        );
+        expect(window.aa).toHaveBeenCalledWith(
+          'clickedObjectIDsAfterSearch',
+          {
+            eventName: 'Hit Clicked',
+            index: 'indexName',
+            objectIDs: ['indexName-0'],
+            positions: [1],
+          },
+          {
+            headers: {
+              'X-Algolia-API-Key': 'apiKey',
+              'X-Algolia-Application-Id': 'appId',
+            },
           }
         );
-        expect(window.aa).toHaveBeenCalledWith('clickedObjectIDsAfterSearch', {
-          eventName: 'Hit Clicked',
-          index: 'indexName',
-          objectIDs: ['indexName-0'],
-          positions: [1],
-        });
       }
     });
 
@@ -493,12 +580,21 @@ export function createInsightsTests(setup: InfiniteHitsSetup, act: Act) {
         userEvent.click(screen.getByTestId('main-hits-click-1'));
 
         expect(window.aa).toHaveBeenCalledTimes(1);
-        expect(window.aa).toHaveBeenCalledWith('clickedObjectIDsAfterSearch', {
-          eventName: 'Clicked',
-          index: 'indexName',
-          objectIDs: ['indexName-0'],
-          positions: [1],
-        });
+        expect(window.aa).toHaveBeenCalledWith(
+          'clickedObjectIDsAfterSearch',
+          {
+            eventName: 'Clicked',
+            index: 'indexName',
+            objectIDs: ['indexName-0'],
+            positions: [1],
+          },
+          {
+            headers: {
+              'X-Algolia-API-Key': 'apiKey',
+              'X-Algolia-Application-Id': 'appId',
+            },
+          }
+        );
       }
     });
 
@@ -566,18 +662,36 @@ export function createInsightsTests(setup: InfiniteHitsSetup, act: Act) {
         userEvent.click(screen.getByTestId('main-hits-top-level-1'));
 
         expect(window.aa).toHaveBeenCalledTimes(2);
-        expect(window.aa).toHaveBeenCalledWith('clickedObjectIDsAfterSearch', {
-          eventName: 'Clicked nested',
-          index: 'nested',
-          objectIDs: ['nested-0'],
-          positions: [1],
-        });
-        expect(window.aa).toHaveBeenCalledWith('clickedObjectIDsAfterSearch', {
-          eventName: 'Hit Clicked',
-          index: 'indexName',
-          objectIDs: ['indexName-0'],
-          positions: [1],
-        });
+        expect(window.aa).toHaveBeenCalledWith(
+          'clickedObjectIDsAfterSearch',
+          {
+            eventName: 'Clicked nested',
+            index: 'nested',
+            objectIDs: ['nested-0'],
+            positions: [1],
+          },
+          {
+            headers: {
+              'X-Algolia-API-Key': 'apiKey',
+              'X-Algolia-Application-Id': 'appId',
+            },
+          }
+        );
+        expect(window.aa).toHaveBeenCalledWith(
+          'clickedObjectIDsAfterSearch',
+          {
+            eventName: 'Hit Clicked',
+            index: 'indexName',
+            objectIDs: ['indexName-0'],
+            positions: [1],
+          },
+          {
+            headers: {
+              'X-Algolia-API-Key': 'apiKey',
+              'X-Algolia-Application-Id': 'appId',
+            },
+          }
+        );
       }
     });
   });