Skip to content

Commit c632f3e

Browse files
author
Jon Wayne Parrott
committed
Adding GAE standard analytics sample. (#366)
Change-Id: I6aa41df0e3b4bff39dc6c9a4de212c1acaa985d3
1 parent 70347c5 commit c632f3e

File tree

6 files changed

+159
-2
lines changed

6 files changed

+159
-2
lines changed

appengine/flexible/analytics/main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
GA_TRACKING_ID = os.environ['GA_TRACKING_ID']
2828

2929

30-
def track_event(category, action, label=None, value=None):
30+
def track_event(category, action, label=None, value=0):
3131
data = {
3232
'v': '1', # API Version.
3333
'tid': GA_TRACKING_ID, # Tracking ID / Property ID.
@@ -38,7 +38,7 @@ def track_event(category, action, label=None, value=None):
3838
'ec': category, # Event category.
3939
'ea': action, # Event action.
4040
'el': label, # Event label.
41-
'ev': value, # Event valueself.
41+
'ev': value, # Event value, must be an integer
4242
}
4343

4444
response = requests.post(

appengine/standard/analytics/app.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
runtime: python27
2+
api_version: 1
3+
threadsafe: yes
4+
5+
handlers:
6+
- url: .*
7+
script: main.app
8+
9+
#[START env]
10+
env_variables:
11+
GA_TRACKING_ID: your-tracking-id
12+
#[END env]
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Copyright 2016 Google Inc.
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+
from google.appengine.ext import vendor
16+
17+
# Add any libraries installed in the "lib" folder.
18+
vendor.add('lib')

appengine/standard/analytics/main.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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)
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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 re
16+
17+
import pytest
18+
import responses
19+
20+
21+
@pytest.fixture
22+
def app(monkeypatch, testbed):
23+
monkeypatch.setenv('GA_TRACKING_ID', '1234')
24+
25+
import main
26+
27+
main.app.testing = True
28+
return main.app.test_client()
29+
30+
31+
@responses.activate
32+
def test_tracking(app):
33+
responses.add(
34+
responses.POST,
35+
re.compile(r'.*'),
36+
body='{}',
37+
content_type='application/json')
38+
39+
r = app.get('/')
40+
41+
assert r.status_code == 200
42+
assert 'Event tracked' in r.data.decode('utf-8')
43+
44+
assert len(responses.calls) == 1
45+
request_body = responses.calls[0].request.body
46+
assert 'tid=1234' in request_body
47+
assert 'ea=test+action' in request_body
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Flask==0.10.1
2+
requests==2.10.0
3+
requests-toolbelt==0.6.2

0 commit comments

Comments
 (0)