Skip to content

Instantly share code, notes, and snippets.

@livibetter
Created May 25, 2012 12:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save livibetter/2787831 to your computer and use it in GitHub Desktop.
Save livibetter/2787831 to your computer and use it in GitHub Desktop.
Using Google App Engine for quick visitor tracking via XMPP
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
Files in this Gist are written by Yu-Jie Lin and placed in Public Domain.
// 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;
});
});
# 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