summaryrefslogtreecommitdiffstats
path: root/libraries/ganalytics/ganalytics.cpp
blob: e2caad50a46482ef213cb7eeb887961a40ada2ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
#include "ganalytics.h"

#include <QCoreApplication>
#include <QDataStream>
#include <QDateTime>
#include <QDebug>
#include <QLocale>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QQueue>
#include <QSettings>
#include <QTimer>
#include <QUrlQuery>
#include <QUuid>

#ifdef QT_GUI_LIB
#include <QScreen>
#include <QGuiApplication>
#endif // QT_GUI_LIB

#ifdef QT_QML_LIB
#include <QQmlEngine>
#include <QQmlContext>
#endif // QT_QML_LIB

struct QueryBuffer
{
    QUrlQuery postQuery;
    QDateTime time;
};

/**
 * Class Private
 * Private members and functions.
 */
class GAnalytics::Private : public QObject
{
    Q_OBJECT

public:
    explicit Private(GAnalytics *parent = 0);
    ~Private();

    GAnalytics *q;

    QNetworkAccessManager *networkManager;

    QQueue<QueryBuffer> messageQueue;
    QTimer timer;
    QNetworkRequest request;
    GAnalytics::LogLevel logLevel;

    QString trackingID;
    QString clientID;
    QString userID;
    QString appName;
    QString appVersion;
    QString language;
    QString screenResolution;
    QString viewportSize;

    bool isSending;

    const static int fourHours = 4 * 60 * 60 * 1000;
    const static QString dateTimeFormat;

public:
    void logMessage(GAnalytics::LogLevel level, const QString &message);

    QUrlQuery buildStandardPostQuery(const QString &type);
#ifdef QT_GUI_LIB
    QString getScreenResolution();
#endif // QT_GUI_LIB
    QString getUserAgent();
    QString getSystemInfo();
    QList<QString> persistMessageQueue();
    void readMessagesFromFile(const QList<QString> &dataList);
    QString getClientID();
    QString getUserID();
    void setUserID(const QString &userID);
    void enqueQueryWithCurrentTime(const QUrlQuery &query);
    void setIsSending(bool doSend);

signals:
    void postNextMessage();

public slots:
    void postMessage();
    void postMessageFinished();
};

const QString GAnalytics::Private::dateTimeFormat  = "yyyy,MM,dd-hh:mm::ss:zzz";

/**
 * Constructor
 * Constructs an object of class Private.
 * @param parent
 */
GAnalytics::Private::Private(GAnalytics *parent)
: QObject(parent)
, q(parent)
, networkManager(NULL)
, request(QUrl("http://www.google-analytics.com/collect"))
, logLevel(GAnalytics::Error)
, isSending(false)
{
    clientID = getClientID();
    userID = getUserID();
    language = QLocale::system().name().toLower().replace("_", "-");
#ifdef QT_GUI_LIB
    screenResolution = getScreenResolution();
#endif // QT_GUI_LIB
    request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
    appName = QCoreApplication::instance()->applicationName();
    appVersion = QCoreApplication::instance()->applicationVersion();
    request.setHeader(QNetworkRequest::UserAgentHeader, getUserAgent());
    connect(this, SIGNAL(postNextMessage()), this, SLOT(postMessage()));
    timer.start(30000);
    connect(&timer, SIGNAL(timeout()), this, SLOT(postMessage()));
}

/**
 * Destructor
 * Delete an object of class Private.
 */
GAnalytics::Private::~Private()
{
}

void GAnalytics::Private::logMessage(LogLevel level, const QString &message)
{
    if (logLevel > level)
    {
        return;
    }

    qDebug() << "[Analytics]" << message;
}

/**
 * Build the POST query. Adds all parameter to the query
 * which are used in every POST.
 * @param type      Type of POST message. The event which is to post.
 * @return query    Most used parameter in a query for a POST.
 */
QUrlQuery GAnalytics::Private::buildStandardPostQuery(const QString &type)
{
    QUrlQuery query;
    query.addQueryItem("v", "1");
    query.addQueryItem("tid", trackingID);
    query.addQueryItem("cid", clientID);
    if(!userID.isEmpty())
    {
        query.addQueryItem("uid", userID);
    }
    query.addQueryItem("t", type);
    query.addQueryItem("ul", language);

#ifdef QT_GUI_LIB
    query.addQueryItem("vp", viewportSize);
    query.addQueryItem("sr", screenResolution);
#endif // QT_GUI_LIB

    return query;
}

#ifdef QT_GUI_LIB
/**
 * Get devicese screen resolution.
 * @return      A QString like "800x600".
 */
QString GAnalytics::Private::getScreenResolution()
{
    QScreen *screen = QGuiApplication::primaryScreen();
    QSize size = screen->size();

    return QString("%1x%2").arg(size.width()).arg(size.height());
}
#endif // QT_GUI_LIB


/**
 * Try to gain information about the system where this application
 * is running. It needs to get the name and version of the operating
 * system, the language and screen resolution.
 * All this information will be send in POST messages.
 * @return agent        A QString with all the information formatted for a POST message.
 */
QString GAnalytics::Private::getUserAgent()
{
    QString locale = QLocale::system().name();
    QString system = getSystemInfo();

    return QString("%1/%2 (%3; %4) GAnalytics/1.0 (Qt/%5)").arg(appName).arg(appVersion).arg(system).arg(locale).arg(QT_VERSION_STR);
}


#ifdef Q_OS_MAC
/**
 * Only on Mac OS X
 * Get the Operating system name and version.
 * @return os   The operating system name and version in a string.
 */
QString GAnalytics::Private::getSystemInfo()
{
    QSysInfo::MacVersion version = QSysInfo::macVersion();
    QString os;
    switch (version)
    {
    case QSysInfo::MV_9:
        os = "Macintosh; Mac OS 9";
        break;
    case QSysInfo::MV_10_0:
        os = "Macintosh; Mac OS 10.0";
        break;
    case QSysInfo::MV_10_1:
        os = "Macintosh; Mac OS 10.1";
        break;
    case QSysInfo::MV_10_2:
        os = "Macintosh; Mac OS 10.2";
        break;
    case QSysInfo::MV_10_3:
        os = "Macintosh; Mac OS 10.3";
        break;
    case QSysInfo::MV_10_4:
        os = "Macintosh; Mac OS 10.4";
        break;
    case QSysInfo::MV_10_5:
        os = "Macintosh; Mac OS 10.5";
        break;
    case QSysInfo::MV_10_6:
        os = "Macintosh; Mac OS 10.6";
        break;
    case QSysInfo::MV_10_7:
        os = "Macintosh; Mac OS 10.7";
        break;
    case QSysInfo::MV_10_8:
        os = "Macintosh; Mac OS 10.8";
        break;
    case QSysInfo::MV_10_9:
        os = "Macintosh; Mac OS 10.9";
        break;
    case QSysInfo::MV_10_10:
        os = "Macintosh; Mac OS 10.10";
        break;
    case QSysInfo::MV_10_11:
        os = "Macintosh; Mac OS 10.11";
        break;
#if (QT_VERSION >= QT_VERSION_CHECK(5, 8, 0))
    case QSysInfo::MV_10_12:
        os = "Macintosh; Mac OS 10.12";
        break;
#endif
    case QSysInfo::MV_Unknown:
        os = "Macintosh; Mac OS unknown";
        break;
    case QSysInfo::MV_IOS_5_0:
        os = "iPhone; iOS 5.0";
        break;
    case QSysInfo::MV_IOS_5_1:
        os = "iPhone; iOS 5.1";
        break;
    case QSysInfo::MV_IOS_6_0:
        os = "iPhone; iOS 6.0";
        break;
    case QSysInfo::MV_IOS_6_1:
        os = "iPhone; iOS 6.1";
        break;
    case QSysInfo::MV_IOS_7_0:
        os = "iPhone; iOS 7.0";
        break;
    case QSysInfo::MV_IOS_7_1:
        os = "iPhone; iOS 7.1";
        break;
    case QSysInfo::MV_IOS_8_0:
        os = "iPhone; iOS 8.0";
        break;
    case QSysInfo::MV_IOS_8_1:
        os = "iPhone; iOS 8.1";
        break;
    case QSysInfo::MV_IOS_8_2:
        os = "iPhone; iOS 8.2";
        break;
    case QSysInfo::MV_IOS_8_3:
        os = "iPhone; iOS 8.3";
        break;
    case QSysInfo::MV_IOS_8_4:
        os = "iPhone; iOS 8.4";
        break;
    case QSysInfo::MV_IOS_9_0:
        os = "iPhone; iOS 9.0";
        break;
#if (QT_VERSION >= QT_VERSION_CHECK(5, 8, 0))
    case QSysInfo::MV_IOS_9_1:
        os = "iPhone; iOS 9.1";
        break;
    case QSysInfo::MV_IOS_9_2:
        os = "iPhone; iOS 9.2";
        break;
    case QSysInfo::MV_IOS_9_3:
        os = "iPhone; iOS 9.3";
        break;
    case QSysInfo::MV_IOS_10_0:
        os = "iPhone; iOS 10.0";
        break;
#endif
    case QSysInfo::MV_IOS:
        os = "iPhone; iOS unknown";
        break;
    default:
        os = "Macintosh";
        break;
    }
    return os;
}
#endif

#ifdef Q_OS_WIN
/**
 * Only on Windows
 * Get operating system and its version.
 * @return os   A QString containing the oprating systems name and version.
 */
QString GAnalytics::Private::getSystemInfo()
{
    QSysInfo::WinVersion version = QSysInfo::windowsVersion();
    QString os("Windows; ");
    switch (version)
    {
    case QSysInfo::WV_95:
        os += "Win 95";
        break;
    case QSysInfo::WV_98:
        os += "Win 98";
        break;
    case QSysInfo::WV_Me:
        os += "Win ME";
        break;
    case QSysInfo::WV_NT:
        os += "Win NT";
        break;
    case QSysInfo::WV_2000:
        os += "Win 2000";
        break;
    case QSysInfo::WV_2003:
        os += "Win Server 2003";
        break;
    case QSysInfo::WV_VISTA:
        os += "Win Vista";
        break;
    case QSysInfo::WV_WINDOWS7:
        os += "Win 7";
        break;
    case QSysInfo::WV_WINDOWS8:
        os += "Win 8";
        break;
    case QSysInfo::WV_WINDOWS8_1:
        os += "Win 8.1";
        break;
      case QSysInfo::WV_WINDOWS10:
        os += "Win 10";
        break;
    default:
        os = "Windows; unknown";
        break;
    }
    return os;
}
#endif

#if defined(Q_OS_ANDROID)
#include <QAndroidJniObject>

QString GAnalytics::Private::getSystemInfo()
{
    return QString("Linux; U; Android %1; %2 %3 Build/%4; %5")
            .arg(QAndroidJniObject::getStaticObjectField<jstring>("android/os/Build$VERSION", "RELEASE").toString())
            .arg(QAndroidJniObject::getStaticObjectField<jstring>("android/os/Build", "MANUFACTURER").toString())
            .arg(QAndroidJniObject::getStaticObjectField<jstring>("android/os/Build", "MODEL").toString())
            .arg(QAndroidJniObject::getStaticObjectField<jstring>("android/os/Build", "ID").toString())
            .arg(QAndroidJniObject::getStaticObjectField<jstring>("android/os/Build", "BRAND").toString());
}
#elif defined(Q_OS_LINUX)
#include <sys/utsname.h>

/**
 * Only on Unix systems.
 * Get operation system name and version.
 * @return os       A QString with the name and version of the operating system.
 */
QString GAnalytics::Private::getSystemInfo()
{
    struct utsname buf;
    uname(&buf);
    QString system(buf.sysname);
    QString release(buf.release);

    return system + "; " + release;
}
#endif


/**
 * The message queue contains a list of QueryBuffer object.
 * QueryBuffer holds a QUrlQuery object and a QDateTime object.
 * These both object are freed from the buffer object and
 * inserted as QString objects in a QList.
 * @return dataList     The list with concartinated queue data.
 */
QList<QString> GAnalytics::Private::persistMessageQueue()
{
    QList<QString> dataList;
    foreach (QueryBuffer buffer, messageQueue)
    {
        dataList << buffer.postQuery.toString();
        dataList << buffer.time.toString(dateTimeFormat);
    }

    return dataList;
}

/**
 * Reads persistent messages from a file.
 * Gets all message data as a QList<QString>.
 * Two lines in the list build a QueryBuffer object.
 */
void GAnalytics::Private::readMessagesFromFile(const QList<QString> &dataList)
{
    QListIterator<QString> iter(dataList);
    while (iter.hasNext())
    {
        QString queryString = iter.next();
        QString dateString = iter.next();
        QUrlQuery query;
        query.setQuery(queryString);
        QDateTime dateTime = QDateTime::fromString(dateString, dateTimeFormat);
        QueryBuffer buffer;
        buffer.postQuery = query;
        buffer.time = dateTime;
        messageQueue.enqueue(buffer);
    }
}

/**
 * Change the user id.
 * @param userID         A string with the user id.
 */
void GAnalytics::Private::setUserID(const QString &userID)
{
    this->userID = userID;
    QSettings settings;
    settings.setValue("GAnalytics-uid", userID);
}

/**
 * Get the user id.
 * User id once created is stored in application settings.
 * @return userID         A string with the user id.
 */
QString GAnalytics::Private::getUserID()
{
    QSettings settings;
    QString userID = settings.value("GAnalytics-uid", QString("")).toString();

    return userID;
}

/**
 * Get the client id.
 * Client id once created is stored in application settings.
 * @return clientID         A string with the client id.
 */
QString GAnalytics::Private::getClientID()
{
    QSettings settings;
    QString clientID;
    if (!settings.contains("GAnalytics-cid"))
    {
        clientID = QUuid::createUuid().toString();
        settings.setValue("GAnalytics-cid", clientID);
    }
    else
    {
        clientID = settings.value("GAnalytics-cid").toString();
    }

    return clientID;
}

/**
 * Takes a QUrlQuery object and wrapp it together with
 * a QTime object into a QueryBuffer struct. These struct
 * will be stored in the message queue.
 * @param query
 */
void GAnalytics::Private::enqueQueryWithCurrentTime(const QUrlQuery &query)
{
    QueryBuffer buffer;
    buffer.postQuery = query;
    buffer.time = QDateTime::currentDateTime();

    messageQueue.enqueue(buffer);
}

/**
 * Change status of class. Emit signal that status was changed.
 * @param doSend
 */
void GAnalytics::Private::setIsSending(bool doSend)
{
    if (doSend)
    {
        timer.stop();
    }
    else
    {
        timer.start();
    }

    bool changed = (isSending != doSend);

    isSending = doSend;

    if (changed)
    {
        emit q->isSendingChanged(isSending);
    }
}


/**
 * CONSTRUCTOR  GAnalytics
 * ------------------------------------------------------------------------------------------------------------
 * Constructs the GAnalytics Object.
 * @param parent        The application which uses this object.
 * @param trackingID
 * @param clientID
 * @param withGet       Determines wheather the messages are send with GET or POST.
 */
GAnalytics::GAnalytics(QObject *parent)
: QObject(parent)
, d(new Private(this))
{
}

GAnalytics::GAnalytics(const QString &trackingID, QObject *parent)
: QObject(parent)
, d(new Private(this))
{
    setTrackingID(trackingID);
}

/**
 * Destructor of class GAnalytics.
 */
GAnalytics::~GAnalytics()
{
    delete d;
}

void GAnalytics::setLogLevel(GAnalytics::LogLevel logLevel)
{
    if (d->logLevel != logLevel)
    {
        d->logLevel = logLevel;
        emit logLevelChanged();
    }
}

GAnalytics::LogLevel GAnalytics::logLevel() const
{
    return d->logLevel;
}

// SETTER and GETTER
void GAnalytics::setViewportSize(const QString &viewportSize)
{
    if (d->viewportSize != viewportSize)
    {
        d->viewportSize = viewportSize;
        emit viewportSizeChanged();
    }
}

QString GAnalytics::viewportSize() const
{
    return d->viewportSize;
}

void GAnalytics::setLanguage(const QString &language)
{
    if (d->language != language)
    {
        d->language = language;
        emit languageChanged();
    }
}

QString GAnalytics::language() const
{
    return d->language;
}

void GAnalytics::setTrackingID(const QString &trackingID)
{
    if (d->trackingID != trackingID)
    {
        d->trackingID = trackingID;
        emit trackingIDChanged();
    }
}

QString GAnalytics::trackingID() const
{
    return d->trackingID;
}

void GAnalytics::setSendInterval(int milliseconds)
{
    if (d->timer.interval() != milliseconds)
    {
        d->timer.setInterval(milliseconds);
        emit sendIntervalChanged();
    }
}

int GAnalytics::sendInterval() const
{
    return (d->timer.interval());
}

void GAnalytics::startSending()
{
    if (!isSending())
      emit d->postNextMessage();
}

bool GAnalytics::isSending() const
{
    return d->isSending;
}

void GAnalytics::setNetworkAccessManager(QNetworkAccessManager *networkAccessManager)
{
    if (d->networkManager != networkAccessManager)
    {
        // Delete the old network manager if it was our child
        if (d->networkManager && d->networkManager->parent() == this)
        {
            d->networkManager->deleteLater();
        }

        d->networkManager = networkAccessManager;
    }
}

QNetworkAccessManager *GAnalytics::networkAccessManager() const
{
    return d->networkManager;
}

static void appendCustomValues(QUrlQuery &query, const QVariantMap &customValues) {
  for(QVariantMap::const_iterator iter = customValues.begin(); iter != customValues.end(); ++iter) {
    query.addQueryItem(iter.key(), iter.value().toString());
  }
}


/**
* SentAppview is called when the user changed the applications view.
* Deprecated because after SDK Version 3.08 and up no more "appview" event:
* Use sendScreenView() instead
* @param appName
* @param appVersion
* @param screenName
*/
void GAnalytics::sendAppView(const QString &screenName,
                             const QVariantMap &customValues)
{
    sendScreenView(screenName, customValues);
}

/**
 * Sent screen view is called when the user changed the applications view.
 * These action of the user should be noticed and reported. Therefore
 * a QUrlQuery is build in this method. It holts all the parameter for
 * a http POST. The UrlQuery will be stored in a message Queue.
 * @param appName
 * @param appVersion
 * @param screenName
 */
void GAnalytics::sendScreenView(const QString &screenName,
                                const QVariantMap &customValues)
{
    d->logMessage(Info, QString("ScreenView: %1").arg(screenName));

    QUrlQuery query = d->buildStandardPostQuery("screenview");
    query.addQueryItem("cd", screenName);
    query.addQueryItem("an", d->appName);
    query.addQueryItem("av", d->appVersion);
    appendCustomValues(query, customValues);

    d->enqueQueryWithCurrentTime(query);
}

/**
 * This method is called whenever a button was pressed in the application.
 * A query for a POST message will be created to report this event. The
 * created query will be stored in a message queue.
 * @param eventCategory
 * @param eventAction
 * @param eventLabel
 * @param eventValue
 */
void GAnalytics::sendEvent(const QString &category, const QString &action,
                           const QString &label, const QVariant &value,
                           const QVariantMap &customValues)
{
    QUrlQuery query = d->buildStandardPostQuery("event");
    query.addQueryItem("an", d->appName);
    query.addQueryItem("av", d->appVersion);
    query.addQueryItem("ec", category);
    query.addQueryItem("ea", action);
    if (! label.isEmpty())
        query.addQueryItem("el", label);
    if (value.isValid())
        query.addQueryItem("ev", value.toString());

    appendCustomValues(query, customValues);

    d->enqueQueryWithCurrentTime(query);
}

/**
 * Method is called after an exception was raised. It builds a
 * query for a POST message. These query will be stored in a
 * message queue.
 * @param exceptionDescription
 * @param exceptionFatal
 */
void GAnalytics::sendException(const QString &exceptionDescription,
                               bool exceptionFatal,
                               const QVariantMap &customValues)
{
    QUrlQuery query = d->buildStandardPostQuery("exception");
    query.addQueryItem("an", d->appName);
    query.addQueryItem("av", d->appVersion);

    query.addQueryItem("exd", exceptionDescription);

    if (exceptionFatal)
    {
        query.addQueryItem("exf", "1");
    }
    else
    {
        query.addQueryItem("exf", "0");
    }
    appendCustomValues(query, customValues);

    d->enqueQueryWithCurrentTime(query);
}

/**
 * Session starts. This event will be sent by a POST message.
 * Query is setup in this method and stored in the message
 * queue.
 */
void GAnalytics::startSession()
{
	QVariantMap customValues;
	customValues.insert("sc", "start");
	sendEvent("Session", "Start", QString(), QVariant(), customValues);
}

/**
 * Session ends. This event will be sent by a POST message.
 * Query is setup in this method and stored in the message
 * queue.
 */
void GAnalytics::endSession()
{
	QVariantMap customValues;
	customValues.insert("sc", "end");
	sendEvent("Session", "End", QString(), QVariant(), customValues);
}

/**
 * This function is called by a timer interval.
 * The function tries to send a messages from the queue.
 * If message was successfully send then this function
 * will be called back to send next message.
 * If message queue contains more than one message then
 * the connection will kept open.
 * The message POST is asyncroniously when the server
 * answered a signal will be emitted.
 */
void GAnalytics::Private::postMessage()
{
    if (messageQueue.isEmpty())
    {
        setIsSending(false);
        return;
    }
    else
    {
        setIsSending(true);
    }

    QString connection = "close";
    if (messageQueue.count() > 1)
    {
        connection = "keep-alive";
    }

    QueryBuffer buffer = messageQueue.head();
    QDateTime sendTime = QDateTime::currentDateTime();
    qint64 timeDiff = buffer.time.msecsTo(sendTime);

    if(timeDiff > fourHours)
    {
        // too old.
        messageQueue.dequeue();
        emit postNextMessage();
        return;
    }

    buffer.postQuery.addQueryItem("qt", QString::number(timeDiff));
    request.setRawHeader("Connection", connection.toUtf8());
    request.setHeader(QNetworkRequest::ContentLengthHeader, buffer.postQuery.toString().length());

    // Create a new network access manager if we don't have one yet
    if (networkManager == NULL)
    {
        networkManager = new QNetworkAccessManager(this);
    }

    QNetworkReply *reply = networkManager->post(request, buffer.postQuery.query(QUrl::EncodeUnicode).toUtf8());
    connect(reply, SIGNAL(finished()), this, SLOT(postMessageFinished()));
}

/**
 * NetworkAccsessManager has finished to POST a message.
 * If POST message was successfully send then the message
 * query should be removed from queue.
 * SIGNAL "postMessage" will be emitted to send next message
 * if there is any.
 * If message couldn't be send then next try is when the
 * timer emits its signal.
 */
void GAnalytics::Private::postMessageFinished()
{
    QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender());

    int httpStausCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
    if (httpStausCode < 200 || httpStausCode > 299)
    {
        logMessage(GAnalytics::Error, QString("Error posting message: %s").arg(reply->errorString()));

        // An error ocurred.
        setIsSending(false);
        return;
    }
    else
    {
        logMessage(GAnalytics::Debug, "Message sent");
    }

    messageQueue.dequeue();
    emit postNextMessage();
    reply->deleteLater();
}


/**
 * Qut stream to persist class GAnalytics.
 * @param outStream
 * @param analytics
 * @return
 */
QDataStream &operator<<(QDataStream &outStream, const GAnalytics &analytics)
{
    outStream << analytics.d->persistMessageQueue();

    return outStream;
}


/**
 * In stream to read GAnalytics from file.
 * @param inStream
 * @param analytics
 * @return
 */
QDataStream &operator >>(QDataStream &inStream, GAnalytics &analytics)
{
    QList<QString> dataList;
    inStream >> dataList;
    analytics.d->readMessagesFromFile(dataList);

    return inStream;
}

#ifdef QT_QML_LIB
void GAnalytics::classBegin()
{
    // Get the network access manager from the QmlEngine
    QQmlContext *context = QQmlEngine::contextForObject(this);
    if (context)
    {
        QQmlEngine *engine = context->engine();
        setNetworkAccessManager(engine->networkAccessManager());
    }
}

void GAnalytics::componentComplete()
{
}
#endif // QT_QML_LIB

#include "ganalytics.moc"