Skip to content

Commit bbb6f74

Browse files
author
Ace Nassri
authored
GCF Analytics (GoogleCloudPlatform#759)
* Add analytics sample * Add test * Remove callback + add Node 10 sample
1 parent 28c7d70 commit bbb6f74

File tree

4 files changed

+101
-0
lines changed

4 files changed

+101
-0
lines changed

functions/firebase/index.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,23 @@ exports.makeUpperCase = (event) => {
9595
});
9696
};
9797
// [END functions_firebase_reactive]
98+
99+
// [START functions_firebase_analytics]
100+
/**
101+
* Triggered by a Google Analytics for Firebase log event.
102+
*
103+
* @param {!Object} event The Cloud Functions event.
104+
*/
105+
exports.helloAnalytics = (event) => {
106+
const resource = event.resource;
107+
console.log(`Function triggered by the following event: ${resource}`);
108+
109+
const analyticsEvent = event.data.eventDim[0];
110+
console.log(`Name: ${analyticsEvent.name}`);
111+
console.log(`Timestamp: ${new Date(analyticsEvent.timestampMicros / 1000)}`);
112+
113+
const userObj = event.data.userDim;
114+
console.log(`Device Model: ${userObj.deviceInfo.deviceModel}`);
115+
console.log(`Location: ${userObj.geoInfo.city}, ${userObj.geoInfo.country}`);
116+
};
117+
// [END functions_firebase_analytics]

functions/firebase/test/index.test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,36 @@ test(`should listen to Auth events`, t => {
107107
t.true(console.log.calledWith(`Email: me@example.com`));
108108
});
109109

110+
test.serial('should monitor Analytics', t => {
111+
const date = new Date();
112+
const event = {
113+
data: {
114+
eventDim: [{
115+
name: 'my-event',
116+
timestampMicros: `${date.valueOf()}000`
117+
}],
118+
userDim: {
119+
deviceInfo: {
120+
deviceModel: 'Pixel'
121+
},
122+
geoInfo: {
123+
city: 'London',
124+
country: 'UK'
125+
}
126+
}
127+
},
128+
resource: 'my-resource'
129+
};
130+
131+
const sample = getSample();
132+
sample.program.helloAnalytics(event);
133+
t.is(console.log.args[0][0], `Function triggered by the following event: my-resource`);
134+
t.is(console.log.args[1][0], `Name: my-event`);
135+
t.is(console.log.args[2][0], `Timestamp: ${date}`);
136+
t.is(console.log.args[3][0], `Device Model: Pixel`);
137+
t.is(console.log.args[4][0], `Location: London, UK`);
138+
});
139+
110140
test(`should update data in response to Firestore events`, t => {
111141
const sample = getSample();
112142

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]
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 the following 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]

functions/node8/test/index.test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,33 @@ 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+
const data = {
89+
eventDim: [{
90+
name: 'my-event',
91+
timestampMicros: `${date.valueOf()}000`
92+
}],
93+
userDim: {
94+
deviceInfo: {
95+
deviceModel: 'Pixel'
96+
},
97+
geoInfo: {
98+
city: 'London',
99+
country: 'UK'
100+
}
101+
}
102+
};
103+
104+
const context = {
105+
resource: 'my-resource'
106+
};
107+
108+
program.helloAnalytics(data, context);
109+
t.is(console.log.args[0][0], `Function triggered by the following event: my-resource`);
110+
t.is(console.log.args[1][0], `Name: my-event`);
111+
t.is(console.log.args[2][0], `Timestamp: ${date}`);
112+
t.is(console.log.args[3][0], `Device Model: Pixel`);
113+
t.is(console.log.args[4][0], `Location: London, UK`);
114+
});

0 commit comments

Comments
 (0)