Skip to content

Commit 41a4a78

Browse files
authored
Merge pull request #175 from NativeScript/dtodorov/narusevic-add-analytics
Feat: add analytics
2 parents a755f94 + 21d3b4f commit 41a4a78

20 files changed

+160
-11
lines changed

README.md

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ NativeScript : Facebook SDK ![apple](https://cdn3.iconfinder.com/data/icons/pico
4646
- [Hide Custom Button If Can't share](#hide-custom-button-1)
4747
- [Login Response](#login-response)
4848
- [Get Current Access Token](#get-current-access-token)
49+
- [Basic Analytics](#basic-analytics)
4950
- [Graph API Example](#graph-api-example)
5051
- [Release notes](#release-notes)
5152
- [FAQ](#faq)
@@ -58,6 +59,7 @@ NativeScript : Facebook SDK ![apple](https://cdn3.iconfinder.com/data/icons/pico
5859
- [x] Login & Logout
5960
- [x] Share
6061
- [ ] Graph API
62+
- [x] Basic Analytics
6163

6264

6365
## Installation
@@ -346,7 +348,7 @@ If the Messenger app is not installed, the Send button will be hidden. Be sure t
346348

347349
### Show Share Dialog Programmatically
348350

349-
**Note** The share dialog will try fallback to browse page sharing if user doesn't have Facebook installed (only for linkContent)
351+
**Note** The share dialog will try fallback to browse page sharing if user doesn't have Facebook installed (only for linkContent)
350352

351353
```TypeScript
352354
showShareDialog(this.linkContent);
@@ -594,6 +596,34 @@ The callback that have to be provided to Facebook.login method receives 2 argume
594596
## Get Current Access Token
595597
The plugin allows to get the current access token, if any, via getCurrentAccessToken() method.
596598

599+
## Basic Analytics
600+
The plugin allows to log analytics events. At the initialization of the application you need to init analytics:
601+
602+
```Typescript
603+
application.on(application.launchEvent, function (args) {
604+
nsFacebook.init("{facebook_app_id}");
605+
nsFacebook.initAnalytics();
606+
});
607+
```
608+
609+
Events logging:
610+
611+
```Typescript
612+
nsFacebook.logEvent('Lead');
613+
```
614+
615+
Logging event with parameters:
616+
617+
```Typescript
618+
const value = 5;
619+
const parameters = [{
620+
key: 'value',
621+
value: value.toString(),
622+
}];
623+
624+
nsFacebook.logEvent(FundsAdded, parameters);
625+
```
626+
597627
## Graph API Example
598628
Once the Facebook access token is retrieved you can execute Graph API requests. In the example below after successful login, the access token is stored in application settings. And then on the home view it is retrieved and 2 Graph API calls are executed.
599629
1. Get Facebook id of the logged in user

demo-angular/app/app.module.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ import { NativeScriptFacebookModule } from "nativescript-facebook/angular";
66
import * as application from 'tns-core-modules/application';
77
import { routes } from "./app.routing";
88
import { NavigationService } from "./services/navigation.service";
9-
import { init } from "nativescript-facebook";
9+
import { init, initAnalytics } from "nativescript-facebook";
1010

1111
application.on(application.launchEvent, function (args) {
1212
init("1771472059772879");
13+
initAnalytics();
1314
});
1415

1516
@NgModule({

demo-angular/app/pages/home/home.component.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,7 @@
1111
<Button text="Log out (custom)" (tap)="logout()"></Button>
1212
<Button text="Get current access token" (tap)="getCurrentAccessToken()"></Button>
1313
</StackLayout>
14+
<StackLayout class="buttons">
15+
<Button text="Log event" (tap)="logEvent()"></Button>
16+
</StackLayout>
1417
</StackLayout>

demo-angular/app/pages/home/home.component.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export class HomeComponent {
1616
userId: string;
1717
username: string;
1818
avatarUrl: string;
19+
eventCounter: number = 0;
1920

2021
constructor(private ref: ChangeDetectorRef, private navigationService: NavigationService) {
2122
// Get logged in user's info
@@ -53,6 +54,14 @@ export class HomeComponent {
5354
});
5455
}
5556

57+
logEvent() {
58+
this.eventCounter++;
59+
Facebook.logEvent('Home', [{
60+
key: 'counter',
61+
value: this.eventCounter.toString()
62+
}]);
63+
}
64+
5665
public getCurrentAccessToken() {
5766
let accessToken = Facebook.getCurrentAccessToken();
5867

demo-angular/app/pages/login/login.component.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@
1313
*ngIf = "canShowLinksMessageDialog"></Button>
1414
<Button (tap) = "onSendGenericDialog()" text = "Share Message Generic Template"
1515
*ngIf = "canShowGenericMessageDialog"></Button>
16+
<Button (tap) = "logEvent()" text="Log event"></Button>
1617
</StackLayout>

demo-angular/app/pages/login/login.component.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export class LoginComponent {
1818
canShowPhotosShareDialog = false;
1919
canShowLinksMessageDialog = false;
2020
canShowGenericMessageDialog = false;
21+
eventCounter = 0;
2122

2223
constructor(private ref: ChangeDetectorRef, private navigationService: NavigationService) {
2324
// have to init after facebook sdk inited
@@ -119,4 +120,12 @@ export class LoginComponent {
119120
onSendGenericDialog() {
120121
Facebook.showMessageDialog(this.genericContent);
121122
}
123+
124+
logEvent() {
125+
this.eventCounter++;
126+
Facebook.logEvent('Login', [{
127+
key: 'counter',
128+
value: this.eventCounter.toString()
129+
}]);
130+
}
122131
}

demo-vue/app/app.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import Login from "./components/Login";
44

55
import {
66
init,
7+
initAnalytics,
78
} from 'nativescript-facebook';
89

910
import FacebookPlugin from "nativescript-facebook/vue";
@@ -12,7 +13,8 @@ Vue.use(FacebookPlugin);
1213
Vue.config.silent = true;
1314

1415
application.on(application.launchEvent, function (args) {
15-
init("1771472059772879");
16+
init("1771472059772879");
17+
initAnalytics();
1618
});
1719

1820
new Vue({

demo-vue/app/components/Home.vue

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@
1212
<FacebookLoginButton @logout="onLogout"></FacebookLoginButton>
1313
<Button @tap="logout" text="Log out (Custom)"></Button>
1414
<Button @tap="getAccessToken" text="Get current access token"></Button>
15+
<Button @tap="logEventAction" text="Log event"></Button>
1516
</StackLayout>
1617
</StackLayout>
1718
</Page>
1819
</template>
1920

2021
<script>
21-
import { logout as fbLogout, getCurrentAccessToken } from "nativescript-facebook";
22+
import { logout as fbLogout, getCurrentAccessToken, logEvent } from "nativescript-facebook";
2223
import Login from "./Login";
2324
let appSettings = require("tns-core-modules/application-settings");
2425
let http = require("tns-core-modules/http");
@@ -47,7 +48,8 @@
4748
return {
4849
userId: null,
4950
accessToken: appSettings.getString("access_token"),
50-
avatarUrl: null
51+
avatarUrl: null,
52+
eventCounter: 0
5153
}
5254
},
5355
methods: {
@@ -64,6 +66,13 @@
6466
getAccessToken: function() {
6567
let accessToken = getCurrentAccessToken();
6668
alert("Current access token: " + JSON.stringify(accessToken, null, '\t'));
69+
},
70+
logEventAction() {
71+
this.counter++;
72+
logEvent('Home', [{
73+
key: 'counter',
74+
value: this.eventCounter.toString()
75+
}]);
6776
}
6877
}
6978
};

demo-vue/app/components/Login.vue

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
<Button @tap="onShareDialogPhotos" text="Open Share dialog (photos)" :visibility="canShowPhotosShareDialog ? 'visible' : 'collapsed'"></Button>
1616
<Button @tap="onSendDialog" text="Share Message (link)" :visibility="canShowLinksMessageDialog ? 'visible' : 'collapsed'"></Button>
1717
<Button @tap="onSendGenericDialog" text="Share Message (generic)" :visibility="canShowGenericMessageDialog ? 'visible' : 'collapsed'"></Button>
18+
<Button @tap="logEventAction" text="Log event"></Button>
1819
</StackLayout>
1920
</Page>
2021
</template>
@@ -34,7 +35,8 @@
3435
showShareDialog,
3536
showMessageDialog,
3637
canShareDialogShow,
37-
canMessageDialogShow
38+
canMessageDialogShow,
39+
logEvent
3840
} from 'nativescript-facebook';
3941
let appSettings = require('tns-core-modules/application-settings');
4042
@@ -53,7 +55,8 @@
5355
canShowLinksShareDialog: false,
5456
canShowPhotosShareDialog: false,
5557
canShowLinksMessageDialog: false,
56-
canShowGenericMessageDialog: false
58+
canShowGenericMessageDialog: false,
59+
eventCounter: 0
5760
}
5861
},
5962
methods: {
@@ -139,6 +142,13 @@
139142
},
140143
onSendGenericDialog: function() {
141144
showMessageDialog(this.genericContent);
145+
},
146+
logEventAction() {
147+
this.eventCounter++;
148+
logEvent('Login', [{
149+
key: 'counter',
150+
value: this.eventCounter.toString()
151+
}]);
142152
}
143153
}
144154
};

demo/app/app.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import * as application from 'tns-core-modules/application';
2-
import { init } from "nativescript-facebook";
2+
import { init, initAnalytics } from "nativescript-facebook";
33

44
application.on(application.launchEvent, function (args) {
55
init("1771472059772879");
6+
initAnalytics();
67
});
78

89
application.run({ moduleName: "app-root" });

0 commit comments

Comments
 (0)