|
| 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 | +} |
0 commit comments