Extend Google Analytics with Cloud Functions Stay organized with collections Save and categorize content based on your preferences.
Google Analytics provides event reports that help you understandhow users interact with your app. WithCloud Functions (1st gen), youcan access conversion events you have logged from Apple and Android devicesand trigger functions based on those events.
Only Apple platform and Android events marked as conversion eventsare currently supported byCloud Functions; Web conversion events are notcurrently available. You can specify which events are conversionevents in theEvents tab of theFirebase consoleAnalytics pane.Note:Cloud Functions for Firebase (2nd gen) does not provide support for theevents and triggers described in this guide. Because 1st gen and 2nd gen functions can coexist side-by-side in the same source file, you can stilldevelop and deploy this functionality together with 2nd gen functions.Trigger aGoogle Analytics function
Cloud Functions supports theGoogle AnalyticsAnalyticsEvent.This event is triggered whenever user activity generates a conversion event.For example, you could write a function thattriggers when thein_app_purchase event is generated, indicating that anin-app purchase has occurred.You must specify theAnalytics event thatyou want to trigger your function using thefunctions.analytics.event()method, and handle the event within theonLog()event handler:
exports.sendCouponOnPurchase=functions.analytics.event('in_app_purchase').onLog((event)=>{//...});
Access event attributes
With eachAnalytics event, you have access to all relevantparameters and user properties. These include information about the user, thedevice, the app, and geographical information for the event.For the complete list of parameters and user properties, see thefunctions.analytics reference.
For a purchase-triggered function as illustrated inthis sample,you might want to access user attributes such as the user's language and theevent's value (valueInUSD).This second attribute allows the sample function to test whether this is ahigh-value conversion event, in order to send a higher-value coupon to valuable customers.
/***Afterauserhascompletedapurchase,sendthemacouponviaFCMvalidontheirnextpurchase.*/exports.sendCouponOnPurchase=functions.analytics.event('in_app_purchase').onLog((event)=>{constuser=event.user;constuid=user.userId;//TheuserIDsetviathesetUserIdAPI.constpurchaseValue=event.valueInUSD;//AmountofthepurchaseinUSD.constuserLanguage=user.deviceInfo.userDefaultLanguage;//Theuserlanguageinlanguage-countryformat.//Forpurchasesabove500USD,wesendacouponofhighervalue.if(purchaseValue >500){returnsendHighValueCouponViaFCM(uid,userLanguage);}returnsendCouponViaFCM(uid,userLanguage);});
Next steps
To learn more about handlingAnalytics events inCloud Functions,see theGoogle Analytics documentation and thefunctions.analytics reference,and try running the code samplecoupon-on-purchase.
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-12-16 UTC.