Commit d2a059f6b6f8eef3c76cabc085018b3fe9d116f3
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.
| | | | 47 | | 47 | |
---|
48 | | 48 | |
---|
49 | dbClient = pymongo.MongoClient() | 49 | dbClient = pymongo.MongoClient() |
---|
| | 50 | |
---|
50 | db = dbClient[conf.DB] | 51 | db = dbClient[conf.DB] |
---|
51 | siteContent = db['content'] | 52 | siteContent = db['content'] |
---|
52 | siteMenu = db['menu'] | 53 | siteMenu = db['menu'] |
---|
| | 54 | analytics_coll = db['analytics'] |
---|
| | 55 | |
---|
53 | if siteMenu.find_one() == None: | 56 | if siteMenu.find_one() == None: |
---|
54 | siteMenu.insert({'customMenu': False, 'menuOrder': [], 'html': ''}) | 57 | siteMenu.insert({'customMenu': False, 'menuOrder': [], 'html': ''}) |
---|
55 | | 58 | |
---|
… | | … | |
---|
340 | res = os.remove(filepath) | 340 | res = os.remove(filepath) |
---|
341 | print res | 341 | 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']) |
---|
| | 346 | def 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) |
---|
343 | | 365 | |
---|
344 | | 366 | |
---|
345 | @app.route('/robots.txt') | 367 | @app.route('/robots.txt') |
---|
| | | | 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 a | 24 | //TODO: write code to use bootstrap's breadcrumbs to render a |
---|
25 | // navigational breadcrumb | 25 | // 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 | }); |
---|
28 | | 47 | |
---|
… | | … | |
---|
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(); |
---|
| | | | 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(); |
---|