-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
218 lines (176 loc) · 8 KB
/
main.cpp
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
// Copyright (C) 2020 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <iostream>
#include <QString>
#include <QCoreApplication>
#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QNetworkRequest>
#include <QtNetwork/QNetworkReply>
#include <QUrlQuery>
#include <QUrl>
#include <QDebug>
#include <QJsonDocument>
#include <QJsonObject>
#include <QByteArray>
#include <QJsonArray>
void handleNetworkReply(QNetworkReply *reply, QNetworkAccessManager *manager) {
// Connect signals for handling response or errors
QObject::connect(reply, &QNetworkReply::finished, [=]() {
// Handle response here (e.g., check for errors)
if (reply->error() != QNetworkReply::NoError) {
qDebug() << "HTTP Status Code:" << reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
qDebug() << "Error:" << reply->errorString();
} else {
QByteArray responseData = reply->readAll();
qDebug() << "HTTP Status Code:" << reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
qDebug() << "Response:" << responseData;
}
// Clean up
reply->deleteLater();
manager->deleteLater();
});
}
void sendPageLevelDataToUA(QUrlQuery & urlQuery) {
// Add other custom parameters
urlQuery.addQueryItem("cd2","User ID");
urlQuery.addQueryItem("cd3","Login Status");
urlQuery.addQueryItem("cd4", "Language");
urlQuery.addQueryItem("cd5","Page Name");
urlQuery.addQueryItem("cd7","Line Of Business");
urlQuery.addQueryItem("cd12","User Type");
urlQuery.addQueryItem("cd16","Platform");
urlQuery.addQueryItem("cd22","Category");
urlQuery.addQueryItem("cd27","Learning Mode");
urlQuery.addQueryItem("cd28","Tip Rank Mode");
urlQuery.addQueryItem("cd35","Discreet mode");
urlQuery.addQueryItem("cd38","Country");
urlQuery.addQueryItem("cd39","Page Location");
urlQuery.addQueryItem("cd40","Page Referrer");
urlQuery.addQueryItem("cd41","Page Title");
urlQuery.addQueryItem("cd42","Subcategory");
urlQuery.addQueryItem("cd42","Color Theme Mode"); // check the duplication in cd index (42)
urlQuery.addQueryItem("cd45","Invitation Status");
urlQuery.addQueryItem("cd46","co_user_status");
urlQuery.addQueryItem("cd47","Application Type");
urlQuery.addQueryItem("cd57","Tech Profile");
}
void sendGoogleAnalyticsHit(QString tid, QString cid, QString hitType) {
QNetworkAccessManager* manager = new QNetworkAccessManager();
// Construct the URL with the Measurement Protocol parameters
QUrl url("https://www.google-analytics.com/collect");
QUrlQuery urlQuery;
urlQuery.addQueryItem("v", "1"); // Protocol version
urlQuery.addQueryItem("tid", tid); // Property ID
urlQuery.addQueryItem("cid", cid); // Dummey Client ID
urlQuery.addQueryItem("t", hitType); // Hit type (e.g., 'event')
urlQuery.addQueryItem("dp", "/test");
urlQuery.addQueryItem("ec", "test_cat"); // Event category
urlQuery.addQueryItem("ea", "test_act"); // Event action
urlQuery.addQueryItem("el", "test_label"); // Event label
urlQuery.addQueryItem("ev", "1"); // Event value
sendPageLevelDataToUA(urlQuery); // send page-level data points
url.setQuery(urlQuery);
QNetworkRequest request(url);
// Send the request
QNetworkReply* reply = manager->get(request);
// Connect signals for handling response or errors
handleNetworkReply(reply, manager);
}
void sendPageLevelDataToGA4(QJsonObject & paramsObject) {
// Add other custom parameters
paramsObject["language"] = "Language";
paramsObject["login_status"] = "Login Status";
paramsObject["page_location"] = "Page Location";
paramsObject["line_of_business"] = "Line Of Business";
paramsObject["user_type"] = "User Type";
paramsObject["page_referrer"] = "Page Referrer";
paramsObject["page_title"] = "Page Name";
paramsObject["platform"] = "platform";
paramsObject["learning_mode"] = "Learning Mode";
paramsObject["tip_rank_mode"] = "Tip Rank Mode";
paramsObject["discreet_mode"] = "Discreet mode";
paramsObject["page_name"] = "Page Name";
paramsObject["user_id"] = "User ID";
paramsObject["country"] = "Country";
paramsObject["color_theme_mode"] = "Color Theme Mode";
paramsObject["category"] = "Category";
paramsObject["sub_category"] = "SubCategory";
paramsObject["invitation_status"] = "invitation status";
paramsObject["co_user_status"] = "co_user_status";
paramsObject["application_type"] = "Application Type";
paramsObject["consent_status"] = "Consent Status";
paramsObject["tech_profile"] = "Tech Profile";
}
void sendGA4Hit(QString measurement_id, QString api_secret, QString cid) {
QNetworkAccessManager* manager = new QNetworkAccessManager();
// Construct the URL with the GA4 Measurement API endpoint
QUrl url("https://www.google-analytics.com/mp/collect");
QUrlQuery urlQuery;
urlQuery.addQueryItem("api_secret", api_secret);
urlQuery.addQueryItem("measurement_id", measurement_id);
url.setQuery(urlQuery);
// Get the current timestamp in microseconds
qint64 timestamp_micros = QDateTime::currentMSecsSinceEpoch() * 1000;
// Create the JSON payload
QJsonObject payload;
payload["client_id"] = cid;
payload["timestamp_micros"] = timestamp_micros;
payload["non_personalized_ads"] = false;
// Create a User Properties object
QJsonObject userPropertiesObject;
// Create the 1st User Property object
QJsonObject userPropertyObject1;
userPropertyObject1["value"] = "Test User Property Object 1";
userPropertiesObject["test_user_property_1"] = userPropertyObject1;
// Create the 2nd User Property object
QJsonObject userPropertyObject2;
userPropertyObject2["value"] = "Test User Property Object 2";
userPropertiesObject["test_user_property_2"] = userPropertyObject2;
payload["user_properties"] = userPropertiesObject;
// Create an events object
QJsonArray eventsArray;
QJsonObject eventObject;
eventObject["name"] = "navigation_click";
QJsonObject paramsObject;
paramsObject["link_url"] = "link_url";
paramsObject["link_text"] = "link_text";
paramsObject["link_type"] = "click";
paramsObject["engagement_time_msec"] = "1";
paramsObject["debug_mode"] = 1; //true - show debug view
sendPageLevelDataToGA4(paramsObject);
eventObject["params"] = paramsObject;
eventsArray.append(eventObject);
payload["events"] = eventsArray;
qDebug() << "payload:" << payload;
// Convert the JSON payload to a QByteArray
QJsonDocument jsonDoc(payload);
QByteArray jsonData = jsonDoc.toJson();
// Construct the request with POST method
QNetworkRequest request(url);
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
// Send the POST request
QNetworkReply* reply = manager->post(request, jsonData);
// Connect signals for handling response or errors
handleNetworkReply(reply, manager);
}
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QString cid = "132082374.1692290545"; // dummy data - needs to get dynamic solution
// UA configuration, and finction call
QString tid = "UA-XXXXXX-XX";
sendGoogleAnalyticsHit(tid, cid, "pageview" /* or "event" */);
// GA4 configuration, and finction call
QString measurement_id = "G-XXXXXXXXXX";
QString api_secret = "XXXXXXXXXX-XXXXXXXXXXX";
sendGA4Hit(measurement_id, api_secret, cid);
// Lunching the app window
QQmlApplicationEngine engine;
engine.addImportPath(":/imports");
engine.load(QUrl(QLatin1String("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;
return app.exec();
}