App Infrastructure

Adding a Recently Viewed page

To generate data on what videos have been viewed by a user most recently and what videos are most popular, I needed a way of recording users’ actions.

After investigating Google Analytics which I’m already familiar with from creating blog-based sites, I decided it was too complicated for what I needed to do. The solution would need to:

  • Have a Python API
  • Be free or cheap

After some searching I found Keen.io, which allows an event to be recorded whenever a user follows a route within the app. When someone views a video, the user ID, video ID, the referring URL and the user’s IP address are recorded with this line:

keen.add_event("video_view", { "_id": user, "page": video, "referrer": url, "ip": ip })

To generate a page of recently viewed videos I have used the following route:

@app.route('/recent')
def recent():
url = request.headers.get("Referer")
ip = request.remote_addr
keen.add_event("view", { "_id": user, "page": "recent", "referrer": url, "ip": ip })
recent_videolist = keen.select_unique("video_view", target_property="page", timeframe="this_7_days")
return render_template('recent.html', recent_videolist=recent_videolist)

There is also a general event trigger that I’ll be using for analytics in the app as a whole.

Standard