/qt-google-analytics

Utilty to send events from a Qt application to Google Analytics 4.

Primary LanguageC++Apache License 2.0Apache-2.0

Qt Google Analytics

GitHub issues Build Status Code Coverage Project license

About

This script allows you to send events from your Qt application to Google Analytics 4.

It supports sending events, setting user properties, as well as operating system detection (using a generated User-Agent as well as the User-Agent Client Hints specification).

Compatible with both Qt 5 and Qt 6.

Authors

License

The script is licensed under the Apache License 2.0.

Usage

Create a QtGoogleAnalytics instance

#include "qt-google-analytics.h"

QtGoogleAnalytics analytics("G-XXXXXXXXXX");

Alternatively, you can set your measurement ID using the setMeasurementId method:

QtGoogleAnalytics analytics;
analytics.setMeasurementId("G-XXXXXXXXXX");

(Optional) Set user information

You can set an user ID using the setUserId method:

analytics.setUserId("some-unique-user-id");

You can also set user properties using the setUserProperties method:

QVariantMap userProperties;
userProperties["some_property"] = 123;
analytics.setUserProperties(userProperties);

(Optional) Enable debug mode

If you want to debug the events using the DebugView, you can use setDebugModeEnabled.

analytics.setDebugModeEnabled(true);

(Optional) Override the User-Agent header

By default, an User-Agent header for the current platform will be generated. If you wish to override it and provide your own, you can use setUserAgent. Note that Google Analytics bases part of its platform detection on the User-Agent header, so overriding it to a const might make your platform analytics less reliable.

analytics.setUserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36");

Note that this does not impact the User-Agent Client Hints sent with the request.

Send events

Once you're set up, you can send events using the sendEvent method:

analytics.sendEvent("some_event");

You can also add event parameters as a second argument to the method:

QVariantMap eventParameters;
eventParameters["some_param"] = 123;
analytics.sendEvent("some_event_with_parameters", eventParameters);