# HubSpot Chat & Google Analytics Event Tracking

**URL:** <https://community.hubspot.com/t/hubspot-chat-google-analytics-event-tracking/25457>\
**Category:** Reporting & Analytics\
**Tags:** website-analytics, events\
**Created:** [September 14, 2020, 10:43pm UTC](https://community.hubspot.com/t/hubspot-chat-google-analytics-event-tracking/25457 "2020-09-14T22:43:52Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![jamesthrasher](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/jamesthrasher/32/81343_2.png) [@jamesthrasher](https://community.hubspot.com/u/jamesthrasher)\
**Post date:** [September 14, 2020, 10:43pm UTC](https://community.hubspot.com/t/hubspot-chat-google-analytics-event-tracking/25457/1 "2020-09-14T22:43:52Z")

</div>

I’ve used both snippets below to push a GA event whenever a conversation is started, but neither seem to work. Can anyone tell me what I’m doing wrong here?

```lia-code-sample
//Option A
<script type="text/javascript">
 window.HubSpotConversations.on('conversationStarted', payload => { ga('send', 'event', 'Beacon', 'chat-started', 'Hubspot'); });
</script>

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

```

---

<div class="post-metadata">

**Author:** ![JessicaH](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/jessicah/32/14826_2.png) [@JessicaH](https://community.hubspot.com/u/JessicaH)\
**Post date:** [September 15, 2020, 9:06am UTC](https://community.hubspot.com/t/hubspot-chat-google-analytics-event-tracking/25457/2 "2020-09-15T09:06:55Z")

</div>

Hi @jamesthrasher,

Thanks for reaching out.

I want to tag in some subject matter experts to see if they can assist with this.

Hi @derekcavaliero [@Mike\_Eastwood](https://community.hubspot.com/t5/user/viewprofilepage/user-id/9259) @MikeCormack, would you be able to share your thoughts with @jamesthrasher?

Thanks!

Jess

---

<div class="post-metadata">

**Author:** ![Mike\_Eastwood](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/mike_eastwood/32/70075_2.png) [@Mike\_Eastwood](https://community.hubspot.com/u/Mike_Eastwood)\
**Post date:** [September 18, 2020, 4:35pm UTC](https://community.hubspot.com/t/hubspot-chat-google-analytics-event-tracking/25457/3 "2020-09-18T16:35:34Z")

</div>

Sorry for the delayed reaction @jamesthrasher

Do you see any errors in your browser’s inspector when the page loads?

Are you able to share (by direct message) a link to the page you’re testing?

Or ,did you work it out already?

Mike

---

<div class="post-metadata">

**Author:** ![Mike\_Eastwood](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/mike_eastwood/32/70075_2.png) [@Mike\_Eastwood](https://community.hubspot.com/u/Mike_Eastwood)\
**Post date:** [September 18, 2020, 9:16pm UTC](https://community.hubspot.com/t/hubspot-chat-google-analytics-event-tracking/25457/4 "2020-09-18T21:16:25Z")

</div>

Looks to me like the HubSpot Javascript is loading after your Javascript fires.

So, James added the On Load Event Listener so the Javascript fired after everything loaded.

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

```

Notice the line:

`window.addEventListener("load", function(){`

and it’s closed with

`});`

Have fun

Mike

---

<div class="post-metadata">

**Author:** ![Rortega](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/rortega/32/16754_2.png) [@Rortega](https://community.hubspot.com/u/Rortega)\
**Post date:** [September 19, 2020, 7:08am UTC](https://community.hubspot.com/t/hubspot-chat-google-analytics-event-tracking/25457/5 "2020-09-19T07:08:32Z")

</div>

Hello!

I want to tag the chat with tag manager but when I add your script I get and error:

---

Hay un error en la línea 3, carácter 58: This language feature is only supported for ECMASCRIPT6 mode or better: arrow function.

---

Do you know how to solve it?

Best regards

---

<div class="post-metadata">

**Author:** ![AORourke](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/aorourke/32/93181_2.png) [@AORourke](https://community.hubspot.com/u/AORourke)\
**Post date:** [June 15, 2021, 4:55pm UTC](https://community.hubspot.com/t/hubspot-chat-google-analytics-event-tracking/25457/6 "2021-06-15T16:55:22Z")

</div>

I got the same issue. Did you ever find a fix?  
The closest related issues i can find is here but their fix does not work on the above code. [https://stackoverflow.com/questions/48922855/javascript-compiler-error-in-google-tag-manager-this-language-feature-is-only-s](https://stackoverflow.com/questions/48922855/javascript-compiler-error-in-google-tag-manager-this-language-feature-is-only-s)

---

<div class="post-metadata">

**Author:** ![piersg](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/piersg/32/99782_2.png) [@piersg](https://community.hubspot.com/u/piersg)\
**Post date:** [July 6, 2021, 1:44pm UTC](https://community.hubspot.com/t/hubspot-chat-google-analytics-event-tracking/25457/7 "2021-07-06T13:44:42Z")

</div>

@AORourke @Rortega you can’t use arrow functions (and other ECMA6 features) in JS that you use in GTM. [@Mike\_Eastwood](https://community.hubspot.com/t5/user/viewprofilepage/user-id/9259)’s code was to put on your site to send an event straight to GA. To add it in GTM instead, change Mike’s code to remove the arrow function, and also you would have to change the ga() call as that wouldn’t work in GTM. Maybe make it a custom HTML tag that pushes a custom event to the dataLayer that you can then use as a trigger for to fire a GTM event that will send event data to GA.

```lia-code-sample
<script type="text/javascript">
 window.addEventListener("load", function(){
	window.HubSpotConversations.on('conversationStarted', function(payload){
	 // ga('send', 'event', 'Beacon', 'chat-started', 'Hubspot');
		window.dataLayer.push({
			'event': 'hubspot-chat-started',
			'hs-chat-id': payload.conversation.conversationId
		});
	});
 });
</script>

```

---

<div class="post-metadata">

**Author:** ![AORourke](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/aorourke/32/93181_2.png) [@AORourke](https://community.hubspot.com/u/AORourke)\
**Post date:** [July 6, 2021, 1:56pm UTC](https://community.hubspot.com/t/hubspot-chat-google-analytics-event-tracking/25457/8 "2021-07-06T13:56:35Z")

</div>

Thank you.

---

<div class="post-metadata">

**Author:** ![EXWE-GmbH](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/exwe-gmbh/32/66855_2.png) [@EXWE-GmbH](https://community.hubspot.com/u/EXWE-GmbH)\
**Post date:** [April 6, 2022, 8:48am UTC](https://community.hubspot.com/t/hubspot-chat-google-analytics-event-tracking/25457/9 "2022-04-06T08:48:30Z")

</div>

Hey,

we also ran into some issues regarding the chat widget and tracking our conversions.

Our problem was, that we were using a CookieManager to fulfill German/EU DSGVO…  
We found a solution where we first checked for the CookieConsent and later added the eventListeners for the widget, when ready. We wrote everything down in a small article:  
English:  
[https://www.exwe.de/en/news/web-development/googleanalytics-hubspot-chat-tracking-with-cookiemanager/](https://www.exwe.de/en/news/web-development/googleanalytics-hubspot-chat-tracking-with-cookiemanager/)  
Deutsch:  
[https://www.exwe.de/de/news/web-entwicklung/googleanalytics-hubspot-chat-tracking-mit-cookiemanager/](https://www.exwe.de/de/news/web-entwicklung/googleanalytics-hubspot-chat-tracking-mit-cookiemanager/)

The main trick is to check the status:

```auto
 if (window.HubSpotConversations) {
 console.log('hubspot chat already running'); 
 onConversationsAPIReady();
 } else {
 console.log('hubspot chat awaiting'); 
 window.hsConversationsOnReady = [onConversationsAPIReady];
 } 

```

---

<div class="post-metadata">

**Author:** ![Djones](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/djones/32/82797_2.png) [@Djones](https://community.hubspot.com/u/Djones)\
**Post date:** [June 14, 2022, 12:07am UTC](https://community.hubspot.com/t/hubspot-chat-google-analytics-event-tracking/25457/10 "2022-06-14T00:07:33Z")

</div>

Hoping this helps someone.

I wrote an article here that breaks down how I [track Hubspot chats as GA events](https://medium.com/@dillonjones/track-hubspot-chats-in-google-analytics-4-simple-steps-4d799e6469c4)

---

<div class="post-metadata">

**Author:** ![kvlschaefer](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/kvlschaefer/32/71076_2.png) [@kvlschaefer](https://community.hubspot.com/u/kvlschaefer)\
**Post date:** [June 14, 2022, 7:15pm UTC](https://community.hubspot.com/t/hubspot-chat-google-analytics-event-tracking/25457/11 "2022-06-14T19:15:10Z")

</div>

Thanks for sharing, @Djones!
