Skip to content

Commit 922d353

Browse files
author
Nicolas Garnier
committed
Add a Firebase Analytics "Send Survey on app update" sample
Change-Id: I392b18c93f9cdeca0b503b3eca0fd70ded5ac0a8
1 parent d4b97bf commit 922d353

File tree

4 files changed

+111
-0
lines changed

4 files changed

+111
-0
lines changed

survey-app-update/README.md

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Send a survey when users update your app
2+
3+
This sample shows how to send a survey to your users who have updated your app. App Update is detected using a Firebase Analytics event.
4+
5+
6+
## Functions Code
7+
8+
See file [functions/index.js](functions/index.js) for the trigger and the email sending code.
9+
10+
Sending emails is performed using [nodemailer](https://www.npmjs.com/package/nodemailer) a node based Email client with comprehensive EMail server setup. For simplicity, in this sample we're showing how to send email through SMTP using a Gmail account. Be aware that Gmail has an [email sending quota](https://support.google.com/mail/answer/22839). If you are planning on sending a large number of emails you should use a professional email sending platform such as [Sendgrid](https://console.cloud.google.com/launcher/details/sendgrid-app/sendgrid-email), [Mailjet](https://www.mailjet.com/google) or [Mailgun](http://www.mailgun.com/google).
11+
12+
The dependencies are listed in [functions/package.json](functions/package.json).
13+
14+
15+
## Trigger rules
16+
17+
The function triggers on changes to `app_update` Firebase Analytics event..
18+
19+
20+
## Setting up the sample
21+
22+
Set the `gmail.email` and `gmail.password` Google Cloud environment variables to match the email and password of the Gmail account used to send emails. For this use:
23+
24+
```bash
25+
firebase functions:config:set gmail.email="[email protected]" gmail.password="secretpassword"
26+
```
27+
28+
29+
## Deploy and test
30+
31+
This sample can be tested on your Android and iOS app. To test it out:
32+
33+
- Set the project to your Firebase project using `firebase use --add` then select your projec tin the list.
34+
- Deploy your project using `firebase deploy`
35+
- Have users update your app, for instance through the play store.
36+
- Within a few hours the emails to the survey will be sent.

survey-app-update/firebase.json

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

survey-app-update/functions/index.js

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**
2+
* Copyright 2016 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
'use strict';
17+
18+
const functions = require('firebase-functions');
19+
const admin = require('firebase-admin');
20+
admin.initializeApp(functions.config().firebase);
21+
const nodemailer = require('nodemailer');
22+
// Configure the email transport using the default SMTP transport and a GMail account.
23+
// For other types of transports such as Sendgrid see https://nodemailer.com/transports/
24+
// TODO: Configure the `gmail.email` and `gmail.password` Google Cloud environment variables.
25+
const gmailEmail = encodeURIComponent(functions.config().gmail.email);
26+
const gmailPassword = encodeURIComponent(functions.config().gmail.password);
27+
const mailTransport = nodemailer.createTransport(
28+
`smtps://${gmailEmail}:${gmailPassword}@smtp.gmail.com`);
29+
30+
// TODO: Create yor own survey.
31+
const LINK_TO_SURVEY = 'https://goo.gl/forms/IdurnOZ66h3FtlO33';
32+
33+
/**
34+
* After a user has updated the app. Send them a survey to compare the app with the old version.
35+
*/
36+
exports.sendAppUpdateSurvey = functions.analytics.event('app_update').onLog(event => {
37+
const uid = event.data.user.userId;
38+
39+
// Fetch the email of the user. In this sample we assume that the app is using Firebase Auth and
40+
// has set the Firebase Analytics User ID to be the same as the Firebase Auth uid using the
41+
// setUserId API.
42+
return admin.auth().getUser(uid).then(user => {
43+
const email = user.email;
44+
const name = user.displayName;
45+
return sendSurveyEmail(email, name);
46+
});
47+
});
48+
49+
/**
50+
* Sends an email pointing to the Upgraded App survey.
51+
*/
52+
function sendSurveyEmail(email, name) {
53+
const mailOptions = {
54+
from: '"MyCoolApp" <[email protected]>',
55+
to: email,
56+
subject: 'How did you like our new app?',
57+
text: `Hey ${name}, We've seen that you have upgraded to the new version of our app!
58+
It would be awesome if you could tell us how you like it.
59+
Fill out our survey: ${LINK_TO_SURVEY}`
60+
};
61+
62+
return mailTransport.sendMail(mailOptions).then(() => {
63+
console.log('Upgrade App Survey email sent to:', email);
64+
});
65+
}
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "bigquery-import-functions",
3+
"description": "Import data to BigQuery Firebase Functions sample",
4+
"dependencies": {
5+
"nodemailer": "^2.4.1",
6+
"firebase-admin": "^4.1.1",
7+
"firebase-functions": "https://storage.googleapis.com/firebase-preview-drop/node/firebase-functions/firebase-functions-0.5.1-preview1.tgz"
8+
}
9+
}

0 commit comments

Comments
 (0)