Step 3: Create an Analytics Request

Next, we’ll fire an Analytics event. For a full list of the types of events you can define, see the Search Analytics Event Types reference doc.

1. Initialize the Analytics Library

In the body of your application, initialize the analytics library with your Experience Key, Experience Version and Business ID.

const analytics = provideAnalytics({
  experienceKey: '<one of your experience keys>', // example: answers-js-docs
  businessId: '<your business id>', //example: 3350634
  experienceVersion: 'PRODUCTION'
});

2. Fire an Analytics Event

Now that the Analytics Library has been initialized, we’ll fire off a Search analytics event:

analytics.report({
  type: "CTA_CLICK",
  entityId: '1',
  verticalKey: 'people',
  searcher: 'VERTICAL',
  queryId: '95751527-9db6-4859-8278-60d1c060b6c0'
});

3. Putting It All Together

The full index.ts file will look like this:

import { provideAnalytics } from '@yext/analytics';

const analytics = provideAnalytics({
  experienceKey: '<one of your experience keys>', // string, example: 'answers-js-docs'
  businessId: <your business id>, //number, example: 3350634
  experienceVersion: 'PRODUCTION'
});

analytics.report({
  type: "CTA_CLICK",
  entityId: '1',
  verticalKey: 'people',
  searcher: 'VERTICAL',
  queryId: '95751527-9db6-4859-8278-60d1c060b6c0'
});

If you followed the previous step’s instructions, you should see the analytics event fire in the network tab of your code sandbox page.