smartanalytics/ts/smartanalytics.classes.analytics.ts

42 lines
1.0 KiB
TypeScript
Raw Normal View History

2018-10-28 22:29:45 +00:00
import * as plugins from './smartanalytics.plugins';
2017-08-23 12:55:11 +00:00
export class Analytics {
2018-10-28 22:29:45 +00:00
public projectId: string;
public appName: string;
public apiEndPoint: string;
public secretKey: string = '';
constructor(optionsArg: {
projectId: string;
appName: string;
apiEndPoint: string;
secretKey?: string;
2017-08-23 12:55:11 +00:00
}) {
2018-10-28 22:29:45 +00:00
this.projectId = optionsArg.projectId;
this.appName = optionsArg.appName;
this.apiEndPoint = optionsArg.apiEndPoint;
2017-08-29 13:18:10 +00:00
if (optionsArg.secretKey) {
2018-10-28 22:29:45 +00:00
this.secretKey = optionsArg.secretKey;
2017-08-23 12:55:11 +00:00
}
}
2018-10-28 22:29:45 +00:00
async recordEvent(eventIdArg: string, analyticsDataArg: any) {
2017-08-23 12:55:11 +00:00
let dataToSend = {
2017-08-23 23:06:09 +00:00
projectId: this.projectId,
2017-08-23 12:55:11 +00:00
appName: this.appName,
2017-08-29 13:18:10 +00:00
eventId: eventIdArg,
2017-08-23 12:55:11 +00:00
analyticsData: analyticsDataArg
2018-10-28 22:29:45 +00:00
};
await plugins.smartrequest
.postJson(this.apiEndPoint, {
headers: {
authenticate: this.secretKey,
'Content-Type': 'application/json'
},
requestBody: dataToSend
})
.catch(err => {
console.log(err);
});
2017-08-23 12:55:11 +00:00
}
}