diff --git a/functions/firebase/index.js b/functions/firebase/index.js index cc6280b707..69d049d86b 100644 --- a/functions/firebase/index.js +++ b/functions/firebase/index.js @@ -95,3 +95,23 @@ exports.makeUpperCase = (event) => { }); }; // [END functions_firebase_reactive] + +// [START functions_firebase_analytics] +/** + * Triggered by a Google Analytics for Firebase log event. + * + * @param {!Object} event The Cloud Functions event. + */ +exports.helloAnalytics = (event) => { + const resource = event.resource; + console.log(`Function triggered by the following event: ${resource}`); + + const analyticsEvent = event.data.eventDim[0]; + console.log(`Name: ${analyticsEvent.name}`); + console.log(`Timestamp: ${new Date(analyticsEvent.timestampMicros / 1000)}`); + + const userObj = event.data.userDim; + console.log(`Device Model: ${userObj.deviceInfo.deviceModel}`); + console.log(`Location: ${userObj.geoInfo.city}, ${userObj.geoInfo.country}`); +}; +// [END functions_firebase_analytics] diff --git a/functions/firebase/test/index.test.js b/functions/firebase/test/index.test.js index effa7be1ac..9dd6303f3b 100644 --- a/functions/firebase/test/index.test.js +++ b/functions/firebase/test/index.test.js @@ -107,6 +107,36 @@ test(`should listen to Auth events`, t => { t.true(console.log.calledWith(`Email: me@example.com`)); }); +test.serial('should monitor Analytics', t => { + const date = new Date(); + const event = { + data: { + eventDim: [{ + name: 'my-event', + timestampMicros: `${date.valueOf()}000` + }], + userDim: { + deviceInfo: { + deviceModel: 'Pixel' + }, + geoInfo: { + city: 'London', + country: 'UK' + } + } + }, + resource: 'my-resource' + }; + + const sample = getSample(); + sample.program.helloAnalytics(event); + t.is(console.log.args[0][0], `Function triggered by the following event: my-resource`); + t.is(console.log.args[1][0], `Name: my-event`); + t.is(console.log.args[2][0], `Timestamp: ${date}`); + t.is(console.log.args[3][0], `Device Model: Pixel`); + t.is(console.log.args[4][0], `Location: London, UK`); +}); + test(`should update data in response to Firestore events`, t => { const sample = getSample(); diff --git a/functions/node8/index.js b/functions/node8/index.js index 5ab4e86085..405d758ae4 100644 --- a/functions/node8/index.js +++ b/functions/node8/index.js @@ -186,3 +186,24 @@ exports.helloAuth = (data, context) => { } }; // [END functions_firebase_auth_node8] + +// [START functions_firebase_analytics] +/** + * Triggered by a Google Analytics for Firebase log event. + * + * @param {object} data The event payload. + * @param {object} context The event metadata. + */ +exports.helloAnalytics = (data, context) => { + const resource = context.resource; + console.log(`Function triggered by the following event: ${resource}`); + + const analyticsEvent = data.eventDim[0]; + console.log(`Name: ${analyticsEvent.name}`); + console.log(`Timestamp: ${new Date(analyticsEvent.timestampMicros / 1000)}`); + + const userObj = data.userDim; + console.log(`Device Model: ${userObj.deviceInfo.deviceModel}`); + console.log(`Location: ${userObj.geoInfo.city}, ${userObj.geoInfo.country}`); +}; +// [END functions_firebase_analytics] diff --git a/functions/node8/test/index.test.js b/functions/node8/test/index.test.js index 618bc84e97..c7e3d40d90 100644 --- a/functions/node8/test/index.test.js +++ b/functions/node8/test/index.test.js @@ -82,3 +82,33 @@ test.serial('should monitor Auth', t => { t.true(console.log.secondCall.args[0].includes(dateString)); t.true(console.log.thirdCall.args[0].includes(emailString)); }); + +test.serial('should monitor Analytics', t => { + const date = new Date(); + const data = { + eventDim: [{ + name: 'my-event', + timestampMicros: `${date.valueOf()}000` + }], + userDim: { + deviceInfo: { + deviceModel: 'Pixel' + }, + geoInfo: { + city: 'London', + country: 'UK' + } + } + }; + + const context = { + resource: 'my-resource' + }; + + program.helloAnalytics(data, context); + t.is(console.log.args[0][0], `Function triggered by the following event: my-resource`); + t.is(console.log.args[1][0], `Name: my-event`); + t.is(console.log.args[2][0], `Timestamp: ${date}`); + t.is(console.log.args[3][0], `Device Model: Pixel`); + t.is(console.log.args[4][0], `Location: London, UK`); +});