From 68068c13510b02a78f5398e82b364b2ec745b805 Mon Sep 17 00:00:00 2001
From: Simba Zhang <solderzzc@gmail.com>
Date: Wed, 27 Jul 2016 21:20:26 -0700
Subject: [PATCH] Anonymous login succ/failed/relogin/logout info to GA.
 (#1312)

* There's a big warning before bot login and 2s wait for exit if the health report is turned on.
Anonymous login succ/failed/relogin/logout info without any user account info will be sent to GA.
The function is not called to wait Signal system merge.

* Fixed page view is not true.

* Removed [x] since we ditched it.
---
 configs/config.json.example              |  1 +
 configs/config.json.pokemons.example     |  1 +
 pokecli.py                               |  7 ++++++
 pokemongo_bot/health_record/__init__.py  |  3 +++
 pokemongo_bot/health_record/bot_event.py | 31 ++++++++++++++++++++++++
 requirements.txt                         |  1 +
 6 files changed, 44 insertions(+)
 create mode 100644 pokemongo_bot/health_record/__init__.py
 create mode 100644 pokemongo_bot/health_record/bot_event.py

diff --git a/configs/config.json.example b/configs/config.json.example
index afdd07e1b2..820a2fffd9 100644
--- a/configs/config.json.example
+++ b/configs/config.json.example
@@ -11,6 +11,7 @@
     "action_wait_max": 4,
     "debug": false,
     "test": false,
+    "health_record": true,
     "location_cache": true,
     "distance_unit": "km",
     "reconnecting_timeout": 15,
diff --git a/configs/config.json.pokemons.example b/configs/config.json.pokemons.example
index 2e4539a99d..5d629d10cb 100644
--- a/configs/config.json.pokemons.example
+++ b/configs/config.json.pokemons.example
@@ -11,6 +11,7 @@
     "action_wait_max": 4,
     "debug": false,
     "test": false,
+    "health_record": true,
     "location_cache": true,
     "distance_unit": "km",
     "item_filter": {
diff --git a/pokecli.py b/pokecli.py
index 5d80f89a72..a13064abe5 100755
--- a/pokecli.py
+++ b/pokecli.py
@@ -176,6 +176,13 @@ def init_config():
         type=float,
         default=15.0
     )
+    parser.add_argument(
+        "-hr",
+        "--health_record",
+        help="Send anonymous bot event to GA for bot health record. Set \"health_record\":false if you need disable it.",
+        type=bool,
+        default=True
+    )
 
     # Start to parse other attrs
     config = parser.parse_args()
diff --git a/pokemongo_bot/health_record/__init__.py b/pokemongo_bot/health_record/__init__.py
new file mode 100644
index 0000000000..a40a959a1c
--- /dev/null
+++ b/pokemongo_bot/health_record/__init__.py
@@ -0,0 +1,3 @@
+# -*- coding: utf-8 -*-
+
+from bot_event import BotEvent
diff --git a/pokemongo_bot/health_record/bot_event.py b/pokemongo_bot/health_record/bot_event.py
new file mode 100644
index 0000000000..26189c344d
--- /dev/null
+++ b/pokemongo_bot/health_record/bot_event.py
@@ -0,0 +1,31 @@
+# -*- coding: utf-8 -*-
+from UniversalAnalytics import Tracker
+from pokemongo_bot import logger
+from time import sleep
+
+class BotEvent(object):
+    def __init__(self,bot):
+        self.bot = bot
+        # UniversalAnalytics can be reviewed here:
+        # https://github.com/analytics-pros/universal-analytics-python
+        # For central TensorFlow training, forbiden any personally information
+        # report to server
+        # Review Very Carefully for the following line, forbiden ID changed PR:
+        if bot.config.health_record:
+            logger.log('Send anonymous bot health report to server, it can be disabled by config \"health_record\":false in config file', 'red')
+            logger.log('Wait for 2 seconds ', 'red')
+            sleep(3)
+            self.tracker = Tracker.create('UA-81469507-1', use_post=True)
+    # No RAW send function to be added here, to keep everything clean
+    def login_success(self):
+        if self.bot.config.health_record:
+            self.tracker.send('pageview', '/loggedin', title='succ')
+    def login_failed(self):
+        if self.bot.config.health_record:
+            self.tracker.send('pageview', '/login', title='fail')
+    def login_retry(self):
+        if self.bot.config.health_record:
+            self.tracker.send('pageview', '/relogin', title='relogin')
+    def logout(self):
+        if self.bot.config.health_record:
+            self.tracker.send('pageview', '/logout', title='logout')
diff --git a/requirements.txt b/requirements.txt
index 5c5223bf71..66ac27ee62 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -11,3 +11,4 @@ enum34==1.1.6
 pyyaml==3.11
 haversine==0.4.5
 polyline==1.3.1
+universal-analytics-python==0.2.4