Created
May 25, 2012 12:40
-
-
Save livibetter/2787831 to your computer and use it in GitHub Desktop.
Using Google App Engine for quick visitor tracking via XMPP
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
application: <APPID> | |
version: 1 | |
runtime: python | |
api_version: 1 | |
handlers: | |
- url: /qt.js | |
static_files: qt.js | |
upload: qt.js | |
- url: /qt.* | |
script: qt.py |
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
Files in this Gist are written by Yu-Jie Lin and placed in Public Domain. |
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
// Written by Yu-Jie Lin | |
$(function(){ | |
var data = { | |
url: location.href, | |
referrer: document.referrer | |
}; | |
var qs = ''; | |
$.each(data, function(k, v){ | |
qs += '&' + encodeURIComponent(k) + '=' + encodeURIComponent(v); | |
}); | |
$.getJSON('http://<APPID>.appspot.com/qt.json?callback=?' + qs, function(data){ | |
// nothing returned, nothing to do; | |
}); | |
}); |
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
# Written by Yu-Jie Lin | |
import logging | |
from google.appengine.api import xmpp | |
from google.appengine.ext import webapp | |
from google.appengine.ext.webapp.util import run_wsgi_app | |
EMAIL = '<EMAIL ADDRESS>' | |
class QuickTrackJSON(webapp.RequestHandler): | |
def get(self): | |
user_address = EMAIL | |
chat_message_sent = False | |
kv = [('ip', self.request.remote_addr)] | |
keys = ('url', 'referrer') | |
kv += map(lambda key: (key, self.request.get(key)), keys) | |
keys = ('X-AppEngine-Country', 'X-AppEngine-Region', 'X-AppEngine-City') | |
kv += [('Geo', '/'.join(list(self.request.headers.get(key, '') for key in keys)))] | |
kv = map(lambda pair: '%s = %s' % pair, kv) | |
msg = ', '.join(kv) | |
if xmpp.send_message(user_address, msg) != xmpp.NO_ERROR: | |
logging.error('XMPP message cannot be sent') | |
self.response.headers.add_header('Content-Type', 'application/javascript') | |
self.response.out.write(self.request.get('callback') + '({});') | |
class QuickTrackInvitePage(webapp.RequestHandler): | |
def get(self): | |
xmpp.send_invite(EMAIL) | |
self.response.headers.add_header('Content-Type', 'text/plain') | |
self.response.out.write('invite sent') | |
application = webapp.WSGIApplication([ | |
('/qt.json', QuickTrackJSON), | |
('/qt-invite', QuickTrackInvitePage), | |
], | |
debug=True) | |
def main(): | |
run_wsgi_app(application) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment