Skip to content
This repository was archived by the owner on Apr 4, 2023. It is now read-only.

Commit 63da64c

Browse files
#125 Analytics API
1 parent bd4c1bf commit 63da64c

File tree

11 files changed

+234
-25
lines changed

11 files changed

+234
-25
lines changed

.npmignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
platforms/android/libraryproject/
2-
docs
2+
docs
3+
README.md
4+
CHANGELOG.md

CHANGELOG.md

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,30 @@
11
<img src="docs/images/firebase-logo.png" width="116px" height="32px" alt="Firebase"/>
22

3+
## 3.6.0 (2016, September 7)
4+
5+
[Full changelog](https://github.com/EddyVerbruggen/nativescript-plugin-firebase/compare/3.5.4...3.6.0)
6+
7+
### SDK versions
8+
If version numbers __changed__, clean your platform folders to avoid build errors.
9+
10+
- __iOS: 3.5.x__
11+
- Android: 9.4.0
12+
13+
### New
14+
- [#125](#125) Analytics API
15+
16+
17+
### Fixes
18+
- ..
19+
20+
21+
322
## 3.5.4 (2016, August 29)
423

524
[Full changelog](https://github.com/EddyVerbruggen/nativescript-plugin-firebase/compare/3.5.3...3.5.4)
625

726
### SDK versions
8-
If version numbers changed clean your platform folders to avoid build errors.
27+
If version numbers __changed__, clean your platform folders to avoid build errors.
928

1029
- iOS: 3.4.x
1130
- Android: 9.4.0
@@ -21,7 +40,7 @@ If version numbers changed clean your platform folders to avoid build errors.
2140
[Full changelog](https://github.com/EddyVerbruggen/nativescript-plugin-firebase/compare/3.5.1...3.5.3)
2241

2342
### SDK versions
24-
If version numbers changed clean your platform folders to avoid build errors.
43+
If version numbers __changed__, clean your platform folders to avoid build errors.
2544

2645
- __iOS: 3.4.x__
2746
- Android: 9.4.0
@@ -39,7 +58,7 @@ If version numbers changed clean your platform folders to avoid build errors.
3958
[Full changelog](https://github.com/EddyVerbruggen/nativescript-plugin-firebase/compare/3.5.0...3.5.1)
4059

4160
### SDK versions
42-
If version numbers changed clean your platform folders to avoid build errors.
61+
If version numbers __changed__, clean your platform folders to avoid build errors.
4362

4463
- iOS: 3.3.x
4564
- Android: 9.4.0
@@ -54,7 +73,7 @@ If version numbers changed clean your platform folders to avoid build errors.
5473
[Full changelog](https://github.com/EddyVerbruggen/nativescript-plugin-firebase/compare/3.4.4...3.5.0)
5574

5675
### SDK versions
57-
If version numbers changed clean your platform folders to avoid build errors.
76+
If version numbers __changed__, clean your platform folders to avoid build errors.
5877

5978
- iOS: 3.3.x
6079
- __Android: 9.4.0__
@@ -74,7 +93,7 @@ If version numbers changed clean your platform folders to avoid build errors.
7493
[Full changelog](https://github.com/EddyVerbruggen/nativescript-plugin-firebase/compare/3.4.3...3.4.4)
7594

7695
### SDK versions
77-
If version numbers changed clean your platform folders to avoid build errors.
96+
If version numbers __changed__, clean your platform folders to avoid build errors.
7897

7998
- iOS: 3.3.x
8099
- Android: 9.2.0
@@ -89,7 +108,7 @@ If version numbers changed clean your platform folders to avoid build errors.
89108
[Full changelog](https://github.com/EddyVerbruggen/nativescript-plugin-firebase/compare/3.4.2...3.4.3)
90109

91110
### SDK versions
92-
If version numbers changed clean your platform folders to avoid build errors.
111+
If version numbers __changed__, clean your platform folders to avoid build errors.
93112

94113
- iOS: 3.3.x
95114
- Android: 9.2.0
@@ -104,7 +123,7 @@ If version numbers changed clean your platform folders to avoid build errors.
104123
[Full changelog](https://github.com/EddyVerbruggen/nativescript-plugin-firebase/compare/3.4.0...3.4.2)
105124

106125
### SDK versions
107-
If version numbers changed clean your platform folders to avoid build errors.
126+
If version numbers __changed__, clean your platform folders to avoid build errors.
108127

109128
- iOS: 3.3.x
110129
- Android: 9.2.0
@@ -125,7 +144,7 @@ If version numbers changed clean your platform folders to avoid build errors.
125144
[Full changelog](https://github.com/EddyVerbruggen/nativescript-plugin-firebase/compare/3.3.0...3.4.0)
126145

127146
### SDK versions
128-
If version numbers changed clean your platform folders to avoid build errors.
147+
If version numbers __changed__, clean your platform folders to avoid build errors.
129148

130149
- iOS: 3.3.x
131150
- __Android: 9.2.0__
@@ -143,7 +162,7 @@ If version numbers changed clean your platform folders to avoid build errors.
143162
[Full changelog](https://github.com/EddyVerbruggen/nativescript-plugin-firebase/compare/3.2.0...3.3.0)
144163

145164
### SDK versions
146-
If version numbers changed clean your platform folders to avoid build errors.
165+
If version numbers __changed__, clean your platform folders to avoid build errors.
147166

148167
- iOS: __3.2.x__
149168
- Android: 9.0.2
@@ -157,7 +176,7 @@ If version numbers changed clean your platform folders to avoid build errors.
157176
[Full changelog](https://github.com/EddyVerbruggen/nativescript-plugin-firebase/compare/3.1.0...3.2.0)
158177

159178
### SDK versions
160-
If version numbers changed clean your platform folders to avoid build errors.
179+
If version numbers __changed__, clean your platform folders to avoid build errors.
161180

162181
- iOS: 3.2.0
163182
- Android: __9.0.2__
@@ -171,7 +190,7 @@ If version numbers changed clean your platform folders to avoid build errors.
171190
[Full changelog](https://github.com/EddyVerbruggen/nativescript-plugin-firebase/compare/3.0.0...3.1.0)
172191

173192
### SDK versions
174-
If version numbers changed clean your platform folders to avoid build errors.
193+
If version numbers __changed__, clean your platform folders to avoid build errors.
175194

176195
- iOS: 3.2.0
177196
- Android: 9.0.0
@@ -185,7 +204,7 @@ If version numbers changed clean your platform folders to avoid build errors.
185204
[Full changelog](https://github.com/EddyVerbruggen/nativescript-plugin-firebase/compare/2.1.8...3.0.0)
186205

187206
### SDK versions
188-
If version numbers changed clean your platform folders to avoid build errors.
207+
If version numbers __changed__, clean your platform folders to avoid build errors.
189208

190209
- iOS: 3.2.0
191210
- Android: 9.0.0

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ For readability the supported features have been moved to their own README's:
1313
* [Cloud Messaging](docs/MESSAGING.md)
1414
* [Storage](docs/STORAGE.md)
1515
* [Crash Reporting](docs/CRASHREPORTING.md)
16+
* [Analytics](docs/ANALYTICS.md)
1617

1718
## NativeScript version
1819
Please update to at least tns 2.1.0 to make plugin installation a little easier (otherwise you'll need to manually add an `applicationId` to `app/App_Resources/Android/app.gradle`).
@@ -136,7 +137,8 @@ firebase.init({
136137

137138
#### Pod dependency error
138139
If you see an error like `Unable to satisfy the following requirements: Firebase (~> 3.3.0) required by Podfile`,
139-
then check [issue 98](#98) which perfectly explains how to update your local Pod spec repo.
140+
then check [issue 98](#98) which perfectly explains how to update your local Pod spec repo, or first try
141+
to `tns platform remove ios && tns platform add ios`.
140142

141143
## Known issues on Android
142144

docs/ANALYTICS.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<img src="images/firebase-logo.png" width="116px" height="32px" alt="Firebase"/>
2+
3+
## Enabling Analytics
4+
This plugin has had enalytics enabled since day 1 to collect usage and behavior data for your app. However, you were not able to push your own events. That changed with version 3.6.0 😃🔥
5+
6+
_Beware that your Analytics events in the Firebase console dashboards are updated periodically throughout the day, not real-time._
7+
8+
9+
## Functions
10+
11+
### analytics.logEvent
12+
Events can be used to analyze what is happening in your app, such as user actions, system events, or errors.
13+
14+
Analytics automatically logs [some events](https://support.google.com/firebase/answer/6317485) for you; you don't need to add any code to receive them. If your app needs to collect additional data, you can log up to 500 different Analytics Event types in your app. There is no limit on the total volume of events your app logs.
15+
16+
For a list of predefined Events [check this Android reference](https://firebase.google.com/docs/reference/android/com/google/firebase/analytics/FirebaseAnalytics.Event.html). Just copy the String values - these are also available on iOS.
17+
18+
```js
19+
firebase.analytics.logEvent({
20+
key: "add_to_cart",
21+
parameters: [ // optional
22+
{
23+
key: "item_id",
24+
value: "p7654"
25+
},
26+
{
27+
key: "item_name",
28+
value: "abc"
29+
}]
30+
}).then(
31+
function () {
32+
console.log("Firebase Analytics event logged");
33+
}
34+
);
35+
```
36+
37+
### analytics.setUserProperty
38+
User properties are attributes you define to describe segments of your userbase, such as language preference or geographic location.
39+
40+
Analytics automatically logs [some user properties](https://support.google.com/firebase/answer/6317486); you don't need to add any code to enable them. If your app needs to collect additional data, you can set up to 25 different Analytics User Properties in your app.
41+
42+
You own properties need to be [preregistered with Firebase](https://support.google.com/firebase/answer/6317519?hl=en&ref_topic=6317489#create-property) before you can use them, so add a property for __all relevant platforms__ like this:
43+
44+
<img src="images/analytics-userproperty.png" width="905px" height="559px" alt="User properties"/>
45+
46+
47+
```js
48+
firebase.analytics.setUserProperty({
49+
key: "origin",
50+
value: "demoapp"
51+
}).then(
52+
function () {
53+
console.log("Analytics user property set");
54+
}
55+
);
56+
```
152 KB
Loading

firebase-common.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var firebase = {};
2+
firebase.analytics = {};
23

34
firebase.LoginType = {
45
ANONYMOUS: "anonymous",

firebase.android.js

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,56 @@ firebase._isGooglePlayServicesAvailable = function () {
282282
return playServicesStatus === playServiceStatusSuccess;
283283
};
284284

285+
firebase.analytics.logEvent = function (arg) {
286+
return new Promise(function (resolve, reject) {
287+
try {
288+
if (arg.key === undefined) {
289+
reject("Argument 'key' is missing");
290+
return;
291+
}
292+
293+
var bundle = new android.os.Bundle();
294+
if (arg.properties !== undefined) {
295+
for (var p in arg.properties) {
296+
var prop = arg.properties[p];
297+
if (prop.value !== undefined) {
298+
bundle.putString(prop.key, prop.value);
299+
}
300+
}
301+
}
302+
303+
com.google.firebase.analytics.FirebaseAnalytics.getInstance(appModule.android.currentContext).logEvent(arg.key, bundle);
304+
305+
resolve();
306+
} catch (ex) {
307+
console.log("Error in firebase.logEvent: " + ex);
308+
reject(ex);
309+
}
310+
});
311+
};
312+
313+
firebase.analytics.setUserProperty = function (arg) {
314+
return new Promise(function (resolve, reject) {
315+
try {
316+
if (arg.key === undefined) {
317+
reject("Argument 'key' is missing");
318+
return;
319+
}
320+
if (arg.value === undefined) {
321+
reject("Argument 'value' is missing");
322+
return;
323+
}
324+
325+
com.google.firebase.analytics.FirebaseAnalytics.getInstance(appModule.android.currentContext).setUserProperty(arg.key, arg.value);
326+
327+
resolve();
328+
} catch (ex) {
329+
console.log("Error in firebase.setUserProperty: " + ex);
330+
reject(ex);
331+
}
332+
});
333+
};
334+
285335
firebase.getRemoteConfig = function (arg) {
286336
return new Promise(function (resolve, reject) {
287337

@@ -759,11 +809,6 @@ firebase._addObservers = function(to, updateCallback) {
759809
},
760810
onChildMoved: function (snapshot, previousChildKey) {
761811
updateCallback(firebase.getCallbackData('ChildMoved', snapshot));
762-
},
763-
onCancelled: function (databaseError) {
764-
updateCallback({
765-
error: databaseError.getMessage()
766-
});
767812
}
768813
});
769814
to.addChildEventListener(listener);

firebase.d.ts

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,9 @@ declare module "nativescript-plugin-firebase" {
183183
/**
184184
* The configuration properties to retrieve for your app. Specify as:
185185
* properties: [{
186-
* key: "holiday_promo_enabled",
187-
* default: false
188-
* }, ..]
186+
* key: "holiday_promo_enabled",
187+
* default: false
188+
* }, ..]
189189
*/
190190
keys: Array<string>;
191191
properties: Array<RemoteConfigProperty>
@@ -346,6 +346,41 @@ declare module "nativescript-plugin-firebase" {
346346
*/
347347
export function keepInSync(path: string, switchOn: boolean): Promise<any>;
348348

349+
// Analytics, nicely grouped in its own module
350+
export module analytics {
351+
export interface LogEventParameter {
352+
key: string;
353+
value: string;
354+
}
355+
export interface LogEventOptions {
356+
/**
357+
* The name of the event. You can use any name, but it's recommended to use one of
358+
* the predefined constants. These values are the same for both iOS and Android, so
359+
* for the complete list see https://firebase.google.com/docs/reference/android/com/google/firebase/analytics/FirebaseAnalytics.Event.html
360+
*/
361+
key: string;
362+
/**
363+
* Each (predefined) event has its own set of optional parameters, see
364+
* https://firebase.google.com/docs/reference/android/com/google/firebase/analytics/FirebaseAnalytics.Param
365+
* Example:
366+
*
367+
* parameters: [{
368+
* key: "item_name",
369+
* value: "abc"
370+
* }, ..]
371+
*/
372+
parameters?: Array<LogEventParameter>
373+
}
374+
375+
export interface SetUserPropertyOptions {
376+
key: string;
377+
value: string;
378+
}
379+
380+
function logEvent(options: LogEventOptions): Promise<any>;
381+
function setUserProperty(options: SetUserPropertyOptions): Promise<any>;
382+
}
383+
349384
// Auth
350385
export function login(options: LoginOptions): Promise<User>;
351386
export function logout(): Promise<any>;

0 commit comments

Comments
 (0)