Sending an object with Matomo event

Hi,
I struggle understanding how to track multiple fields on a single event.
I have a chat and want to send an event when a new message is sent, each event should log several values (chat_id, stream_id, is_new_chat, and other fields)
These value come from the client state, but for an event the number of fields are limited to 3 I think

Is there a way to logs all these fields in a single event on a button click ?

Cheers

1 Like

Hi there,

By default events will only allow for 3 values.

However you can use JSON stringify to encapsulate multiple fields into a single string parameter. That way you can have multiple values within an event value.

A example test page that would achieve this is below.
This would track an event as:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Matomo Event Tracking Example</title>
    <!-- Your Matomo Tracking Code -->
</head>
<body>
    <h1>Matomo Event Tracking Example</h1>
    <button id="trackEventButton">Send Event</button>

    <script>
        document.getElementById('trackEventButton').addEventListener('click', function() {
            var chatData = {
                chat_id: '12345',
                stream_id: '67890',
                is_new_chat: true,
                other_field: 'some_value'
            };

            var eventData = JSON.stringify(chatData);

            _paq.push(['trackEvent', 'Chat', 'NewMessage', eventData]);
        });
    </script>
</body>
</html>
1 Like

Hey thanks for the answer,

this was my first instinct but I was worried about the PO not being able to use the data like this.

Would is be easy for them to create metrics from stringified json objects ?

Cheers

Hi @JoeSlain
You can also use custom action dimensions to track additional data within your event.
But if you want to analyze events with these custom dimensions, you’ll have to use the Custom Reports premium plugin, or analyse directly in the MySQL database.