Skip to content

Commit f82b29f

Browse files
author
Ace Nassri
authored
Add analytics sample + switch to static values in tests (GoogleCloudPlatform#1740)
Change-Id: I0b78475a513db9d85cba2a17044680a7f6dae5db
1 parent 53c87ed commit f82b29f

File tree

2 files changed

+75
-21
lines changed

2 files changed

+75
-21
lines changed

functions/firebase/main.py

+30
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
# [START functions_firebase_analytics]
16+
from datetime import datetime
17+
# [END functions_firebase_analytics]
18+
1519
# [START functions_firebase_rtdb]
1620
# [START functions_firebase_firestore]
1721
# [START functions_firebase_auth]
@@ -69,3 +73,29 @@ def hello_auth(data, context):
6973
if 'email' in data:
7074
print('Email: %s' % data["email"])
7175
# [END functions_firebase_auth]
76+
77+
78+
# [START functions_firebase_analytics]
79+
def hello_analytics(data, context):
80+
print(data)
81+
print(context)
82+
""" Triggered by a Google Analytics for Firebase log event.
83+
Args:
84+
data (dict): The event payload.
85+
context (google.cloud.functions.Context): Metadata for the event.
86+
"""
87+
trigger_resource = context.resource
88+
print(f'Function triggered by the following event: {trigger_resource}')
89+
90+
event = data["eventDim"][0]
91+
print(f'Name: {event["name"]}')
92+
93+
event_timestamp = int(event["timestampMicros"][:-6])
94+
print(f'Timestamp: {datetime.utcfromtimestamp(event_timestamp)}')
95+
96+
user_obj = data["userDim"]
97+
print(f'Device Model: {user_obj["deviceInfo"]["deviceModel"]}')
98+
99+
geo_info = user_obj["geoInfo"]
100+
print(f'Location: {geo_info["city"]}, {geo_info["country"]}')
101+
# [END functions_firebase_analytics]

functions/firebase/main_test.py

+45-21
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
from datetime import datetime
1616
import json
1717

18-
import uuid
19-
2018
import main
2119

2220

@@ -25,61 +23,87 @@ class Context(object):
2523

2624

2725
def test_rtdb(capsys):
28-
data_id = str(uuid.uuid4())
29-
resource_id = str(uuid.uuid4())
30-
3126
data = {
3227
'admin': True,
33-
'delta': {'id': data_id}
28+
'delta': {'id': 'my-data'}
3429
}
3530

3631
context = Context()
37-
context.resource = resource_id
32+
context.resource = 'my-resource'
3833

3934
main.hello_rtdb(data, context)
4035

4136
out, _ = capsys.readouterr()
4237

43-
assert ('Function triggered by change to: %s' % resource_id) in out
38+
assert 'Function triggered by change to: my-resource' in out
4439
assert 'Admin?: True' in out
45-
assert data_id in out
40+
assert 'my-data' in out
4641

4742

4843
def test_firestore(capsys):
49-
resource_id = str(uuid.uuid4())
50-
5144
context = Context()
52-
context.resource = resource_id
45+
context.resource = 'my-resource'
5346

5447
data = {
55-
'oldValue': {'uuid': str(uuid.uuid4())},
56-
'value': {'uuid': str(uuid.uuid4())}
48+
'oldValue': {'a': 1},
49+
'value': {'b': 2}
5750
}
5851

5952
main.hello_firestore(data, context)
6053

6154
out, _ = capsys.readouterr()
6255

63-
assert ('Function triggered by change to: %s' % resource_id) in out
56+
assert 'Function triggered by change to: my-resource' in out
6457
assert json.dumps(data['oldValue']) in out
6558
assert json.dumps(data['value']) in out
6659

6760

6861
def test_auth(capsys):
69-
user_id = str(uuid.uuid4())
7062
date_string = datetime.now().isoformat()
71-
email_string = '%s@%s.com' % (uuid.uuid4(), uuid.uuid4())
7263

7364
data = {
74-
'uid': user_id,
65+
'uid': 'my-user',
7566
'metadata': {'createdAt': date_string},
76-
'email': email_string
67+
'email': '[email protected]'
7768
}
7869

7970
main.hello_auth(data, None)
8071

8172
out, _ = capsys.readouterr()
8273

83-
assert user_id in out
74+
assert 'Function triggered by creation/deletion of user: my-user' in out
8475
assert date_string in out
85-
assert email_string in out
76+
assert 'Email: [email protected]' in out
77+
78+
79+
def test_analytics(capsys):
80+
timestamp = int(datetime.utcnow().timestamp())
81+
82+
data = {
83+
'eventDim': [{
84+
'name': 'my-event',
85+
'timestampMicros': f'{str(timestamp)}000000'
86+
}],
87+
'userDim': {
88+
'deviceInfo': {
89+
'deviceModel': 'Pixel'
90+
},
91+
'geoInfo': {
92+
'city': 'London',
93+
'country': 'UK'
94+
}
95+
}
96+
}
97+
98+
context = Context()
99+
context.resource = 'my-resource'
100+
101+
main.hello_analytics(data, context)
102+
103+
out, _ = capsys.readouterr()
104+
105+
assert 'Function triggered by the following event: my-resource' in out
106+
assert f'Timestamp: {datetime.utcfromtimestamp(timestamp)}' in out
107+
assert 'Name: my-event' in out
108+
assert 'Device Model: Pixel' in out
109+
assert 'Location: London, UK' in out

0 commit comments

Comments
 (0)