Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/162.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 谷歌分析在C++;桌面应用程序_C++_Qt - Fatal编程技术网

C++ 谷歌分析在C++;桌面应用程序

C++ 谷歌分析在C++;桌面应用程序,c++,qt,C++,Qt,我正在尝试将Google Analytics tracking添加到我的C++/Qt桌面应用程序中。要做到这一点,我需要进行http访问http://www.google-analytics.com/__utm.gif如此处所述: 我的URL看起来像: http://www.google-analytics.com/__utm.gif?utmwv=5.2.5&utmac=UA-XXXXXXXX-1&utmhn=prot-on.com&utms=1&utmn=17

我正在尝试将Google Analytics tracking添加到我的C++/Qt桌面应用程序中。要做到这一点,我需要进行http访问
http://www.google-analytics.com/__utm.gif
如此处所述:

我的URL看起来像:

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);
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;
    }
}
qint64 currentTimestamp=QDateTime::currentMSecsSinceEpoch()/1000;
如果(此->timeOfFirstVisit==0)
此->timeOfFirstVisit=currentTimestamp;
如果(此->上次访问时间==0)
此->TimeOfPrevious Visit=currentTimestamp;
QString googleAnalyticsRequestUrl;

QTextStream(&googleAnalyticsRequestUrl)遗憾的是,谷歌没有官方的Qt库可用。不过,我建议您查看他们的Google Analytics API(目前处于测试阶段) 这还表示,即使您的请求无效且未正确处理,它也将始终返回200。他们更可能支持您使用这个测试版API,然后反向工程他们的javascript直接使用它


编辑:再看看您的代码,最好立即从QUrl开始,使用QUrl添加查询项,这样Qt会注意正确的编码和所有这些。

解决方案是打开一个套接字,正如我在这个PHP库中所做的:

我粘贴我的源代码:

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);
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;
    }
}
GoogleAnalyticsManager::GoogleAnalyticsManager()
:domainHash(this->generateDomainHash(主机名)),
sessionId(qrand()),
sessionNumberOfQueries(1),
timeOfFirstVisit(0),
前访时间(0)
{
}
void GoogleAnalyticsManager::sendPageVisit(常量字符*pageUrl)
{
QTC插座;
socket.connectToHost(“www.google-analytics.com”,80);
socket.waitForConnected();
if(socket.state()!=QAbstractSocket::ConnectedState){
qDebug()timeOfFirstVisit==0)
此->timeOfFirstVisit=currentTimestamp;
如果(此->上次访问时间==0)
此->TimeOfPrevious Visit=currentTimestamp;
QString googleAnalyticsRequest;
QTextStream(&googleAnalyticsRequest)这里有一个小类(包含在单个文件头中),用于从Qt应用程序向Google Analytics发送测量事件:


当然,请记住遵循谷歌的隐私政策,不要发送个人识别信息,知情同意,并提供用户选择退出……/P>你能直接从浏览器调用你的URL吗?如果这样调用,它是工作的,并且只有在从C++代码中调用时才失败?同样的问题是来自T的响应。服务器,您发出的这个GET请求可以200吗?@user1204395,在这种情况下,我会说您的问题不在C++/Qt代码中。在对代码进行更多工作之前,我会关注通过浏览器获得成功的GET。响应总是200(尽管参数是虚构的,但无论如何返回200)。我已编辑了我的帖子以添加更多信息。感谢您的回答!检查我看到的内容此库将打开一个套接字并发送请求(与我的问题格式相同)你能给我看看你的工作代码吗?就在上周,我正在考虑将分析集成到我的Qt应用程序中,在我发现beta api后,我有点停止了研究。