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.
  • Diff rendering mode:
  • inline
  • side by side

mouchak/server.py

4747
4848
49dbClient = pymongo.MongoClient()49dbClient = pymongo.MongoClient()
50
50db = dbClient[conf.DB]51db = dbClient[conf.DB]
51siteContent = db['content']52siteContent = db['content']
52siteMenu = db['menu']53siteMenu = db['menu']
54analytics_coll = db['analytics']
55
53if siteMenu.find_one() == None:56if siteMenu.find_one() == None:
54 siteMenu.insert({'customMenu': False, 'menuOrder': [], 'html': ''})57 siteMenu.insert({'customMenu': False, 'menuOrder': [], 'html': ''})
5558
340 res = os.remove(filepath)340 res = os.remove(filepath)
341 print res341 print res
342 return '200 OK'342 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
345@app.route('/robots.txt')367@app.route('/robots.txt')

mouchak/static/js/mouchak.js

12 },12 },
13 initialize: function() {13 initialize: function() {
14 _.bindAll.apply(_, [this].concat(_.functions(this)));14 _.bindAll.apply(_, [this].concat(_.functions(this)));
15 this.$pageview_counter = $('#pageview-counter');
15 },16 },
16 render: function() {17 render: function() {
17 var menu = new M.types.model.menu(M.site_content.menu);18 var menu = new M.types.model.menu(M.site_content.menu);
23 updateBreadcrumbs: function(event) {23 updateBreadcrumbs: function(event) {
24 //TODO: write code to use bootstrap's breadcrumbs to render a24 //TODO: write code to use bootstrap's breadcrumbs to render a
25 // navigational breadcrumb25 // 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);
26 }45 }
27});46});
2847
158 }158 }
159 //console.log('navclicked');159 //console.log('navclicked');
160 M.appView.navView.trigger('navclicked');160 M.appView.navView.trigger('navclicked');
161 M.appView.recordPageView(page);
161 },162 },
162 render404: function() {163 render404: function() {
163 $('.pageview').hide();164 $('.pageview').hide();

mouchak/templates/index.html

22 window.M = window.M || {};22 window.M = window.M || {};
23 M.MenuURL = function() { return "{{ url_for('insertMenu') }}"};23 M.MenuURL = function() { return "{{ url_for('insertMenu') }}"};
24 M.PageURL = function() { return "{{ url_for('insertPage') }}"; };24 M.PageURL = function() { return "{{ url_for('insertPage') }}"; };
25 M.AnalyticsURL = function() { return "{{ url_for('analytics') }}"; };
25 M.site_content = {{ content|tojson|safe }};26 M.site_content = {{ content|tojson|safe }};
26 window.onload = function() {27 window.onload = function() {
27 M.init();28 M.init();