开发者

How to trigger Google Analytics events from Python?

开发者 https://www.devze.com 2022-12-16 12:41 出处:网络
I\'m developing a site that has a REST API and I\'d like to track the API usage using Google Analytics events.Is there a straightforward way to trigger GA events from Python that doesn\'t invo开发者_C

I'm developing a site that has a REST API and I'd like to track the API usage using Google Analytics events. Is there a straightforward way to trigger GA events from Python that doesn't invo开发者_C百科lve loading up an entire webbrowser component just to send a javascript request?


This project called PyGA is much better. Unlike "Google-Analytics for Mobile" project on github it is well documentated and has rich API.


There is an open source implementation of Google-Analytics for Mobile in python available here: http://github.com/b1tr0t/Google-Analytics-for-Mobile--python-

You can probably integrate this into your own setup by importing 'track_page_view' and providing it with the appropriate WSGI request environment.


There's a question for this in SO, Generate Google Analytics events (__utm.gif requests) serverside.

The problem is:

it seems like it's a little old and there's no documentation of how to log events, only page views.

...and you want to track events instead of page views.

When searching "server side google analytics" in google, you find the same sources presented in SO question as the more relevant ones. There is a lib though in php named Server Side Google Analytics to do it. The last commit is from Apr 28 2009, so I dont know if it's working or if it's possible to track events (and it's in php, not Python).

I know that there's an official google analytics traking solution without javascript (also check the release note from their blog) for mobile applications, and it even comes with a php library. Maybe you can "fool" their service and act like your application is a mobile one to track your events, rewriting this lib in Python? (I don't know if this violates Google's TOS, you should read it just in case).

IMHO, if google analytics is really the only way you want to do what you want and everything fails, I would try to use a webbrowser component.

As a last resource, you can try to create a simple analytics tool to track your api usage using google's documentation.

(If you think none of these suggestions are useful, I would try to search "google analytics without javascript" on google. Good luck!)


In the present you can use Google Analytics Measurement Protocol. There are some python libraries to send events, for example xoxzo.galib:

client = HitClient('UA-12345678-1')
client.send_hit('event', event_category='auth', event_action='signup')

Or google-measurement-protocol or you can write requests yourself with python-requests by this guide.


All other answers are for the outdated ga.js protocol, this library seems to implement the newer universal.js (I didn't try it yet):

https://github.com/analytics-pros/universal-analytics-python


Basically it boils down to: https://gist.github.com/canburak/1593381

For more information about the parameters etc: https://medium.com/python-programming-language/80eb9691d61f

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号