How to use the firebase-functions.analytics function in firebase-functions

To help you get started, we’ve selected a few firebase-functions examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github firebase / functions-samples / developer-motivator / functions / index.js View on Github external
*/
'use strict';

const admin = require('firebase-admin');
const functions = require('firebase-functions');
admin.initializeApp();

// TODO: Make sure you configure the 'dev_motivator.device_token' Google Cloud environment variables.
const deviceToken = functions.config().dev_motivator.device_token;

/**
 * Triggers when the app is opened the first time in a user device and sends a notification to your developer device.
 *
 * The device model name, the city and the country of the user are sent in the notification message
 */
exports.appinstalled = functions.analytics.event('first_open').onLog((event) => {
  const user = event.user;
  const payload = {
    notification: {
      title: 'You have a new user \uD83D\uDE43',
      body: `${user.deviceInfo.mobileModelName} from ${user.geoInfo.city}, ${user.geoInfo.country}`,
    }
  };

  return admin.messaging().sendToDevice(deviceToken, payload);
});

/**
 * Triggers when the app is removed from the user device and sends a notification to your developer device.
 * NOTE: for this trigger to  work, you must mark the `app_remove` event as a conversion event in Firebase's
 * Analytics dashboard.
 *
github firebase / functions-samples / survey-app-update / functions / index.js View on Github external
// 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';
const LATEST_VERSION = '2.0';

/**
 * 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(async (event) => {
  const uid = event.user.userId;
  const appVerion = event.user.appInfo.appVersion;

  // Check that the user has indeed upgraded to the latest version.
  if (appVerion === LATEST_VERSION) {
    // 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.
    const user = await admin.auth().getUser(uid);
    const email = user.email;
    const name = user.displayName;
    return sendSurveyEmail(email, name);
  }
  return null;
});
github firebase / functions-samples / coupon-on-purchase / functions / index.js View on Github external
* 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);

// [START all]
/**
 * After a user has completed a purchase. Send them a coupon via FCM valid on their next purchase.
 */
// [START trigger]
exports.sendCouponOnPurchase = functions.analytics.event('in_app_purchase').onLog(event => {
// [END trigger]
  // [START attributes]
  const user = event.data.user;
  const uid = user.userId; // The user ID set via the setUserId API.
  const purchaseValue = event.data.valueInUSD; // Amount of the purchase in USD.
  const userLanguage = user.deviceInfo.userDefaultLanguage; // The user language in language-country format.
  // [END attributes]

  // For purchases above 500 USD we send a coupon of higher value.
  if (purchaseValue > 500) {
    return sendHighValueCouponViaFCM(uid, userLanguage);
  }
  return sendCouponViaFCM(uid, userLanguage);
});
// [END all]
github firebase / functions-samples / developer-motivator / functions / index.js View on Github external
title: 'You have a new user \uD83D\uDE43',
      body: `${user.deviceInfo.mobileModelName} from ${user.geoInfo.city}, ${user.geoInfo.country}`,
    }
  };

  return admin.messaging().sendToDevice(deviceToken, payload);
});

/**
 * Triggers when the app is removed from the user device and sends a notification to your developer device.
 * NOTE: for this trigger to  work, you must mark the `app_remove` event as a conversion event in Firebase's
 * Analytics dashboard.
 *
 * The device model name, the city and the country of the user are sent in the notification message
 */
exports.appremoved = functions.analytics.event('app_remove').onLog((event) => {
  const user = event.user;
  const payload = {
    notification: {
      title: 'You lost a user \uD83D\uDE1E',
      body: `${user.deviceInfo.mobileModelName} from ${user.geoInfo.city}, ${user.geoInfo.country}`,
    }
  };

  return admin.messaging().sendToDevice(deviceToken, payload);
});