📜 ⬆️ ⬇️

PyGA: using Google Analytics tracking in a backend written in Python

For the recently made me in the framework of the study of Flask microservice shares.datagreed.ru I wanted to track transitions to different API addresses. How to do this if Google Analytics is integrated into the page via JS?

So I did not know either. The search led me to the official PHP library for mobile sites that were not supposed (once) to use JavaScript.

Further my research resulted in a small python module that allows you to use page tracking (events, alas, are not supported) by Google Analytics from the server side. Regularly in the library included classes for Django and Flask. The class for Flask is currently deployed and run around me on shares.datagreed.ru , and I have not tested the class for Django yet, so bug reports are welcome.
')

Django is used like this (in views.py ):

 from pyga import DjangoGATracker def some_view(request): ga = DjangoGATracker('domain.com', 'UA-xxxx') #   GA ga.track(request) #    #<...> 


For Flask, you need to pass another session object:

 from flask import request, session #   secret_key    flask, .   flask from pyga import FlaskGATracker #<...> ga = FlaskGATracker('domain.com', 'UA-xxxx') # GA ga.track(request, session) #   


If necessary, you can use all of this at a slightly lower level:

 from pyga import GATracker ga = GATracker('domain.com', 'UA-xxxx') ga.track('/api/news/', user_session_id, ip_address, useragent) 


The project lives on the beatback . Documentation there is a draft, if someone is interested, I can add it.

Installing with pip:
 pip install -e git+https://bitbucket.org/DataGreed/pyga.git#egg=pyga-dev 


Pull requests are welcome :)

Source: https://habr.com/ru/post/157621/


All Articles