|
| 1 | +# Copyright 2016 Google Inc. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import logging |
| 16 | +import os |
| 17 | + |
| 18 | +from flask import Flask |
| 19 | +import requests |
| 20 | +import requests_toolbelt.adapters.appengine |
| 21 | + |
| 22 | +# Use the App Engine Requests adapter. This makes sure that Requests uses |
| 23 | +# URLFetch. |
| 24 | +requests_toolbelt.adapters.appengine.monkeypatch() |
| 25 | + |
| 26 | +app = Flask(__name__) |
| 27 | + |
| 28 | +# Environment variables are defined in app.yaml. |
| 29 | +GA_TRACKING_ID = os.environ['GA_TRACKING_ID'] |
| 30 | + |
| 31 | + |
| 32 | +# [START track_event] |
| 33 | +def track_event(category, action, label=None, value=0): |
| 34 | + data = { |
| 35 | + 'v': '1', # API Version. |
| 36 | + 'tid': GA_TRACKING_ID, # Tracking ID / Property ID. |
| 37 | + # Anonymous Client Identifier. Ideally, this should be a UUID that |
| 38 | + # is associated with particular user, device, or browser instance. |
| 39 | + 'cid': '555', |
| 40 | + 't': 'event', # Event hit type. |
| 41 | + 'ec': category, # Event category. |
| 42 | + 'ea': action, # Event action. |
| 43 | + 'el': label, # Event label. |
| 44 | + 'ev': value, # Event value, must be an integer |
| 45 | + } |
| 46 | + |
| 47 | + response = requests.post( |
| 48 | + 'http://www.google-analytics.com/collect', data=data) |
| 49 | + |
| 50 | + # If the request fails, this will raise a RequestException. Depending |
| 51 | + # on your application's needs, this may be a non-error and can be caught |
| 52 | + # by the caller. |
| 53 | + response.raise_for_status() |
| 54 | + |
| 55 | + |
| 56 | +@app.route('/') |
| 57 | +def track_example(): |
| 58 | + track_event( |
| 59 | + category='Example', |
| 60 | + action='test action') |
| 61 | + return 'Event tracked.' |
| 62 | +# [STOP track_event] |
| 63 | + |
| 64 | + |
| 65 | +@app.errorhandler(500) |
| 66 | +def server_error(e): |
| 67 | + logging.exception('An error occurred during a request.') |
| 68 | + return """ |
| 69 | + An internal error occurred: <pre>{}</pre> |
| 70 | + See logs for full stacktrace. |
| 71 | + """.format(e), 500 |
| 72 | + |
| 73 | + |
| 74 | +if __name__ == '__main__': |
| 75 | + # This is used when running locally. Gunicorn is used to run the |
| 76 | + # application on Google App Engine. See entrypoint in app.yaml. |
| 77 | + app.run(host='127.0.0.1', port=8080, debug=True) |
0 commit comments