# Tracking Gorgias livechat events in Google Analytics

[Gorgias](https://www.gorgias.com) is a popular help desk and customer service tool for ecommerce brands.

If your store uses Gorgias you can track how the pre-checkout interactions with support agents drive purchasing, and how the usage of the Gorgias interacts with other customer events in Google Analytics

## Tracking Gorgias livechat

Using the integration below you can move beyond Gorgias' own analytics to track interactions with the Gorgias livechat widget.

In Google Analytics you can use the events tracked to:

1. Look at ecommerce conversion rate by frequency of chat interaction, and see how live chat drives purchasing.
2. Split livechat usage by marketing channel or product, and see trends in chat usage.
3. Look at widget open rate by screen size or browser, and spot usability issues.

## Events tracked

Gorgias's livechat widget provides a way to [hook into chat integrations](https://docs.gorgias.com/en-US/advanced-customization-new-chat-81792), and the script below relays the most useful events to Google Analytics:

* `gorgias_widget_opened` when the chat widget is opened
* `gorgias_widget_closed` when the chat widget is closed
* `gorgias_message_sent` when a customer sends a message
* `gorgias_message_received` when a customer receives a message

{% hint style="success" %}
This ties in with other [Littledata tracking for Shopify](https://help.littledata.io/partner-recipes/gorgias/broken-reference), allowing you to link chat interactions with adds to cart and purchasing.
{% endhint %}

Here's how it looks on Littledata's merchandise store when we inspect the Google Analytics events being triggered:

![Gorgias events in Google Analytics](https://3956158252-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1QhTbrMraNWciQeDvu1b%2Fuploads%2Fgit-blob-a33eaf6204c230114f4675611aea170fe82a0bcf%2Fscreenshot-2024-01-22-at-13.20.29.png?alt=media)

## Using the events in Google Analytics

You'll be able to see chat events in the realtime reports for testing, and under **Reports > Engagement > Events** within 24 hours

![Events in Google Analytics](https://3956158252-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1QhTbrMraNWciQeDvu1b%2Fuploads%2Fgit-blob-f85ff21020db0167400f96caa94afd31fef17548%2Fscreenshot-2024-01-22-at-13.29.40.png?alt=media)

If you want to see how many people who received a message went on to purchase you can build a customer segment of those that have a `gorgias_message_received` event and compare with those users which did not.

Or look at the volume of events received and whether a certain volume of messaging drives purchasing.

## Adding the script

You can paste this minified script anywhere into the Shopify theme.liquid file. It waits for the Gorgias widget to load before listening to the chat events, and passes those events onto Google Analytics using the `gtag` library.

If `gtag` has not yet loaded the events will be queued until Google Analytics is ready.

```html
<script>
	var eventsToTrack = [
		{ gorgias: "message:sent", ga: "gorgias_message_sent" },
		{ gorgias: "message:received", ga: "gorgias_message_received" },
		{ gorgias: "widget:opened", ga: "gorgias_widget_opened" },
		{ gorgias: "widget:closed", ga: "gorgias_widget_closed" },
	];
	var initGorgiasChatPromise = window.GorgiasChat
		? window.GorgiasChat.init()
		: new Promise(function (resolve) {
				window.addEventListener("gorgias-widget-loaded", function () {
					resolve();
				});
			});
	initGorgiasChatPromise.then(function () {
		eventsToTrack.forEach(function (event) {
			GorgiasChat.on(event.gorgias, function (data) {
				gtag && gtag("event", event.ga);
			});
		});
	});
</script>
```

## The raw code

Here is the full script if you want to adapt it to track different events or to different data destinations

```javascript
var eventsToTrack = [
	{
		gorgias: "message:sent",
		ga: "gorgias_message_sent",
	},
	{
		gorgias: "message:received",
		ga: "gorgias_message_received",
	},
	{
		gorgias: "widget:opened",
		ga: "gorgias_widget_opened",
	},
	{
		gorgias: "widget:closed",
		ga: "gorgias_widget_closed",
	},
];

var initGorgiasChatPromise = window.GorgiasChat
	? window.GorgiasChat.init()
	: new Promise(function (resolve) {
			window.addEventListener("gorgias-widget-loaded", function () {
				resolve();
			});
		});

initGorgiasChatPromise.then(function () {
	eventsToTrack.forEach(function (event) {
		GorgiasChat.on(event.gorgias, function (data) {
			gtag && gtag("event", event.ga);
		});
	});
});
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://help.littledata.io/partner-recipes/gorgias/tracking-gorgias-google-analytics.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
