Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add analytics sample #1740

Merged
merged 2 commits into from
Oct 12, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions functions/firebase/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# [START functions_firebase_analytics]
from datetime import datetime
# [END functions_firebase_analytics]

# [START functions_firebase_rtdb]
# [START functions_firebase_firestore]
# [START functions_firebase_auth]
Expand Down Expand Up @@ -69,3 +73,29 @@ def hello_auth(data, context):
if 'email' in data:
print('Email: %s' % data["email"])
# [END functions_firebase_auth]


# [START functions_firebase_analytics]
def hello_analytics(data, context):
print(data)
print(context)
""" Triggered by a Google Analytics for Firebase log event.
Args:
data (dict): The event payload.
context (google.cloud.functions.Context): Metadata for the event.
"""
trigger_resource = context.resource
print(f'Function triggered by the following event: {trigger_resource}')

event = data["eventDim"][0]
print(f'Name: {event["name"]}')

event_timestamp = int(event["timestampMicros"][:-6])
print(f'Timestamp: {datetime.utcfromtimestamp(event_timestamp)}')

user_obj = data["userDim"]
print(f'Device Model: {user_obj["deviceInfo"]["deviceModel"]}')

geo_info = user_obj["geoInfo"]
print(f'Location: {geo_info["city"]}, {geo_info["country"]}')
# [END functions_firebase_analytics]
66 changes: 45 additions & 21 deletions functions/firebase/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
from datetime import datetime
import json

import uuid

import main


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


def test_rtdb(capsys):
data_id = str(uuid.uuid4())
resource_id = str(uuid.uuid4())

data = {
'admin': True,
'delta': {'id': data_id}
'delta': {'id': 'my-data'}
}

context = Context()
context.resource = resource_id
context.resource = 'my-resource'

main.hello_rtdb(data, context)

out, _ = capsys.readouterr()

assert ('Function triggered by change to: %s' % resource_id) in out
assert 'Function triggered by change to: my-resource' in out
assert 'Admin?: True' in out
assert data_id in out
assert 'my-data' in out


def test_firestore(capsys):
resource_id = str(uuid.uuid4())

context = Context()
context.resource = resource_id
context.resource = 'my-resource'

data = {
'oldValue': {'uuid': str(uuid.uuid4())},
'value': {'uuid': str(uuid.uuid4())}
'oldValue': {'a': 1},
'value': {'b': 2}
}

main.hello_firestore(data, context)

out, _ = capsys.readouterr()

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


def test_auth(capsys):
user_id = str(uuid.uuid4())
date_string = datetime.now().isoformat()
email_string = '%s@%s.com' % (uuid.uuid4(), uuid.uuid4())

data = {
'uid': user_id,
'uid': 'my-user',
'metadata': {'createdAt': date_string},
'email': email_string
'email': '[email protected]'
}

main.hello_auth(data, None)

out, _ = capsys.readouterr()

assert user_id in out
assert 'Function triggered by creation/deletion of user: my-user' in out
assert date_string in out
assert email_string in out
assert 'Email: [email protected]' in out


def test_analytics(capsys):
timestamp = int(datetime.utcnow().timestamp())

data = {
'eventDim': [{
'name': 'my-event',
'timestampMicros': f'{str(timestamp)}000000'
}],
'userDim': {
'deviceInfo': {
'deviceModel': 'Pixel'
},
'geoInfo': {
'city': 'London',
'country': 'UK'
}
}
}

context = Context()
context.resource = 'my-resource'

main.hello_analytics(data, context)

out, _ = capsys.readouterr()

assert 'Function triggered by the following event: my-resource' in out
assert f'Timestamp: {datetime.utcfromtimestamp(timestamp)}' in out
assert 'Name: my-event' in out
assert 'Device Model: Pixel' in out
assert 'Location: London, UK' in out