Last active
April 29, 2019 05:24
-
-
Save answerquest/1cee6b567644dfe9c4449979b9aaa2db to your computer and use it in GitHub Desktop.
python function for usage tracking using self-hosted Matomo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json, requests, platform | |
''' | |
29.April.2019 by Nikhil VJ | |
this Python 3 function sends a non-invasive simple usage ping to your self-hosted Matomo analytics site | |
No more allowing Google to track your users while they let you have some crumbs in exchange. | |
Reference: https://developer.matomo.org/api-reference/tracking-api | |
This function adds in metrics of the operating system (which was my use case). | |
you can skip all that stuff if you wish. | |
Assumptions: | |
Your Matomo installation (similar to a wordpress.org site, runs on simple PHP and MySQL) is on https://yoursite.you/matomo/ | |
See https://matomo.org/ to know more | |
Unlicence Boilerplate: | |
----------------------------------------------------------------------- | |
This is free and unencumbered software released into the public domain. | |
Anyone is free to copy, modify, publish, use, compile, sell, or | |
distribute this software, either in source code form or as a compiled | |
binary, for any purpose, commercial or non-commercial, and by any | |
means. | |
In jurisdictions that recognize copyright laws, the author or authors | |
of this software dedicate any and all copyright interest in the | |
software to the public domain. We make this dedication for the benefit | |
of the public at large and to the detriment of our heirs and | |
successors. We intend this dedication to be an overt act of | |
relinquishment in perpetuity of all present and future rights to this | |
software under copyright law. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | |
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR | |
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, | |
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | |
OTHER DEALINGS IN THE SOFTWARE. | |
For more information, please refer to <http://unlicense.org/> | |
---------------------------------------------------------------------- | |
Ok now the code! | |
''' | |
def logUse(action='launch',type='read'): | |
payload = {'idsite': 2, 'rec': 1, 'send_image':0} | |
# get 'idsite' value from your matomo project; see Image tracking link in Tracking code section | |
# up till here the payload items were mandatory. Subsequent parts are optional and you may skip to the try-except request block | |
payload['action_name'] = action | |
cvar = {} | |
cvar['1'] = ['type', type] | |
cvar['2'] = ['OS', platform.system()] | |
cvar['3'] = ['processor',platform.processor()] | |
if cvar['2'][1] == 'Linux': | |
cvar['2'][1] = platform.linux_distribution()[0] | |
cvar['4'] = ['version', platform.linux_distribution()[1] ] | |
else: | |
cvar['4'] = ['version', platform.release() ] | |
payload['_cvar'] = json.dumps(cvar) | |
try: | |
r = requests.get('https://yoursite.you/matomo/piwik.php', params=payload, verify=False, timeout=1) | |
# last arguments given to ensure your program does not waste time waiting for a callback etc | |
except requests.exceptions.RequestException as e: | |
# If the tracking action fails for whatever reason, then print some alert, or just silently ignore and pass | |
# print('exception',e) | |
pass | |
# usage: | |
logUse('saveRecord','write') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment