C ++桌面应用程序中的Google Analytics

时间:2013-03-05 10:58:10

标签: c++ qt

我正在尝试将Google Analytics跟踪添加到我的C ++ / Qt桌面应用程序中。要做到这一点,我正在按照此处的指定对http://www.google-analytics.com/__utm.gif进行http GET: http://automateeverything.tumblr.com/post/20500736298/google-analytics-without-javascript-or-cookies

我的网址如下:

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

这是我的源代码:

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);

但我的网页访问次数从未出现在我的Google Analytics(分析)页面中...如果我做的不好,请问我这个吗?

3 个答案:

答案 0 :(得分:3)

可悲的是谷歌没有正式的Qt库。不过,我建议您查看他们的Google AnalyticsAPI(目前处于测试阶段)https://developers.google.com/analytics/devguides/collection/protocol/v1/ 这也表示,即使您的请求无效且未正确处理,它也将始终返回200。他们更有可能支持您使用此测试版API,然后对其javascript进行逆向工程以直接使用它。

编辑:同时查看您的代码,立即开始使用QUrl并使用http://harmattan-dev.nokia.com/docs/library/html/qt4/qurl.html#addQueryItem添加查询项目可能更明智,因此Qt会关注正确的编码以及所有这些。

答案 1 :(得分:1)

解决方案是打开一个套接字,因为我这个PHP库: http://code.google.com/p/php-ga/source/browse/trunk/src/GoogleAnalytics/Internals/Request/HttpRequest.php#175

我粘贴了我的源代码:

GoogleAnalyticsManager::GoogleAnalyticsManager()
    : domainHash(this->generateDomainHash(HOST_NAME)),
      sessionId(qrand()),
      sessionNumberOfQueries(1),
      timeOfFirstVisit(0),
      timeOfPreviousVisit(0)
{

}

void GoogleAnalyticsManager::sendPageVisit(const char *pageUrl)
{
    QTcpSocket socket;
    socket.connectToHost("www.google-analytics.com", 80);
    socket.waitForConnected();
    if (socket.state() != QAbstractSocket::ConnectedState) {
        qDebug() << "Impossible to connect to www.google-analytics.com";
        return;
    }

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

    QString googleAnalyticsRequest;
    QTextStream(&googleAnalyticsRequest) << "GET /__utm.gif"
    << "?utmwv=5.2.5"
    << "&utmac=" << TRACKING_ID
    << "&utmhn=" << HOST_NAME
    << "&utms=" << this->sessionNumberOfQueries
    << "&utmn=" << QString::number(qrand())
    << "&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())
    << " HTTP/1.0\r\n"
    << "Host: www.google-analytics.com\r\n"
    << "User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:19.0) Gecko/20100101 Firefox/19.0\r\n"
    << "X-Forwarded-For: 127.0.0.1\r\n"
    << "Connection: close\r\n\r\n\r\n";

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

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

    socket.write(googleAnalyticsRequest.toStdString().c_str());
    socket.waitForBytesWritten();
    socket.close();
}

int GoogleAnalyticsManager::generateDomainHash(const QString &domain)
{
    int hash = 1;

    if (domain != NULL && !domain.isEmpty()) {
        hash = 0;

        for (int pos = domain.length()-1; pos >= 0; pos--) {
            int current = domain.at(pos).toAscii();
            hash = ((hash << 6) & 0xfffffff) + current + (current << 14);
            int leftMost7 = hash & 0xfe00000;

            if(leftMost7 != 0) {
                hash ^= leftMost7 >> 21;
            }
        }
    }

    return hash;
}

void GoogleAnalyticsManager::updateSessionNumberOfQueries()
{
    this->sessionNumberOfQueries++;

    if (this->sessionNumberOfQueries > 500) {
        this->sessionId = qrand();
        this->sessionNumberOfQueries = 1;
        this->timeOfFirstVisit = 0;
        this->timeOfPreviousVisit = 0;
    }
}

答案 2 :(得分:0)

这是一个小类(包含在单个文件头中),用于从Qt应用程序向Google Analytics发送测量事件:

https://github.com/tesch1/qt-google-analytics-collector

当然,请记住遵循Google的隐私权政策,即不发送个人识别信息,知情同意,并为您的用户选择退出...