Gravatar for

Question by ananpatel, May 23, 2017 6:10 PM

Integrating Coveo Analytics with salesforce page

My issue is not so much on how to write the code, it’s about how to correctly reference the Coveo classes and components in my own Javascript in an apex class (.page).

I tried adding the Coveo resources into the project directories as a static resource however the file size gets too big and this work around didn’t work.

My problem is not so much related to the search framework, we already have that working on this application.

I already have an even listener configured and working for a simple button on a page, I simply want to track the button clicks on this button and send the data to Coveo analytics.

From the provided documentation:

This sample code is provided on the website (bottom of page) :



function cancelHelpRequestButtonWasClicked() {
    var eventCause = { name: 'cancelHelpRequest', type: 'caseDeflection' };
    var currentDate = new Date();
    var currentDateString = currentDate.getFullYear() + '/' + (currentDate.getMonth() + 1) + '/' + currentDate.getDate();
    var currentTimeString = currentDate.getHours() + ':' + currentDate.getMinutes();
    var eventMetadata = { date: currentDateString, time: currentTimeString };
    var buttonElement = document.querySelector('#cancelHelpRequestButton');
    Coveo.logCustomEvent(Coveo.$(document).find('#search'), eventCause, eventMetadata, buttonElement);
document.querySelector('#cancelHelpRequestButton').addEventListener('click', cancelHelpRequestButtonWasClicked)); 


I am trying to use this code for the custom button, however the class in unable to reference Coveo here which is where I am stuck

Any ideas on how I can get this to work?

Gravatar for

Comment by amoreau, May 23, 2017 6:29 PM

Which version of Coveo for Salesforce are you using?

Also, are you trying to do this in a Lightning community or in a Visualforce component?

I'm asking because it might change the answer whether you're using 2.41 (the most recent) or a previous package.

2 Replies
Gravatar for

Answer by amoreau, May 23, 2017 7:13 PM

The most probable reason why you wouldn't be able to reference Coveo is if your code is executed before the component loads.

For this reason, I suggest you use the `afterComponentsInitialization` event (see afterInitialization).

Let me know how it goes!

Gravatar for

Comment by ananpatel, May 23, 2017 8:03 PM

I just tried the afterInitialization, and I received the following error:
Uncaught TypeError: $(...).coveo is not a function

The site does use Coveo's search tool, however I am unable to reference Coveo in the javascript for this particular page.

Gravatar for

Answer by Etienne, May 25, 2017 2:45 PM

I would need a little bit more context to your question if I am to help you.

Are you trying to do this in a page where there is already a Coveo search page or case creation page?

Or essentially is there just a button in your page and you are trying to send an event?

Gravatar for

Comment by ananpatel, May 25, 2017 2:52 PM

The page is not the initial Coveo search page or a case creation page, it's the page which opens when a search result is clicked. There is a button added to the search results pages, and I would like to send event data from this button to Coveo.
When I try to initialize Coveo on the page to send the data to analytics, I get the following error:
Uncaught TypeError: $(...).coveo is not a function

Ask a question