Skip to content

Commit d19e38d

Browse files
author
Ace Nassri
committed
Add analytics sample
1 parent b232e96 commit d19e38d

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

functions/firebase/index.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,26 @@ exports.helloAuth = (event, callback) => {
8484
callback();
8585
};
8686
// [END functions_firebase_auth]
87+
88+
// [START functions_firebase_analytics]
89+
/**
90+
* Triggered by a Google Analytics for Firebase log event.
91+
*
92+
* @param {!Object} event The Cloud Functions event.
93+
* @param {!Function} The callback function.
94+
*/
95+
exports.helloAnalytics = (event, callback) => {
96+
const resource = event.resource;
97+
console.log(`Function triggered by the following event: ${resource}`);
98+
99+
const analyticsEvent = event.data.eventDim[0];
100+
console.log(`Name: ${analyticsEvent.name}`);
101+
console.log(`Timestamp: ${new Date(analyticsEvent.timestampMicros / 1000)}`);
102+
103+
const userObj = event.data.userDim;
104+
console.log(`Device Model: ${userObj.deviceInfo.deviceModel}`);
105+
console.log(`Location: ${userObj.geoInfo.city}, ${userObj.geoInfo.country}`);
106+
107+
callback();
108+
};
109+
// [END functions_firebase_analytics]

functions/node8/index.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,3 +186,24 @@ exports.helloAuth = (data, context) => {
186186
}
187187
};
188188
// [END functions_firebase_auth_node8]
189+
190+
// [START functions_firebase_analytics_node8]
191+
/**
192+
* Triggered by a Google Analytics for Firebase log event.
193+
*
194+
* @param {object} data The event payload.
195+
* @param {object} context The event metadata.
196+
*/
197+
exports.helloAnalytics = (data, context) => {
198+
const resource = context.resource;
199+
console.log(`Function triggered by event: ${resource}`);
200+
201+
const analyticsEvent = data.eventDim[0];
202+
console.log(`Name: ${analyticsEvent.name}`);
203+
console.log(`Timestamp: ${new Date(analyticsEvent.timestampMicros / 1000)}`);
204+
205+
const userObj = data.userDim;
206+
console.log(`Device Model: ${userObj.deviceInfo.deviceModel}`);
207+
console.log(`Location: ${userObj.geoInfo.city}, ${userObj.geoInfo.country}`);
208+
};
209+
// [END functions_firebase_analytics_node8]

functions/node8/test/index.test.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,34 @@ test.serial('should monitor Auth', t => {
8282
t.true(console.log.secondCall.args[0].includes(dateString));
8383
t.true(console.log.thirdCall.args[0].includes(emailString));
8484
});
85+
86+
test.serial('should monitor Analytics', t => {
87+
const date = new Date();
88+
89+
const context = {
90+
resource: 'my-resource'
91+
};
92+
const data = {
93+
eventDim: [{
94+
name: 'my-event',
95+
timestampMicros: `${date.valueOf()}000`
96+
}],
97+
userDim: {
98+
deviceInfo: {
99+
deviceModel: 'Pixel'
100+
},
101+
geoInfo: {
102+
city: 'London',
103+
country: 'UK'
104+
}
105+
}
106+
};
107+
108+
program.helloAnalytics(data, context);
109+
110+
t.is(console.log.args[0][0], `Function triggered by event: my-resource`);
111+
t.is(console.log.args[1][0], `Name: my-event`);
112+
t.is(console.log.args[2][0], `Timestamp: ${date}`);
113+
t.is(console.log.args[3][0], `Device Model: Pixel`);
114+
t.is(console.log.args[4][0], `Location: London, UK`);
115+
});

0 commit comments

Comments
 (0)