decoration left 1
decoration left 2
decoration rigth

Show/Hide search bar
black cat logo variable logo
[15 Nov 2013]

Track C++ application with Google Analytics and Qt

You can use Google Analytics to generate detailed statistics about visitors of a website or a mobile application. You can also use this service to track desktop offline applications as well. Following code snippet shows how to notify Google Analytics about usage of C++ application developed with Qt library. First, what you should do is to register Google Analytics account and add new property. To track desktop application you can add web-page property or mobile application property. This choise determines which parameters are required in a request to Google Analytics. Following code assumes that property is of web-page type. After registration, you will receive property identification code, like UA-12345678-1. It should be provided as tid parameter and encoded into the request.
Initialization of QNetworkAccessManager:
void Analytics::initialize()
    // Create new QNetworkAccessManager
    m_manager = new QNetworkAccessManager(this);
    // Call slot_receive() when reply is received
    QObject::connect(m_manager, SIGNAL(finished(QNetworkReply *)),
                     this, SLOT(slot_receive(QNetworkReply *)));

    // Send requests to Google Analytics while app is running
    QTimer * timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(slot_requestAnalyticsView()));
    timer->start(5 * 60 * 1000); // send requests every 5 minutes
    slot_requestAnalyticsView(); // send first request
Request to Google Analytics (more about paremeters is here):
void Analytics::slot_requestAnalyticsView()
    // create request and set URL of receiver
    QNetworkRequest request;
    QUrl host("");

    // setup parameters of request
    QString requestParams;
    requestParams += "v=1"; // version of protocol
    requestParams += "&t=pageview"; // type of request
    requestParams += "&tid=UA-12345678-1"; // Google Analytics account
    requestParams += "&cid=" + getMacAddress(); // unique user identifier
    requestParams += "&dp=ApplicationName"); // name of page (or app name)
    requestParams += "&ul=" + QLocale::system().name(); // language

    // send request via post method
    m_manager->post(request, requestParams.toStdString().c_str());
Get reply from Google Analytics:
void Analytics::slot_receive(QNetworkReply * reply)
     // Output information about reply
    QByteArray bytes=reply->readAll();
    qDebug()<<"Contents" << QString::fromUtf8(, bytes.size());

Sun and Black Cat- Igor Dykhta (igor dykhta email) 2007-2014