Implementing Google Analytics on SharePoint online with SharePoint Framework Extensions

In this article I’m going to talk about Google Analytics and SharePoint Framework, the first is one of the most famous analytics service and the second is an amazing development way.
Microsoft has released SharePoint Framework in general availability worldwide, only to do a recap, the latter allow to build client side web part (React, Angular, jQuery, Riot….) for Office 365 SharePoint online (in this year will be also available for SharePoint On-Premise 2016), but is not all, in fact Microsoft has introduced in these days SharePoint Framework Extensions (preview), you can take a look at the official documentations and the Github project:

https://dev.office.com/sharepoint/docs/spfx/extensions/overview-extensions
https://github.com/SharePoint/sp-dev-fx-extensions

In few words gives the possibility to extend the user experience within modern pages, basically we have three Extension types, Application Customizers, Field Customizers and Command Sets, please take a look at the link above If you want to deepen the argument.
In order to achieve my goal I used a SharePoint Framework Application Customizer, the code is really easy, in fact in the onRender() method basically I inject the Google Analytics code necessary to monitor the site in the head tag, here my solution on github:

https://github.com/giuleon/js-application-analytics

Obviously you can also implement Azure Application Insights with the same approach, feel free to give your contribution is more than welcome.
The final result:

Giuliano De Luca | Blog | Google Analytics on SharePoint online
Google Analytics on SharePoint online

Source code of this web part is also available on the official GitHub of Microsoft SharePoint Framework Extensions samples & tutorials:
https://github.com/SharePoint/sp-dev-fx-extensions/tree/master/samples/js-application-analytics