Skip to content

Commit 5d1fea5

Browse files
authored
Merge pull request #15 from AlaaN-Smadi/master
add analytics
2 parents 1a15a9c + 6ef1ac4 commit 5d1fea5

6 files changed

Lines changed: 148 additions & 48 deletions

File tree

control/content/index.html

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,17 @@
1010

1111
<!-- JS -->
1212
<script src="../../../../scripts/buildfire.min.js"></script>
13+
<!-- build:bundleJSFiles -->
14+
<script src="../../widget/js/Analytics.js"></script>
15+
<script src="../../widget/js/analyticsKeys.js"></script>
16+
<!-- endbuild -->
1317

1418
<!-- WYSIWYG -->
1519
<script src="../../../../scripts/tinymce/tinymce.min.js"></script>
1620

21+
<script>
22+
Analytics.init();
23+
</script>
1724
<script>
1825
const debounce = (func, wait) => {
1926
let timeout;

gulpfile.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ gulp.task('clean',function(){
9595
a processes copy in the 'build' folder
9696
*/
9797
gulp.task('html', function(){
98-
return gulp.src(['widget/**/*.html','widget/**/*.htm','control/**/*.html','control/**/*.htm'],{base: '.'})
98+
return gulp.src(['widget/**/*.html','widget/**/*.htm'],{base: '.'})
9999
/// replace all the <!-- build:bundleJSFiles --> comment bodies
100100
/// with scripts.min.js with cache buster
101101
.pipe(htmlReplace({
@@ -110,6 +110,22 @@ gulp.task('html', function(){
110110
.pipe(gulp.dest(destinationFolder));
111111
});
112112

113+
gulp.task('Controlhtml', function(){
114+
return gulp.src(['control/**/*.html','control/**/*.htm'],{base: '.'})
115+
/// replace all the <!-- build:bundleJSFiles --> comment bodies
116+
/// with scripts.min.js with cache buster
117+
.pipe(htmlReplace({
118+
bundleJSFiles:"../../widget/scripts.min.js?v=" + (new Date().getTime())
119+
,bundleCSSFiles:"styles.min.css?v=" + (new Date().getTime())
120+
}))
121+
122+
/// then strip the html from any comments
123+
.pipe(minHTML({removeComments:true,collapseWhitespace:true}))
124+
125+
/// write results to the 'build' folder
126+
.pipe(gulp.dest(destinationFolder));
127+
});
128+
113129
gulp.task('resources', function(){
114130
return gulp.src(['resources/*','plugin.json'],{base: '.'})
115131
.pipe(gulp.dest(destinationFolder ));
@@ -139,7 +155,7 @@ gulp.task('zip', function () {
139155
.pipe(gulp.dest('..'));
140156
});
141157

142-
var buildTasksToRun=['html','resources','images','fonts','icons'];
158+
var buildTasksToRun=['html', 'Controlhtml','resources','images','fonts','icons'];
143159

144160
cssTasks.forEach(function(task){ buildTasksToRun.push(task.name)});
145161
jsTasks.forEach(function(task){ buildTasksToRun.push(task.name)});

widget/index.html

Lines changed: 4 additions & 2 deletions
Large diffs are not rendered by default.

widget/js/Analytics.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
2+
const Analytics = {
3+
trackView: (eventName, metaData) => {
4+
if (eventName) return buildfire.analytics.trackView(eventName, metaData);
5+
},
6+
trackAction: (eventName, metaData) => {
7+
if (eventName) return buildfire.analytics.trackAction(eventName, metaData);
8+
},
9+
registerEvent: (event, options, callback) => {
10+
if (event.title && event.key) {
11+
let _options = options.silentNotification || true;
12+
buildfire.analytics.registerEvent(event, _options, (err, res) => {
13+
if (err) return callback(err, null);
14+
return callback(null, res);
15+
});
16+
}
17+
},
18+
unregisterEvent: (key, callback) => {
19+
if (key) {
20+
buildfire.analytics.unregisterEvent(key, (err, res) => {
21+
if (err) return callback(err, null);
22+
return callback(null, res);
23+
});
24+
}
25+
},
26+
showReports: (options, callback) => {
27+
if (options.eventKey) {
28+
buildfire.analytics.showReports(options, (err, res) => {
29+
if (err) return callback(err, null);
30+
return callback(null, res);
31+
});
32+
}
33+
},
34+
35+
init: () => {
36+
Analytics.registerEvent(
37+
analyticKeys.VOTE_NUMBER,
38+
{ silentNotification: true },
39+
(err, res) => {
40+
if (err) console.error(err);
41+
}
42+
);
43+
44+
Analytics.registerEvent(
45+
analyticKeys.SUGGESTIONS_NUMBER,
46+
{ silentNotification: true },
47+
(err, res) => {
48+
if (err) console.error(err);
49+
}
50+
);
51+
},
52+
};
53+

widget/js/analyticsKeys.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
const analyticKeys = Object.freeze({
3+
VOTE_NUMBER: {
4+
title: "total number of votes",
5+
key: "Vote_Number",
6+
description: "Number of total votes for all suggestions ",
7+
},
8+
SUGGESTIONS_NUMBER: {
9+
title: "number of suggestions",
10+
key: "Suggestions_Number",
11+
description: "Number of suggestions for all useres",
12+
},
13+
14+
});

widget/js/app.js

Lines changed: 52 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ function getUser(callback) {
1313
callback(_currentUser);
1414
return;
1515
}
16-
buildfire.auth.getCurrentUser(function(err, user) {
16+
buildfire.auth.getCurrentUser(function (err, user) {
1717
if (err) {
1818
callback(null);
1919
return console.error(err);
2020
}
2121
if (!user) {
22-
return buildfire.auth.login({}, function(err, user) {
22+
return buildfire.auth.login({}, function (err, user) {
2323
if (err) {
2424
callback();
2525
return console.error(err);
@@ -35,7 +35,7 @@ function getUser(callback) {
3535
buildfire.notifications.pushNotification.subscribe({ groupName: 'suggestions' });
3636
});
3737
}
38-
getUser(function() {});
38+
getUser(function () { });
3939

4040
var config = {};
4141

@@ -44,15 +44,15 @@ function listCtrl($scope) {
4444
$scope.suggestions = [];
4545
$scope.isInitalized = false;
4646

47-
$scope.$on('suggestionAdded', function(e, obj) {
47+
$scope.$on('suggestionAdded', function (e, obj) {
4848
obj.disableUpvote = true;
4949
$scope.suggestions.unshift(obj);
5050
if (!$scope.$$phase) $scope.$apply();
5151
});
5252

5353
// added pluginInstance search to find out if social wall is available
54-
var social = function() {
55-
buildfire.pluginInstance.search({}, function(err, instances) {
54+
var social = function () {
55+
buildfire.pluginInstance.search({}, function (err, instances) {
5656
if (err) return console.error(err);
5757
if (typeof instances !== 'object') return;
5858

@@ -71,11 +71,12 @@ function listCtrl($scope) {
7171
};
7272

7373
function init() {
74+
7475
buildfire.spinner.show();
7576
$scope.suggestions = [];
7677
$scope.isInitalized = false;
7778

78-
buildfire.publicData.search({ sort: { upVoteCount: -1 } }, 'suggestion', function(err, results) {
79+
buildfire.publicData.search({ sort: { upVoteCount: -1 } }, 'suggestion', function (err, results) {
7980
social();
8081

8182
if (err) return console.error(err);
@@ -147,26 +148,26 @@ function listCtrl($scope) {
147148
$scope.goSocial = (s = {}) => {
148149
if (!s.data) return;
149150
const { title, createdOn, createdBy } = s.data;
150-
const navigateToCwByDefault = (
151-
config && !Object.keys(config).length
152-
?
153-
true
154-
:
155-
config && config.navigateToCwByDefault
156-
?
157-
config.navigateToCwByDefault
158-
:
159-
false
160-
);
151+
const navigateToCwByDefault = (
152+
config && !Object.keys(config).length
153+
?
154+
true
155+
:
156+
config && config.navigateToCwByDefault
157+
?
158+
config.navigateToCwByDefault
159+
:
160+
false
161+
);
161162
const queryString = `wid=${createdBy.displayName}-${createdOn}&wTitle=${title}`;
162-
buildfire.navigation.navigateToSocialWall({
163-
title,
164-
queryString,
165-
pluginTypeOrder: navigateToCwByDefault ? ['community', 'premium_social', 'social'] : ['premium_social', 'social', 'community']
166-
}, () => {});
163+
buildfire.navigation.navigateToSocialWall({
164+
title,
165+
queryString,
166+
pluginTypeOrder: navigateToCwByDefault ? ['community', 'premium_social', 'social'] : ['premium_social', 'social', 'community']
167+
}, () => { });
167168
};
168169

169-
$scope.showVoterModal = function(s) {
170+
$scope.showVoterModal = function (s) {
170171
var voterIds = Object.keys(s.data.upVotedBy);
171172
if (!voterIds.length) {
172173
var richContent = '<p style="padding-top: 10px; text-align: center">No votes yet!<p>';
@@ -189,15 +190,15 @@ function listCtrl($scope) {
189190
var richContent = `
190191
<div class="user-container">
191192
${users
192-
.map(user => {
193-
return `
193+
.map(user => {
194+
return `
194195
<div class="user-item">
195-
<img src=${buildfire.auth.getUserPictureUrl({ userId: user._id })} class="avatar" onerror="this.src=window._appRoot+'media/avatar.png'"/>
196+
<img alt="${user.displayName}" src=${buildfire.auth.getUserPictureUrl({ userId: user._id })} class="avatar" onerror="this.src=window._appRoot+'media/avatar.png'"/>
196197
<p class="ellipsis margin-bottom-zero">${user.displayName}</p>
197198
</div>
198199
`;
199-
})
200-
.join('')}
200+
})
201+
.join('')}
201202
</div>
202203
<style>
203204
.user-container{
@@ -230,12 +231,15 @@ function listCtrl($scope) {
230231
});
231232
};
232233

233-
$scope.upVote = function(suggestionObj) {
234-
getUser(function(user) {
234+
$scope.upVote = function (suggestionObj) {
235+
getUser(function (user) {
235236
if (!suggestionObj.data.upVotedBy) suggestionObj.data.upVotedBy = {};
236237
if (!suggestionObj.data.upVoteCount) suggestionObj.data.upVoteCount = 1;
237238

238239
if (!suggestionObj.data.upVotedBy[user._id]) {
240+
// vote
241+
Analytics.trackAction(analyticKeys.VOTE_NUMBER.key, { votes: 1, _buildfire: { aggregationValue: 1 } });
242+
239243
suggestionObj.data.upVoteCount++;
240244
suggestionObj.disableUpvote = true;
241245
suggestionObj.data.upVotedBy[user._id] = {
@@ -250,13 +254,15 @@ function listCtrl($scope) {
250254
text: user.displayName + ' upvoted your suggestion ' + suggestionObj.data.title,
251255
users: [suggestionObj.data.createdBy._id]
252256
},
253-
function(err) {
257+
function (err) {
254258
if (err) console.error(err);
255259
}
256260
);
257261
}
258262
} else {
259263
// unvote
264+
Analytics.trackAction(analyticKeys.VOTE_NUMBER.key, { votes: -1, _buildfire: { aggregationValue: -1 } });
265+
260266
suggestionObj.data.upVoteCount--;
261267
suggestionObj.disableUpvote = false;
262268
delete suggestionObj.data.upVotedBy[user._id];
@@ -267,14 +273,14 @@ function listCtrl($scope) {
267273
suggestionObj.data.upVoteCount = Object.keys(suggestionObj.data.upVotedBy).length;
268274
}
269275

270-
buildfire.publicData.update(suggestionObj.id, suggestionObj.data, 'suggestion', function(err) {
276+
buildfire.publicData.update(suggestionObj.id, suggestionObj.data, 'suggestion', function (err) {
271277
if (err) console.error(err);
272278
});
273279
});
274280
};
275281
}
276-
upvoteApp.filter('getUserImage', function() {
277-
return function(user) {
282+
upvoteApp.filter('getUserImage', function () {
283+
return function (user) {
278284
var url = './avatar.png';
279285
if (user) {
280286
url = buildfire.auth.getUserPictureUrl({ userId: user._id });
@@ -289,52 +295,54 @@ function suggestionBoxCtrl($scope, $sce, $rootScope) {
289295
$scope.popupOn = false;
290296
$scope.text = $sce.trustAsHtml(config.text);
291297

292-
window.openPopup = function() {
298+
window.openPopup = function () {
293299
$scope.popupOn = true;
294300
if (!$scope.$$phase) $scope.$apply();
295301
};
296302

297-
buildfire.datastore.get(function(err, obj) {
303+
buildfire.datastore.get(function (err, obj) {
298304
if (obj) config = obj.data;
299305
$scope.text = $sce.trustAsHtml(config.text);
300306
});
301307

302-
buildfire.datastore.onUpdate(function(obj) {
308+
buildfire.datastore.onUpdate(function (obj) {
303309
if (obj) config = obj.data;
304310
$scope.text = $sce.trustAsHtml(config.text);
305311
if (!$scope.$$phase) $scope.$apply();
306312
});
307313

308-
$scope.clearForm = function() {
314+
$scope.clearForm = function () {
309315
$scope.suggestionTitle = '';
310316
$scope.suggestionText = '';
311317
$scope.suggestionForm.$setUntouched();
312318
$scope.popupOn = false;
313319
};
314320

315-
$scope.closeForm = function() {
321+
$scope.closeForm = function () {
316322
$scope.popupOn = false;
317323
};
318324

319-
$scope.addSuggestion = function() {
325+
$scope.addSuggestion = function () {
326+
320327
if ($scope.suggestionForm.$invalid) {
321328
$scope.suggestionForm.suggestionTitle.$setTouched();
322329
$scope.suggestionForm.suggestionText.$setTouched();
323330
return;
324331
}
325332

326-
getUser(function(user) {
333+
getUser(function (user) {
327334
_addSuggestion(user, $scope.suggestionTitle, $scope.suggestionText);
328335
$scope.popupOn = false;
329336

337+
Analytics.trackAction(analyticKeys.SUGGESTIONS_NUMBER.key, { _buildfire: { aggregationValue: 1 } });
330338
buildfire.notifications.pushNotification.schedule(
331339
{
332340
title: 'New suggestion by ' + user.displayName,
333341
text: $scope.suggestionTitle,
334342
//,at: new Date()
335343
groupName: 'suggestions'
336344
},
337-
function(err) {
345+
function (err) {
338346
if (err) console.error(err);
339347
}
340348
);
@@ -360,7 +368,7 @@ function suggestionBoxCtrl($scope, $sce, $rootScope) {
360368
user: user
361369
};
362370

363-
buildfire.publicData.insert(obj, 'suggestion', function(err, obj) {
371+
buildfire.publicData.insert(obj, 'suggestion', function (err, obj) {
364372
$rootScope.$broadcast('suggestionAdded', obj);
365373
});
366374
}

0 commit comments

Comments
 (0)