Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Analytics in a C++ desktop application

Tags:

c++

qt

I'm trying to add Google Analytics tracking to my C++/Qt desktop application. To do that, I'm making a http GET to http://www.google-analytics.com/__utm.gif as specified here: http://automateeverything.tumblr.com/post/20500736298/google-analytics-without-javascript-or-cookies

My URL looks like:

http://www.google-analytics.com/__utm.gif?utmwv=5.2.5&utmac=UA-XXXXXXXX-1&utmhn=prot-on.com&utms=1&utmn=1763710005&utmcc=__utma%3D265465294.163654595.1362420921.1362420921.1362420921.1%3B&utmp=%2Freallyallheaders.html&utmcs=-&utmr=-&utmip=127.0.0.1&utmul=es-es&utmfl=-&utmje=-&utmsr=1920x1080&utmhid=957274494

And this is my source code:

qint64 currentTimestamp = QDateTime::currentMSecsSinceEpoch()/1000;
if (this->timeOfFirstVisit == 0)
    this->timeOfFirstVisit = currentTimestamp;
if (this->timeOfPreviousVisit == 0)
    this->timeOfPreviousVisit = currentTimestamp;

QString googleAnalyticsRequestUrl;
QTextStream(&googleAnalyticsRequestUrl) << "http://www.google-analytics.com/__utm.gif"
<< "?utmwv=5.2.5"
<< "&utmac=" << TRACKING_ID
<< "&utmhn=" << HOST_NAME
<< "&utms=" << this->sessionNumberOfQueries
<< "&utmn=" << QString::number(qrand()) //this->generateRandomUTMN()
<< "&utmcc=__utma%3D" << this->domainHash
    << "." << this->sessionId
    << "." << this->timeOfFirstVisit
    << "." << this->timeOfPreviousVisit
    << "." << currentTimestamp
    << ".1%3B"
<< "&utmp=" << QString(QUrl::toPercentEncoding(pageUrl))
<< "&utmcs=-"
<< "&utmr=-"
<< "&utmip=127.0.0.1"
<< "&utmul=" + QLocale::system().name().toLower().replace("_", "-")
<< "&utmfl=-"
<< "&utmje=-"
<< "&utmsr=" + QString::number(QApplication::desktop()->screenGeometry().width()) + "x" + QString::number(QApplication::desktop()->screenGeometry().height())
<< "&utmhid=" + QString::number(qrand());

this->timeOfPreviousVisit = currentTimestamp;
this->updateSessionNumberOfQueries();

qDebug() << "Sending Google Analytics request: " << googleAnalyticsRequestUrl;

// Send a http GET request to the created URL
QNetworkAccessManager *manager = new QNetworkAccessManager();
connect(manager, SIGNAL(finished(QNetworkReply *)),this, SLOT(googleAnalyticsRequestReceived(QNetworkReply *)));
connect(manager, SIGNAL(finished(QNetworkReply *)),manager, SLOT(deleteLater()));

QUrl requestUrl(googleAnalyticsRequestUrl);
QNetworkRequest request(requestUrl);

// I see this headers with Firebug, but I think that they are not necessary
request.setRawHeader("Host", "www.google-analytics.com");
request.setRawHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:19.0) Gecko/20100101 Firefox/19.0");
request.setRawHeader("X-Forwarded-For", "127.0.0.1");
request.setRawHeader("Connection", "close");

manager->get(request);

But my page visits never appears in my Google Analytics page... Can you say me if I'm making something bad please?

like image 786
user1204395 Avatar asked Sep 05 '25 03:09

user1204395


1 Answers

Sadly Google does not have an official Qt library available. I however recommend you to look into their Google Analytics API (currently in beta) https://developers.google.com/analytics/devguides/collection/protocol/v1/ This also says that it'll always return a 200 even if your request was invalid and wasn't processed correctly. They're more likely to support you using this beta API then reverse engineering their javascript to use that directly.

Edit: Also looking at your code, it is probably wiser to start with a QUrl right away and add query items using http://harmattan-dev.nokia.com/docs/library/html/qt4/qurl.html#addQueryItem so Qt will take care about the correct encoding and all that.

like image 92
Schoentoon Avatar answered Sep 07 '25 21:09

Schoentoon