How to Send Custom Metadata with Search, Click, or Custom Events

Sometimes, it can be useful to tweak and modify the standard usage analytics events logged by the Coveo JavaScript Search Framework. You can do so by using a changeAnalyticsCustomData event handler.

  • Add a userRole metadata to all search events.

    /**
     * Gets the role of the currently authenticated user.
     *
     * @returns {string} The user role (e.g., "admin").
     */
     getUserRole = function() {
       // Implementation ...
     };
     
    var root = document.body;
    Coveo.$$(root).on('changeAnalyticsCustomData', function(e, args) {
      if (args.type == 'SearchEvent'){
        currentUserRole = getUserRole();
        args.metaObject['userRole'] = currentUserRole;
      }
    })
    
  • Change the originLevel3 value on all standard usage analytics events.

    /**
     * Gets the current `originLevel3`.
     *
     * @returns {string} The current `originLevel3` (e.g., "https://docs.coveo.com/en/2004").
     */
     getOriginLevel3 = function() {
       // Implementation ...
     };
     
    var root = document.body;
    Coveo.$$(root).on('changeAnalyticsCustomData', function(e, args) {
      currentOriginLevel3 = getOriginLevel3();
      args.originLevel3 = currentOriginLevel3;
    })