Commit d2a059f6b6f8eef3c76cabc085018b3fe9d116f3

  • Tree SHA1: 311493e
  • Parent SHA1: a806b2e (Merge branch 'master' of git.pantoto.org:karnataka-hybrid-micro-devices-ltd-khmdl-website/khmdl-website)
  • raw diff | raw patch
Add rudimentary analytics for KHMDL

  - add pageview counts, when a user routes to any page. the server sends back
    a total count of page views.
  
4747
4848
4949dbClient = pymongo.MongoClient()
50
5051db = dbClient[conf.DB]
5152siteContent = db['content']
5253siteMenu = db['menu']
54analytics_coll = db['analytics']
55
5356if siteMenu.find_one() == None:
5457 siteMenu.insert({'customMenu': False, 'menuOrder': [], 'html': ''})
5558
340340 res = os.remove(filepath)
341341 print res
342342 return '200 OK'
343
344# KHMDL needs analytics. So adding analytics to their website.
345@app.route('/analytics', methods=['GET', 'POST'])
346def analytics():
347 response = flask.make_response()
348 if flask.request.method == 'GET':
349 #TODO: gather analytics data from db and send back a HTML rendering it
350 pass
351 elif flask.request.method == 'POST':
352 if 'type' not in flask.request.form:
353 abort(400)
354
355 data = {}
356 data['type'] = flask.request.form['type']
357 if data['type'] == 'pageview':
358 data['page'] = flask.request.form['page']
359
360 print data
361 analytics_coll.insert(data)
362 total_hits = analytics_coll.find({'type': 'pageview'}).count()
363
364 return flask.jsonify(total_hits=total_hits)
343365
344366
345367@app.route('/robots.txt')
  
1212 },
1313 initialize: function() {
1414 _.bindAll.apply(_, [this].concat(_.functions(this)));
15 this.$pageview_counter = $('#pageview-counter');
1516 },
1617 render: function() {
1718 var menu = new M.types.model.menu(M.site_content.menu);
2323 updateBreadcrumbs: function(event) {
2424 //TODO: write code to use bootstrap's breadcrumbs to render a
2525 // navigational breadcrumb
26 },
27 recordPageView: function(page) {
28 var self = this;
29 $.ajax({
30 url: M.AnalyticsURL(),
31 type: 'POST',
32 data: {'type': 'pageview', 'page': page},
33 success: function(data) {
34 //console.log('recorded by server');
35 self.updatePageViewCounter(data);
36 },
37 error: function(jqxhr, error, status_text) {
38 console.log('Unable to post page view analytics');
39 console.log(error, status_text);
40 }
41 });
42 },
43 updatePageViewCounter: function(data) {
44 this.$pageview_counter.html(data.total_hits);
2645 }
2746});
2847
158158 }
159159 //console.log('navclicked');
160160 M.appView.navView.trigger('navclicked');
161 M.appView.recordPageView(page);
161162 },
162163 render404: function() {
163164 $('.pageview').hide();
  
2222 window.M = window.M || {};
2323 M.MenuURL = function() { return "{{ url_for('insertMenu') }}"};
2424 M.PageURL = function() { return "{{ url_for('insertPage') }}"; };
25 M.AnalyticsURL = function() { return "{{ url_for('analytics') }}"; };
2526 M.site_content = {{ content|tojson|safe }};
2627 window.onload = function() {
2728 M.init();