VisitorIdAccessor Lightning component

Important
Important

As of the Coveo for Salesforce v5 release, the Coveo VisitorIdAccessor Lightning component has been replaced by the Coveo ClientIdAccessor Lightning component.

The Coveo VisitorIdAccessor Lightning component reads and writes the visitor ID in the CoveoV2 namespace localStorage. Due to Lightning Locker restrictions, an Aura wrapper component that belongs to a different namespace can’t directly access the visitor ID in the CoveoV2 namespace localStorage. The Coveo VisitorIdAccessor Lightning component lets any Aura wrapper component in a different namespace access and manipulate the visitor ID so it can be retrieved and reused across sites during a visit (see Integrate a Coveo VisitorIdAccessor Lightning component).

Usage

Reference it in a custom Aura component (for example, LightningComponent.cmp).

<!-- LightningComponent.cmp -->
<aura:component implements="forceCommunity:availableForAllPageTypes">
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
    <CoveoV2:VisitorIdAccessor aura:id="visitorIdAccessor"/>
</aura:component>

Access and set the visitor ID in the CoveoV2 namespace localStorage as follows:

// LightningComponentController.js
({
    doInit: function(component) {
        const visitorIdCmp = component.find("visitorIdAccessor");
        // Access the VisitorID value from the CoveoV2 namespace.
        const visitorId = visitorIdCmp.getVisitorId();
        // Access the VisitorID value from the c namespace.
        const cVisitorId = localStorage.getItem('visitorId');
        if(visitorId) {
            // If there's a value in the CoveoV2 namespace, use it.
            // Save the value in the c namespace localStorage so it
            // can be used by other components.
            localStorage.setItem('visitorId', visitorId);
        } else if(cVisitorId) {
            // If there is no value in the CoveoV2 namespace,
            // but there is one in the c namespace, set it to the CoveoV2 namespace.
            visitorIdCmp.setVisitorId(cVisitorId);
        }
    }
})

Resources included with this component

This component doesn’t include any resources.

Aura methods

This component supports the Aura methods referenced in this section.

getVisitorId

Reads the visitor ID in the CoveoV2 namespace localStorage.

const visitorId = visitorIdCmp.getVisitorId();

setVisitorId

Writes the visitor ID in the CoveoV2 namespace localStorage.

This function requires a parameter:

  • the visitor ID (String): The visitor ID value to set in the CoveoV2 namespace localStorage.

visitorIdCmp.setVisitorId('123e4567-e89b-12d3-a456-426614174000');
Notes
  • This method won’t override any existing visitor ID already in the CoveoV2 namespace localStorage.

  • The visitorId parameter is mandatory. This method will log a warning message in the browser console if you invoke it without a parameter.

Aura events

This component doesn’t include any Aura events.

Options

This component doesn’t include any options.