After I decided to make public a telegram bot to monitor bus time in Dublin (@dublin_bus_bot). Before the release I became curious to see how many people will use it (spoiler: just an handful) and I thought that would be a good idea to track the use on google analytics.

Overview

Google analytics provide a measurement protocol that can be used to track things that are different from websites (mobile apps, IOT). At the moment no elixir client exists for this protocol (and it would not be anything more than an api wrapper). My plan is to make call to the Google Analytics TK endpoint with HTTPOison but I’d prefer to not have to call the tracking function for every single bot command.

One of the feature that I prefer of the elixir are macros, macros allow to generate code at compile time. I decided to define a macro that looking like a function definition would define a function with the same body and with an additional call to the track function. I decided this approach because seems more idiomatics than using the decorator syntax typical of other languages ( @decorator at least in python and javascript).

1 2 3 4 5 6 7 8 9 10 defmetered sample_function(arg1, arg2) do IO.inspect([arg1, arg2]) end def sample_function (arg1, arg2) do track( :sample_function , [ arg1: arg1, arg2: arg2]) IO.inspect([arg1, arg2]) end

Implementation

I implemented this approach in meter to use in the telegram bot I wrote.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 @doc "" " Replace a function definition, automatically tracking every call to the function on google analytics. It also track exception with the function track_error. This macro intended use is with a set of uniform functions that can be concettualy mapped to pageviews (eg: messaging bot commands). Example: defmetered function(arg1, arg2), do: IO.inspect({arg1,arg2}) function(1,2) will call track with this parameters track(:function, [arg1: 1, arg2: 2]) Additional parameters will be loaded from the configurationd " "" defmacro defmetered ({function, _ ,args} = fundef, [ do: body]) do names = Enum.map(args, &elem(& 1 , 0 )) metered = quote do values = unquote( args |> Enum.map( fn arg -> quote do var!(unquote(arg)) end end ) ) map = Enum.zip(unquote(names), values) try do to_return = unquote(body) track(unquote(function), map) to_return rescue e -> track_error(unquote(function), map, e) raise e end end quote do def ( unquote (fundef),unquote([ do: metered])) end end

Conclusions

Elixir macros are a powerful tool to abstract away some functionality or to write DSLs. They require a bit of time to wrap head around, in particular with the context swith, but it totally worth the hassle if you can reduce the clutter in your code base.