OpenShot Video Editor  2.0.0
metrics.py
Go to the documentation of this file.
1 ##
2 #
3 # @file
4 # @brief This file sends anonymous application metrics and errors over HTTP
5 # @author Jonathan Thomas <jonathan@openshot.org>
6 #
7 # @section LICENSE
8 #
9 # Copyright (c) 2008-2016 OpenShot Studios, LLC
10 # (http://www.openshotstudios.com). This file is part of
11 # OpenShot Video Editor (http://www.openshot.org), an open-source project
12 # dedicated to delivering high quality video editing and animation solutions
13 # to the world.
14 #
15 # OpenShot Video Editor is free software: you can redistribute it and/or modify
16 # it under the terms of the GNU General Public License as published by
17 # the Free Software Foundation, either version 3 of the License, or
18 # (at your option) any later version.
19 #
20 # OpenShot Video Editor is distributed in the hope that it will be useful,
21 # but WITHOUT ANY WARRANTY; without even the implied warranty of
22 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 # GNU General Public License for more details.
24 #
25 # You should have received a copy of the GNU General Public License
26 # along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
27 #
28 
29 import httplib2
30 import platform
31 import threading
32 import urllib.parse
33 from copy import deepcopy
34 from classes import info
35 from classes import language
36 from classes.logger import log
37 from classes import settings
38 import openshot
39 
40 from PyQt5.QtCore import QT_VERSION_STR
41 from PyQt5.Qt import PYQT_VERSION_STR
42 
43 
44 # Get libopenshot version
45 libopenshot_version = openshot.GetVersion()
46 
47 # Get settings
49 
50 # Determine OS version
51 os_version = "X11; Linux %s" % platform.machine()
52 linux_distro = "None"
53 try:
54  if platform.system() == "Darwin":
55  v = platform.mac_ver()
56  os_version = "Macintosh; Intel Mac OS X %s" % v[0].replace(".", "_")
57  linux_distro = "OS X %s" % v[0]
58 
59  elif platform.system() == "Windows":
60  v = platform.win32_ver()
61  # TODO: Upgrade windows python (on build server) version to 3.5, so it correctly identifies Windows 10
62  os_version = "Windows NT %s; %s" % (v[0], v[1])
63  linux_distro = "Windows %s" % "-".join(platform.win32_ver())
64 
65  elif platform.system() == "Linux":
66  # Get the distro name and version (if any)
67  linux_distro = "-".join(platform.linux_distribution())
68 
69 except Exception as Ex:
70  log.error("Error determing OS version in metrics.py")
71 
72 # Build user-agent
73 user_agent = "Mozilla/5.0 (%s) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36" % os_version
74 
75 params = {
76  "cid" : s.get("unique_install_id"), # Unique install ID
77  "v" : 1, # Google Measurement API version
78  "tid" : "UA-4381101-5", # Google Analytic Tracking ID
79  "an" : info.PRODUCT_NAME, # App Name
80  "aip" : 1, # Anonymize IP
81  "aid" : "org.openshot.%s" % info.NAME, # App ID
82  "av" : info.VERSION, # App Version
83  "ul" : language.get_current_locale().replace('_','-').lower(), # Current Locale
84  "ua" : user_agent, # Custom User Agent (for OS, Processor, and OS version)
85  "cd1" : libopenshot_version.ToString(), # Dimension 1: libopenshot version
86  "cd2" : platform.python_version(), # Dimension 2: python version (i.e. 3.4.3)
87  "cd3" : QT_VERSION_STR, # Dimension 3: qt5 version (i.e. 5.2.1)
88  "cd4" : PYQT_VERSION_STR, # Dimension 4: pyqt5 version (i.e. 5.2.1)
89  "cd5" : linux_distro
90 }
91 
92 ##
93 # Track a GUI screen being shown
94 def track_metric_screen(screen_name):
95  metric_params = deepcopy(params)
96  metric_params["t"] = "screenview"
97  metric_params["cd"] = screen_name
98 
99  t = threading.Thread(target=send_metric, args=[metric_params])
100  t.start()
101 
102 ##
103 # Track a GUI screen being shown
104 def track_metric_event(event_action, event_label, event_category="General", event_value=0):
105  metric_params = deepcopy(params)
106  metric_params["t"] = "event"
107  metric_params["ec"] = event_category
108  metric_params["ea"] = event_action
109  metric_params["el"] = event_label
110  metric_params["ev"] = event_value
111 
112  t = threading.Thread(target=send_metric, args=[metric_params])
113  t.start()
114 
115 ##
116 # Track an error has occurred
117 def track_metric_error(error_name, is_fatal=False):
118  metric_params = deepcopy(params)
119  metric_params["t"] = "exception"
120  metric_params["exd"] = error_name
121  metric_params["exf"] = 0
122  if is_fatal:
123  metric_params["exf"] = 1
124 
125  t = threading.Thread(target=send_metric, args=[metric_params])
126  t.start()
127 
128 ##
129 # Track a GUI screen being shown
130 def track_metric_session(is_start=True):
131  metric_params = deepcopy(params)
132  metric_params["t"] = "screenview"
133  metric_params["sc"] = "start"
134  metric_params["cd"] = "launch-app"
135  if not is_start:
136  metric_params["sc"] = "end"
137  metric_params["cd"] = "close-app"
138 
139  t = threading.Thread(target=send_metric, args=[metric_params])
140  t.start()
141 
142 ##
143 # Send anonymous metric over HTTP for tracking
144 def send_metric(params):
145  # Check if the user wants to send metrics and errors
146  if s.get("send_metrics"):
147 
148  url_params = urllib.parse.urlencode(params)
149  url = "http://www.google-analytics.com/collect?%s" % url_params
150 
151  # Send metric HTTP data
152  try:
153  resp, content = httplib2.Http(timeout=3).request(url, headers={"user-agent": user_agent})
154  log.info("Track metric: %s (%s)" % (resp, content))
155 
156  except Exception as Ex:
157  log.error("Failed to Track metric: %s" % (Ex))
def send_metric(params)
Send anonymous metric over HTTP for tracking.
Definition: metrics.py:144
def track_metric_session(is_start=True)
Track a GUI screen being shown.
Definition: metrics.py:130
def get_current_locale()
Get the current locale name from the current system.
Definition: language.py:110
def track_metric_event(event_action, event_label, event_category="General", event_value=0)
Track a GUI screen being shown.
Definition: metrics.py:104
def get_settings()
Get the current QApplication&#39;s settings instance.
Definition: settings.py:43
def track_metric_screen(screen_name)
Track a GUI screen being shown.
Definition: metrics.py:94
def track_metric_error(error_name, is_fatal=False)
Track an error has occurred.
Definition: metrics.py:117