Reporting & Analytics

vancewong
Member

Running a code snippet before a chatflow ends (Sending an event to Google Analytics)

SOLVE

Hello, has anyone tried adding an automation action (run a code snippet) right before the Hubspot Chatflow ends?

 

I've seen other threads on using GTM to track conversations started on the live chat, but not many on automated chatflows that can be used to collect contact information for a sales team to follow up with.

 

For eg. a chatflow that asks for the visitor's name, contact number, and time to call back, and sends that data to the team. But right before the chatflow ends, it sends an event to Google Analytics (like the below code snippet) so that that conversion can be recorded as a goal on GA.

 

<script type="text/javascript">
window.addEventListener("load", function(){
   window.HubSpotConversations.on('conversationStarted', payload => { ga('send', 'event', 'Beacon', 'chat-started', 'Hubspot'); });
});
</script>

 

Although I suspect since this code is executed within the chatflow, there's no need to add an event listener. This ensures that only when the user has submitted their details, would this code execute and send to GA as a goal completion.

 

My main challenge is not knowing where to insert the code within the code snippet and how to write the code. Pretty sure it's not just copying and pasting the javascript code inside...

 

I'm guessing can remove the callback function, since it's supposed to be just one-way (from Hubspot to GA), and the code should go right before the responseJson. And I might even be able to remove the responseJson portion.

 

vancewong_0-1622607179306.png

 

Hope the above makes sense, and I'm not a developer so trying to get some help here. Thank in advance to whoever can help!

1 Accepted solution
himanshurauthan
Solution
Thought Leader | Elite Partner
Thought Leader | Elite Partner

Running a code snippet before a chatflow ends (Sending an event to Google Analytics)

SOLVE

Thank you @PamCotton for counting me in.

 

Hi @vancewong,

 

So you can use the below code to register your event on conversationStarted, so when a conversation gets started this event will immediately fire, you can add your code snippet on this event by registering it.

 

window.HubSpotConversations.on('conversationStarted', payload => {
 console.log(payload);
});

 

Here is the link for reference: https://legacydocs.hubspot.com/docs/methods/conversations_api/widget_events

 

Hope this helps!!

 

Have an amazing day ahead!!

 

Regards,

Digital Marketing & Inbound Expert In Growth Hacking Technology

View solution in original post

2 Replies 2
himanshurauthan
Solution
Thought Leader | Elite Partner
Thought Leader | Elite Partner

Running a code snippet before a chatflow ends (Sending an event to Google Analytics)

SOLVE

Thank you @PamCotton for counting me in.

 

Hi @vancewong,

 

So you can use the below code to register your event on conversationStarted, so when a conversation gets started this event will immediately fire, you can add your code snippet on this event by registering it.

 

window.HubSpotConversations.on('conversationStarted', payload => {
 console.log(payload);
});

 

Here is the link for reference: https://legacydocs.hubspot.com/docs/methods/conversations_api/widget_events

 

Hope this helps!!

 

Have an amazing day ahead!!

 

Regards,

Digital Marketing & Inbound Expert In Growth Hacking Technology
PamCotton
Community Manager
Community Manager

Running a code snippet before a chatflow ends (Sending an event to Google Analytics)

SOLVE

Hello @vancewong, thank you for providing this information I will add some top experts to share their opinion @tjoyce @himanshurauthan @DanielSanchez any recommendation to @vancewong chatflow?

 

Thank you,

Pam

Você sabia que a Comunidade está disponível em outros idiomas?
Participe de conversas regionais, alterando suas configurações de idioma !


Did you know that the Community is available in other languages?
Join regional conversations by changing your language settings !