From 922d3539ea5a00e96ed6d699f7cff2eb071a0b7a Mon Sep 17 00:00:00 2001 From: Nicolas Garnier Date: Thu, 2 Mar 2017 21:03:02 -0800 Subject: [PATCH] Add a Firebase Analytics "Send Survey on app update" sample Change-Id: I392b18c93f9cdeca0b503b3eca0fd70ded5ac0a8 --- survey-app-update/README.md | 36 +++++++++++++ survey-app-update/firebase.json | 1 + survey-app-update/functions/index.js | 65 ++++++++++++++++++++++++ survey-app-update/functions/package.json | 9 ++++ 4 files changed, 111 insertions(+) create mode 100644 survey-app-update/README.md create mode 100644 survey-app-update/firebase.json create mode 100644 survey-app-update/functions/index.js create mode 100644 survey-app-update/functions/package.json diff --git a/survey-app-update/README.md b/survey-app-update/README.md new file mode 100644 index 0000000..ebff5b1 --- /dev/null +++ b/survey-app-update/README.md @@ -0,0 +1,36 @@ +# Send a survey when users update your app + +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. + + +## Functions Code + +See file [functions/index.js](functions/index.js) for the trigger and the email sending code. + +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). + +The dependencies are listed in [functions/package.json](functions/package.json). + + +## Trigger rules + +The function triggers on changes to `app_update` Firebase Analytics event.. + + +## Setting up the sample + +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: + +```bash +firebase functions:config:set gmail.email="myusername@gmail.com" gmail.password="secretpassword" +``` + + +## Deploy and test + +This sample can be tested on your Android and iOS app. To test it out: + + - Set the project to your Firebase project using `firebase use --add` then select your projec tin the list. + - Deploy your project using `firebase deploy` + - Have users update your app, for instance through the play store. + - Within a few hours the emails to the survey will be sent. diff --git a/survey-app-update/firebase.json b/survey-app-update/firebase.json new file mode 100644 index 0000000..0967ef4 --- /dev/null +++ b/survey-app-update/firebase.json @@ -0,0 +1 @@ +{} diff --git a/survey-app-update/functions/index.js b/survey-app-update/functions/index.js new file mode 100644 index 0000000..44dffc6 --- /dev/null +++ b/survey-app-update/functions/index.js @@ -0,0 +1,65 @@ +/** + * Copyright 2016 Google Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +'use strict'; + +const functions = require('firebase-functions'); +const admin = require('firebase-admin'); +admin.initializeApp(functions.config().firebase); +const nodemailer = require('nodemailer'); +// Configure the email transport using the default SMTP transport and a GMail account. +// For other types of transports such as Sendgrid see https://nodemailer.com/transports/ +// TODO: Configure the `gmail.email` and `gmail.password` Google Cloud environment variables. +const gmailEmail = encodeURIComponent(functions.config().gmail.email); +const gmailPassword = encodeURIComponent(functions.config().gmail.password); +const mailTransport = nodemailer.createTransport( + `smtps://${gmailEmail}:${gmailPassword}@smtp.gmail.com`); + +// TODO: Create yor own survey. +const LINK_TO_SURVEY = 'https://goo.gl/forms/IdurnOZ66h3FtlO33'; + +/** + * After a user has updated the app. Send them a survey to compare the app with the old version. + */ +exports.sendAppUpdateSurvey = functions.analytics.event('app_update').onLog(event => { + const uid = event.data.user.userId; + + // Fetch the email of the user. In this sample we assume that the app is using Firebase Auth and + // has set the Firebase Analytics User ID to be the same as the Firebase Auth uid using the + // setUserId API. + return admin.auth().getUser(uid).then(user => { + const email = user.email; + const name = user.displayName; + return sendSurveyEmail(email, name); + }); +}); + +/** + * Sends an email pointing to the Upgraded App survey. + */ +function sendSurveyEmail(email, name) { + const mailOptions = { + from: '"MyCoolApp" ', + to: email, + subject: 'How did you like our new app?', + text: `Hey ${name}, We've seen that you have upgraded to the new version of our app! + It would be awesome if you could tell us how you like it. + Fill out our survey: ${LINK_TO_SURVEY}` + }; + + return mailTransport.sendMail(mailOptions).then(() => { + console.log('Upgrade App Survey email sent to:', email); + }); +} diff --git a/survey-app-update/functions/package.json b/survey-app-update/functions/package.json new file mode 100644 index 0000000..1b98c05 --- /dev/null +++ b/survey-app-update/functions/package.json @@ -0,0 +1,9 @@ +{ + "name": "bigquery-import-functions", + "description": "Import data to BigQuery Firebase Functions sample", + "dependencies": { + "nodemailer": "^2.4.1", + "firebase-admin": "^4.1.1", + "firebase-functions": "https://storage.googleapis.com/firebase-preview-drop/node/firebase-functions/firebase-functions-0.5.1-preview1.tgz" + } +}