Let's set up some event tracking on Alchemist Camp!

This video has 3 parts:

What to track (the 5 most important user actions) - at 0:34 The tools (Keenex, Geolix, UAInspector) - at 3:53 Coding it! - at 7:17

You can see the video for a longer explanation but we'll be tracking some data that we don't necessarily have a use for, since past data can be used in the future in ways that aren't immediately apparent. However, in line with advice Emmet Shear (of Twitch fame) gave about analytics a few years ago, we're not going to get carried away tracking as much as we can. Instead we're going to track the most important 5 or so events. For Alchemist Camp, those are as follows: page views, subscription changes, page shares and referrals.

The reason we're using Keen instead of something like Mix Panel is that it's a bit lower level. This makes it both cheaper and more flexible and it's a good choice for developers in general.

Libraries we'll use

The last two libraries are for extracting useful information from IP addresses and user agent strings to enrich our event data.

We create a Plug that gets the data we want to track from our Conn , builds a JSON object and then sends it to Keen. That plug goes into the :browser pipeline in our router. Finally, after getting everything working, we make the event logging asynchronous by using Task.start .

Including the deps

Add the following to the deps portion of your mix file:

{:ex_cldr, "~> 2.0"}, {:geolix, "~> 0.16"}, {:keenex, "~> 1.0.1"}, {:ua_inspector, "~> 0.18"},

Updating your config.exs

Add the following lines and replace the keys and directories as appropriate:

config :keenex, project_id: "your_project_id", read_key: "some_key_17234871209847091274opijskldjfsA09F878F2I", write_key: "some_key_lkasf8asfj2lk15j4289jsdafdif098sdfjlkaj234" config :geolix, databases: [ %{ id: :city, adapter: Geolix.Adapter.MMDB2, source: "path_of_your_choosing/geo/GeoLite2-City.tar.gz" }, %{ id: :country, adapter: Geolix.Adapter.MMDB2, source: "path_of_your_choosing/geo/GeoLite2-Country.tar.gz" } ]

(Example Event plug and router code available for premium members)