LMFX Event – Track HubSpot Live Chats

Posted by: JP Betis Date Posted: December 29, 2023
Back to Case Studies

Issue

Track Hubspot chat engagement – Conversation start

Solution

Lately, I have had a task to track HubSpot Live Chats. Basically, I only need to track its initial user engagement – A conversation being started.

I forgot the client’s name, but I was able to save the script contents I used. which I listed below.

 

1. We will create a custom HTML tag to fire based on conversations being started or contacts associated in HubSpot. This tag needs to fire on all pages that the live chat appears on.

HubSpot event listener tag configuration

Tag name: HubSpot – Live Chat Event Listener

Tag type: Custom HTML

Trigger: All pages (page view)

HTML:

<script>
function onConversationsAPIReady() {
    window.HubSpotConversations.on('contactAssociated', function(payload) {
        window.dataLayer.push({
            'event': 'hubspot_chat',
            'eventCategory': 'hubspot',
            'eventAction': 'contact_associated',
            'eventLabel': payload.conversation.conversationId
        })
    });
    window.HubSpotConversations.on('conversationStarted', function(payload) {
        window.dataLayer.push({
            'event': 'hubspot_chat',
            'eventCategory': 'hubspot',
            'eventAction': 'conversation_started',
            'eventLabel': payload.conversation.conversationId
        })
    });
}
if (window.HubSpotConversations) {
    onConversationsAPIReady();
} else {
    window.hsConversationsOnReady = [onConversationsAPIReady];
}
</script>

 

2. Next is to create a variable to capture the DataLayer parameters

Variable configuration

Variable name: DLV – eventAction

Variable type: Data layer variable

Data layer variable name: eventAction

Data layer version: version 2

Screenshot: https://webpagefx.mangoapps.com/msc/MTUzMDY4NF8xMjU0NDI0NQ

 

Variable name: DLV – eventCategory

Variable type: Data layer variable

Data layer variable name: eventCategory

Data layer version: version 2

Screenshot: https://webpagefx.mangoapps.com/msc/MTUzMDY4NV8xMjU0NDI0Ng

 

Variable name: DLV – eventLabel

Variable type: Data layer variable

Data layer variable name: eventLabel

Data layer version: version 2

Screenshot: https://webpagefx.mangoapps.com/msc/MTUzMDY4Nl8xMjU0NDI0Nw

 

3. Then we will now create an event trigger and a custom HTML to send the data to MCFX

 

Trigger configuration

Trigger name: MCFX -t HubSpot Live Chat – Event

Trigger type: custom event

Event name: hubspot_chat (exactly matching the event name in the HubSpot live chat event listener tag code)

Trigger fires on: all custom events

Screenshot: https://webpagefx.mangoapps.com/msc/MTUzMDY5NF8xMjU0NDI1NQ

 

Custom HTML Configuration

Tag Type: Custom HTML

Tag Name: MCFX Event – HubSpot Live Chat

HTML content

<script>
mcfx('send', {
type: 'event',
category: 'Chat Started',
action: '{{DLV – eventCategory}} {{DLV – eventAction}}',
label: '{{DLV – eventLabel}}',
value: '0'
});
</script>

 

I’ll update this case study once I have more examples (data shown on LMFX – Events).

Achievement

We are trying to track two events:

  • A conversation being started. (Required)
  • A contact being associated in HubSpot – this will track when an email, for example, is captured by the live chat. (Optional)