Add ping to analytics when generating a page master
Add ping to analytics when generating a page

Will add a flag shortly so this can be disabled

file:a/sbt_cli.py -> file:b/sbt_cli.py
--- a/sbt_cli.py
+++ b/sbt_cli.py
@@ -21,7 +21,7 @@
 ADDITIONAL_HEADERS=False
 DISKCACHE='/tmp/sbtcli.cache'
 CACHE_TTL=900 # 15 mins
-
+UPDATE_ANALYTICS=True
 
 # I use this settings file to gain access to the non-public copy of my projects
 if os.path.isfile(os.path.expanduser("~/.sbtcli.settings")):
@@ -243,6 +243,27 @@
 
 
 
+def updateAnalytics(pgview):
+    ''' Send a ping to Piwik
+    
+    This helps me track which snippets people actually find useful.
+    
+    A flag to disable this will be provided later
+    '''
+    
+    if not UPDATE_ANALYTICS:
+        return False
+    
+    url = 'https://piwik.bentasker.co.uk/piwik.php?idsite=1&rec=1&' + urllib.urlencode({"url":pgview})
+    
+    
+    request = urllib2.Request(url)
+    request.add_header("User-Agent","sbt_cli")
+    request.add_header("Referer",pgview)
+    response = urllib2.urlopen(request)
+    r = response.read()
+    response.close()
+    
 
 def getJSON(url,ttl=False):
     #print "Fetching %s" % (url,)
@@ -362,6 +383,7 @@
         return False
     
     url = "%s%s" % (BASEDIR,urlpath)
+    pageurl = "%s%s" % (BASEDIR,urlpath.replace("/json","").replace(".json",".html"))
     
     # Snippets rarely, if ever, change so we can use a long ttl (30 days)
     snip = getJSON(url,ttl=2592000)
@@ -407,9 +429,10 @@
     
     
     print "HTML Link\n----------"
-    print "%s%s" % (BASEDIR,urlpath.replace("/json","").replace(".json",".html"))
+    print pageurl
     
     print ''
+    updateAnalytics(pageurl)
 
 
 
@@ -463,6 +486,7 @@
     # Now output the table
     print buildIssueTable(plist['entries'])
     
+    updateAnalytics(BASEDIR.replace("/json",""))
 
 
 def doSnippetSearch(title=False,lang=False,similar=False,searchstring=False):
@@ -523,6 +547,26 @@
     print "Search results - String: %s, title: %s, lang: %s, similarto: %s" % (searchstring,title,lang,similar)
         
     print buildIssueTable(matches)
+    
+    if not lang:
+        lang = "ANY"
+    
+    if not searchstring and title:
+        searchstring = title
+    elif not searchstring and similar:
+        searchstring = similar
+    
+    search_page = "%s/search.html#%s" % (BASEDIR.replace("/json",""),urllib.urlencode({
+        "srchtrm":searchstring,
+        "searchLang":lang,
+        "searchFields": 'TSKD'
+        }))
+    
+    print ''
+    print search_page
+    print ''
+    
+    updateAnalytics(search_page)