3

我正在尝试将 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(分析)页面中......如果我做错了,你能说我吗?

4

3 回答 3

3

遗憾的是,谷歌没有可用的官方 Qt 库。但是,我建议您查看他们的 Google Analytics API(目前处于测试阶段)https://developers.google.com/analytics/devguides/collection/protocol/v1/ 这也表示即使您的请求无效且未正确处理。他们更有可能使用这个 beta API 来支持你,然后对他们的 javascript 进行逆向工程以直接使用它。

编辑:还要查看您的代码,最好立即从 QUrl 开始并使用http://harmattan-dev.nokia.com/docs/library/html/qt4/qurl.html#addQueryItem添加查询项Qt 将负责正确的编码和所有这些。

于 2013-03-05T14:53:42.853 回答
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;
    }
}
于 2013-03-06T11:19:17.523 回答
0

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

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

当然,请记住遵守 Google 关于不发送个人身份信息、知情同意以及为您的用户提供选择退出的隐私政策……

于 2014-03-20T23:12:14.170 回答