2.0.0 • Published 5 years ago

cordova-plugin-analytics v2.0.0

Weekly downloads
126
License
MIT
Repository
github
Last release
5 years ago

Google Analytics Firebase Plugin

Phonegap, Cordova, Ionic, Intel XDK Google Analytics Firebase plugin for iOS/Android with full support for views, events, timings, ecommerce and social. Ionic Native on the way.

WARNING: the previous version (1.5) is broken as Google no longer supports native app analytics without Firebase

Prerequisites:

  • A Cordova 3.0+ project for iOS and/or Android
  • A Firebase project
  • Download Google-services.json (android) and GoogleService-info.plist (ios) from Firebase console Note: Ionic Demo Project on the way.

Platform SDK supported

  • iOS, using Google Analytics Firebase
  • Android, using Google Analytics Firebase

Quick start

To install this plugin, follow the Command-line Interface Guide. You can use the following command line:

  • cordova plugin add cordova-plugin-analytics --save

Make sure to review the Google Analytics terms and SDK Policy


Javascript API

Note: All success callbacks are in the form 'function () {}', and all failure callbacks are in the form 'function (err) {}' where err is a String explaining the error reason.

  • {function()} success: (Optional) success callback.
  • {function()} failure: (Optional) failure callback.

setCurrentScreen(screenName, success, error)

Sets the current screen name, which specifies the current visual context in your app. This helps identify the areas in your app where users spend their time and how they interact with your app. (https://firebase.google.com/docs/reference/android/com/google/firebase/analytics/FirebaseAnalytics.html#setCurrentScreen)

  • {String} screenName: (Required) the name of the screen to track.

Note: All screens classes will be tracked as MainActivity in android, and MainViewController in ios, as it is cordova default. You should filter by screen names. It is not possible to change screen class.

logEvent(eventName, options, success, failure)

Logs an app event. The event can have up to 25 parameters. Events with the same name must have the same parameters. Up to 500 event names are supported. Using predefined FirebaseAnalytics.Event and/or FirebaseAnalytics.Param is recommended for optimal reporting. (https://firebase.google.com/docs/reference/android/com/google/firebase/analytics/FirebaseAnalytics.html#logEvent)

  • {String} eventName: (Required) the name of the event to track (custom or predefined, see below).
  • {Object} options: (Optional) JSON object with additional options (see options).

eventName: predefined events available see Firebase:

TypeEvent nameDescription
StringADD_PAYMENT_INFOAdd Payment Info event.
StringADD_TO_CARTE-Commerce Add To Cart event.
StringADD_TO_WISHLISTE-Commerce Add To Wishlist event.
StringAPP_OPENApp Open event.
StringBEGIN_CHECKOUTE-Commerce Begin Checkout event.
StringCAMPAIGN_DETAILSLog this event to supply the referral details of a re-engagement campaign.
StringCHECKOUT_PROGRESSCheckout progress.
StringEARN_VIRTUAL_CURRENCYEarn Virtual Currency event.
StringECOMMERCE_PURCHASEE-Commerce Purchase event.
StringGENERATE_LEADGenerate Lead event.
StringJOIN_GROUPJoin Group event.
StringLEVEL_ENDLevel End event.
StringLEVEL_STARTLevel Start event.
StringLEVEL_UPLevel Up event.
StringLOGINLogin event.
StringPOST_SCOREPost Score event.
StringPRESENT_OFFERPresent Offer event.
StringPURCHASE_REFUNDE-Commerce Purchase Refund event.
StringREMOVE_FROM_CARTRemove from cart event.
StringSEARCHSearch event.
StringSELECT_CONTENTSelect Content event.
StringSET_CHECKOUT_OPTIONSet checkout option.
StringSHAREShare event.
StringSIGN_UPSign Up event.
StringSPEND_VIRTUAL_CURRENCYSpend Virtual Currency event.
StringTUTORIAL_BEGINTutorial Begin event.
StringTUTORIAL_COMPLETETutorial End event.
StringUNLOCK_ACHIEVEMENTUnlock Achievement event.
StringVIEW_ITEMView Item event.
StringVIEW_ITEM_LISTView Item List event.
StringVIEW_SEARCH_RESULTSView Search Results event.

Way to use predefined events:

analytics.logEvent(analytics.DEFAULT_EVENTS.ADD_PAYMENT_INFO, {});

eventParams: predefined params, see Firebase Params:

TypeParam nameDescription
StringACHIEVEMENT_IDGame achievement ID (String).
StringACLIDCAMPAIGN_DETAILS click ID.
StringAFFILIATIONThe store or affiliation from which this transaction occurred.
StringCAMPAIGNCAMPAIGN_DETAILS name; used for keyword analysis to identify a specific product promotion or strategic campaign.
StringCHARACTERCharacter used in game (String).
StringCHECKOUT_OPTIONSome option on a step in an ecommerce flow.
StringCHECKOUT_STEPThe checkout step (1..N).
StringCONTENTCAMPAIGN_DETAILS content; used for A/B testing and content-targeted ads to differentiate ads or links that point to the same URL.
StringCONTENT_TYPEType of content selected (String).
StringCOUPONCoupon code for a purchasable item (String).
StringCP1CAMPAIGN_DETAILS custom parameter.
StringCREATIVE_NAMEThe name of a creative used in a promotional spot.
StringCREATIVE_SLOTThe name of a creative slot.
StringCURRENCYPurchase currency in 3 letter ISO_4217 format (String).
StringDESTINATIONFlight or Travel destination (String).
StringEND_DATEThe arrival date, check-out date, or rental end date for the item (String).
StringFLIGHT_NUMBERFlight number for travel events (String).
StringGROUP_IDGroup/clan/guild id (String).
StringINDEXThe index of an item in a list.
StringITEM_BRANDItem brand.
StringITEM_CATEGORYItem category (String).
StringITEM_IDItem ID (String).
StringITEM_LISTThe list in which the item was presented to the user.
StringITEM_LOCATION_IDThe Google Place ID that corresponds to the associated item (String).
StringITEM_NAMEItem name (String).
StringITEM_VARIANTItem variant.
StringLEVELLevel in game (long).
StringLEVEL_NAMEThe name of a level in a game (String).
StringLOCATIONLocation (String).
StringMEDIUMCAMPAIGN_DETAILS medium; used to identify a medium such as email or cost-per-click (cpc).
StringMETHODA particular approach used in an operation; for example, "facebook" or "email" in the context of a sign_up or login event.
StringNUMBER_OF_NIGHTSNumber of nights staying at hotel (long).
StringNUMBER_OF_PASSENGERSNumber of passengers traveling (long).
StringNUMBER_OF_ROOMSNumber of rooms for travel events (long).
StringORIGINFlight or Travel origin (String).
StringPRICEPurchase price (double).
StringQUANTITYPurchase quantity (long).
StringSCOREScore in game (long).
StringSEARCH_TERMThe search string/keywords used (String).
StringSHIPPINGShipping cost (double).
StringSIGN_UP_METHODThis constant was deprecated. Use METHOD instead.
StringSOURCECAMPAIGN_DETAILS source; used to identify a search engine, newsletter, or other source.
StringSTART_DATEThe departure date, check-in date, or rental start date for the item (String).
StringSUCCESSThe result of an operation (long).
StringTAXTax amount (double).
StringTERMCAMPAIGN_DETAILS term; used with paid search to supply the keywords for ads.
StringTRANSACTION_IDA single ID for a ecommerce group transaction (String).
StringTRAVEL_CLASSTravel class (String).
StringVALUEA context-specific numeric value which is accumulated automatically for each event type.
StringVIRTUAL_CURRENCY_NAMEName of virtual currency type (String).

Way to use predefined params:

const params = {};
params[analytics.DEFAULT_PARAMS.CURRENCY] = 'Euro';
params['my param'] = 'my value';
analytics.logEvent(analytics.DEFAULT_EVENTS.ADD_PAYMENT_INFO, params);

resetAnalyticsData(success, error)

Clears all analytics data for this app from the device and resets the app instance id. (https://firebase.google.com/docs/reference/android/com/google/firebase/analytics/FirebaseAnalytics.html#resetAnalyticsData)

setAnalyticsCollectionEnabled(success, error)

Sets whether analytics collection is enabled for this app on this device. This setting is persisted across app sessions. By default it is enabled. (https://firebase.google.com/docs/reference/android/com/google/firebase/analytics/FirebaseAnalytics.html#setAnalyticsCollectionEnabled)

setMinimumSessionDuration(success, error)

Sets the minimum engagement time required before starting a session. The default value is 10000 (10 seconds). (https://firebase.google.com/docs/reference/android/com/google/firebase/analytics/FirebaseAnalytics.html#setMinimumSessionDuration)

setSessionTimeoutDuration(milliseconds, success, error)

Sets the duration of inactivity that terminates the current session. The default value is 1800000 (30 minutes). (https://firebase.google.com/docs/reference/android/com/google/firebase/analytics/FirebaseAnalytics.html#setSessionTimeoutDuration)

  • {number} milliseconds: (Required) The timeout in milliseconds

setUserId(userId, success, error)

Sets the user ID property. This feature must be used in accordance with Google's Privacy Policy. (https://firebase.google.com/docs/reference/android/com/google/firebase/analytics/FirebaseAnalytics.html#setUserId)

  • {string} userId: (Required) The user id

setUserProperty(userPropertyName, userPropertyValue, success, error)

Sets a user property to a given value. Up to 25 user property names are supported. Once set, user property values persist throughout the app lifecycle and across sessions. (https://firebase.google.com/docs/reference/android/com/google/firebase/analytics/FirebaseAnalytics.html#setUserProperty)

  • {string} serPropertyName: (Required) The user property name
  • {string} userPropertyValue: (Required) The user property value