Pushing Data to Google Analytics with Python

Google Analytics and no browser

Can Burak Çilingir
Python Programming Language
2 min readJun 25, 2013

--

I’ll share with you how to push data to Google Analytics with a simple Python example. You can collect/plot/analyse your interesting data, such as:

  • If you have a programmable door lock in the office, you can track employees’ office hours.
  • Track the commits of your team. Tag with branch/hash etc.
  • Track your own computer usage

Google Analytics is mostly used with a JavaScript snippet on the client side but if you browse the documentation, you’ll see that they also provide mobile tracking features which help you to collect data without a web browser or even if the client doesn’t support JavaScript.

Basically, mobile client displays an image which is served by a server-side script hosted by you and the script pushes the visit data to Google via GET request. The parameters of this method is listed on Troubleshooting the Tracking Code section of the documentation.

After looking at this list and PHP/Perl codes provided by Google, with some trial and error I’ve found the minimum parameters to push data.

  • utmwv is the tracking code version. The latest version at the time of this writing is 5.2.2d.
  • utmn is a unique ID generated for each GIF request to prevent caching of the GIF image. Example value in the documentation is ten digits so stick to maximum of ten digits.
  • utmp is the requested path of the current page.
  • utmac is the account string (property id).
  • utmcc contains cookie values, in which __utma is required. These 2 posts discusses this cookie in detail. “1" can be used for each part except unique visitor id. For the visitor id, you can generate something unique for each visitor which doesn’t change between visits. The safe assumption for the length of the id 10 because Google Analytics itself uses around 10 digits.

After this data is generated, simply request http://www.google-analytics.com/__utm.gif with these parameters in the query string.

--

--