ping GA server to keep analytic sessions alive

This commit is contained in:
sfaulds 2016-10-13 11:47:43 +11:00
parent 198ece54e1
commit d9b9901a04
2 changed files with 23 additions and 9 deletions

View file

@ -9,6 +9,9 @@ from utils import window, settings, language as lang
log = logging.getLogger("EMBY."+__name__) log = logging.getLogger("EMBY."+__name__)
# for info on the metrics that can be sent to Google Analytics
# https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#events
class GoogleAnalytics(): class GoogleAnalytics():
testing = False testing = False
@ -51,13 +54,7 @@ class GoogleAnalytics():
return errorType, errorFile return errorType, errorFile
def sendEventData(self, eventCategory, eventAction, eventLabel=None): def sendEventData(self, eventCategory, eventAction, eventLabel=None):
if(settings('metricLogging') == "false"):
return
# for info on the metrics that can be sent to Google Analytics
# https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#events
# all the data we can send to Google Analytics # all the data we can send to Google Analytics
data = {} data = {}
data['v'] = '1' data['v'] = '1'
@ -82,8 +79,15 @@ class GoogleAnalytics():
if(eventLabel != None): if(eventLabel != None):
data['el'] = eventLabel # Event Label data['el'] = eventLabel # Event Label
self.sendData(data)
def sendData(self, data):
log.info("GA: " + str(data)) log.info("GA: " + str(data))
if(settings('metricLogging') == "false"):
return
if(self.testing): if(self.testing):
url = "https://www.google-analytics.com/debug/collect" # test URL url = "https://www.google-analytics.com/debug/collect" # test URL
else: else:
@ -96,4 +100,6 @@ class GoogleAnalytics():
if(self.testing): if(self.testing):
log.info("GA: " + r.text.encode('utf-8')) log.info("GA: " + r.text.encode('utf-8'))

View file

@ -4,6 +4,7 @@
import logging import logging
import sys import sys
import time
import _strptime # Workaround for threads using datetime: _striptime is locked import _strptime # Workaround for threads using datetime: _striptime is locked
from datetime import datetime from datetime import datetime
@ -41,7 +42,7 @@ class Service(object):
library_thread = None library_thread = None
last_progress = datetime.today() last_progress = datetime.today()
lastMetricPing = time.time()
def __init__(self): def __init__(self):
@ -139,7 +140,14 @@ class Service(object):
# or Kodi is shut down. # or Kodi is shut down.
self._server_online_check() self._server_online_check()
# ping metrics server to keep sessions alive
# ping every 3 min
timeSinceLastPing = time.time() - self.lastMetricPing
if(timeSinceLastPing > 180):
self.lastMetricPing = time.time()
ga = GoogleAnalytics()
ga.sendEventData("Application", "Ping")
if self.monitor.waitForAbort(1): if self.monitor.waitForAbort(1):
# Abort was requested while waiting. We should exit # Abort was requested while waiting. We should exit
break break