Skip to content

Commit

Permalink
Merge pull request #42 from el7addad/master
Browse files Browse the repository at this point in the history
Improvements for Google Analytics Instrumentation
  • Loading branch information
fmckenna authored Mar 31, 2019
2 parents 5e55d4d + 4c17ba6 commit 09644e4
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 41 deletions.
6 changes: 4 additions & 2 deletions EE-UQ.pri
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ SOURCES += \
$$PWD/StochasticMotionInput/src/VlachosEtAlModel.cpp \
$$PWD/Controller2D.cpp \
$$PWD/GlWidget2D.cpp \
$$PWD/EDP.cpp
$$PWD/EDP.cpp \
$$PWD/GoogleAnalytics.cpp

HEADERS += \
$$PWD/MainWindowWorkflowApp.h\
Expand Down Expand Up @@ -86,7 +87,8 @@ HEADERS += \
$$PWD/StochasticMotionInput/include/VlachosEtAlModel.h \
$$PWD/Controller2D.h \
$$PWD/GlWidget2D.h \
$$PWD/EDP.h
$$PWD/EDP.h \
$$PWD/GoogleAnalytics.h

RESOURCES += \
images.qrc \
Expand Down
94 changes: 94 additions & 0 deletions GoogleAnalytics.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#include "GoogleAnalytics.h"
#include <QtNetwork/QNetworkReply>
#include <QtNetwork/QNetworkRequest>
#include <QHostInfo>
#include <QCoreApplication>
#include <QSettings>

namespace GoogleAnalytics {

void ReportStart()
{
Report("Start");
}

void ReportLocalRun()
{
Report("Local", "Simulation");
}

void ReportDesignSafeRun()
{
Report("DesignSafe", "Simulation");
}

void StartSession()
{
Report("Start", "Session", SessionControl::Start);

}

void EndSession()
{
Report("End", "Session", SessionControl::End);
}

//TODO: This code may need to be refactored and shared in SimCenterCommon
QUuid GetCommonClientId()
{
QSettings commonSettings("SimCenter", "Common"); //These names will need to be constants to be shared
QVariant clientIdSetting = commonSettings.value("clientId");
if (!clientIdSetting.isValid())
{
commonSettings.setValue("clientId", QUuid::createUuid());
clientIdSetting = commonSettings.value("clientId");
}
return clientIdSetting.toUuid();
}

void SetTrackingId(QString atrackingId)
{
trackingId = atrackingId;
}

void Report(QString eventAction, QString category, SessionControl sessionControl)
{
QNetworkRequest request;
QUrl host("http://www.google-analytics.com/collect");
request.setUrl(host);
request.setHeader(QNetworkRequest::ContentTypeHeader,
"application/x-www-form-urlencoded");

// setup parameters of request
QString requestParams;
QUuid clientId = GetCommonClientId();
requestParams += "v=1"; // version of protocol
requestParams += "&tid=" + trackingId; // Google Analytics account
requestParams += "&cid=" + clientId.toString(); // unique user identifier
requestParams += "&t=event"; // hit type = event others pageview, exception
requestParams += "&an=" + QCoreApplication::applicationName(); // app name
requestParams += "&av=" + QCoreApplication::applicationVersion(); // app version
requestParams += "&ec=" + category; // event category
requestParams += "&ea=" + eventAction; // event action
requestParams += "&aip=1"; // Anonymize IP
requestParams += "&ds=app"; // DataSource

if(sessionControl == SessionControl::Start) {
requestParams += "&sc=start"; // Start Session
}
else if (sessionControl == SessionControl::End){
requestParams += "&sc=end"; // End Session
}
// send request via post method
QNetworkReply* reply = networkManager.post(request, requestParams.toStdString().c_str());

//If this a session end, we have to wait for the network request to finish
if (sessionControl == SessionControl::End)
{
QEventLoop loop;
QObject::connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
loop.exec();
}
}

}
21 changes: 21 additions & 0 deletions GoogleAnalytics.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifndef GOOGLEANALYTICS_H
#define GOOGLEANALYTICS_H
#include <QUuid>
#include <QtNetwork/QNetworkAccessManager>

namespace GoogleAnalytics {
enum SessionControl {Start, End, None};

void Report(QString eventAction, QString category = "Action", SessionControl sessionControl = None);
void ReportStart();
void ReportLocalRun();
void ReportDesignSafeRun();
void StartSession();
void EndSession();
QUuid GetCommonClientId();
void SetTrackingId(QString trackingId);

static QString trackingId = "";
static QNetworkAccessManager networkManager;
}
#endif // GOOGLEANALYTICS_H
42 changes: 5 additions & 37 deletions InputWidgetEE_UQ.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
#include <QtNetwork/QNetworkRequest>
#include <QHostInfo>

#include <GoogleAnalytics.h>

// static pointer for global procedure set in constructor
static InputWidgetEE_UQ *theApp = 0;

Expand Down Expand Up @@ -318,29 +320,6 @@ InputWidgetEE_UQ::InputWidgetEE_UQ(RemoteService *theService, QWidget *parent)
manager->get(QNetworkRequest(QUrl("http://opensees.berkeley.edu/OpenSees/developer/bfm/use.php")));
// manager->get(QNetworkRequest(QUrl("https://simcenter.designsafe-ci.org/multiple-degrees-freedom-analytics/")));


QNetworkRequest request;
QUrl host("http://www.google-analytics.com/collect");
request.setUrl(host);
request.setHeader(QNetworkRequest::ContentTypeHeader,
"application/x-www-form-urlencoded");

// setup parameters of request
QString requestParams;
QUuid uuid = InputWidgetEE_UQ::getUserId();
QString hostname = QHostInfo::localHostName() + "." + QHostInfo::localDomainName();
requestParams += "v=1"; // version of protocol
requestParams += "&tid=UA-126303135-1"; // Google Analytics account
requestParams += "&cid=" + uuid.toString(); // unique user identifier
requestParams += "&t=event"; // hit type = event others pageview, exception
requestParams += "&an=EEUQ"; // app name
requestParams += "&av=1.0.1"; // app version
requestParams += "&ec=EEUQ"; // event category
requestParams += "&ea=start"; // event action
requestParams += "&aip=1"; // Anonymize IP

// send request via post method
manager->post(request, requestParams.toStdString().c_str());
}

InputWidgetEE_UQ::~InputWidgetEE_UQ()
Expand All @@ -353,19 +332,6 @@ void InputWidgetEE_UQ::replyFinished(QNetworkReply *pReply)
return;
}

//TODO: This code may need to be refactored and shared in SimCenterCommon
QUuid InputWidgetEE_UQ::getUserId()
{
QSettings commonSettings("SimCenter", "Common"); //These names will need to be constants to be shared
QVariant userIdSetting = commonSettings.value("userId");
if (!userIdSetting.isValid())
{
commonSettings.setValue("userId", QUuid::createUuid());
userIdSetting = commonSettings.value("userId");
}
return userIdSetting.toUuid();
}

void
InputWidgetEE_UQ::selectionChangedSlot(const QItemSelection & /*newSelection*/, const QItemSelection &/*oldSelection*/) {

Expand Down Expand Up @@ -575,6 +541,7 @@ InputWidgetEE_UQ::onRunButtonClicked() {
theRunWidget->hide();
theRunWidget->setMinimumWidth(this->width()*0.5);
theRunWidget->showLocalApplication();
GoogleAnalytics::ReportLocalRun();
}

void
Expand All @@ -584,14 +551,15 @@ InputWidgetEE_UQ::onRemoteRunButtonClicked(){
bool loggedIn = theRemoteService->isLoggedIn();

if (loggedIn == true) {

theRunWidget->hide();
theRunWidget->setMinimumWidth(this->width()*0.5);
theRunWidget->showRemoteApplication();

} else {
errorMessage("ERROR - You Need to Login");
}

GoogleAnalytics::ReportDesignSafeRun();
}

void
Expand Down
1 change: 0 additions & 1 deletion InputWidgetEE_UQ.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@ public slots:
QStackedWidget *theStackedWidget;
QNetworkAccessManager *manager;

static QUuid getUserId();
};

#endif // INPUT_WIDGET_EE_UQ_H
2 changes: 2 additions & 0 deletions SHAMotionWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ SHAMotionWidget::SHAMotionWidget(RandomVariablesContainer *theRandomVariableIW,
m_intensityMeasure = new IntensityMeasure(this);
IntensityMeasureWidget* imWidget = new IntensityMeasureWidget(*m_intensityMeasure, this);
gmToolsLayout->addWidget(imWidget, 0);
//TODO:Intensity measure widget is ambigous so it is removed temporarily
imWidget->setHidden(true);

m_selectionConfig = new RecordSelectionConfig(this);
RecordSelectionWidget* selectionWidget = new RecordSelectionWidget(*m_selectionConfig, this);
Expand Down
7 changes: 6 additions & 1 deletion main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <QFile>
#include <QTime>
#include <QTextStream>
#include <GoogleAnalytics.h>

// customMessgaeOutput code from web:
// https://stackoverflow.com/questions/4954140/how-to-redirect-qdebug-qwarning-qcritical-etc-output
Expand Down Expand Up @@ -52,10 +53,13 @@ void customMessageOutput(QtMsgType type, const QMessageLogContext &context, cons

int main(int argc, char *argv[])
{
//Setting Core Application Name, Organization and Version
//Setting Core Application Name, Organization, Version and Google Analytics Tracking Id
QCoreApplication::setApplicationName("EE-UQ");
QCoreApplication::setOrganizationName("SimCenter");
QCoreApplication::setApplicationVersion("1.1.0");
GoogleAnalytics::SetTrackingId("UA-126303135-1");
GoogleAnalytics::StartSession();
GoogleAnalytics::ReportStart();

//
// set up logging of output messages for user debugging
Expand Down Expand Up @@ -179,6 +183,7 @@ QTabWidget::pane {background-color: #ECECEC; border: 1px solid rgb(239, 239, 239
theRemoteService->logout();
thread->quit();

GoogleAnalytics::EndSession();
// done
return res;
}

0 comments on commit 09644e4

Please sign in to comment.