Skip to content

Commit fd26599

Browse files
deashayRafael Santos
authored and
Rafael Santos
committed
Allow usage of analytics adapter (parse-community#2327)
* Allow usage of analytics adapter * Use promises in controller
1 parent b76a5c6 commit fd26599

File tree

5 files changed

+57
-8
lines changed

5 files changed

+57
-8
lines changed
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export class AnalyticsAdapter {
2+
appOpened(parameters, req) {
3+
return Promise.resolve({});
4+
}
5+
6+
trackEvent(eventName, parameters, req) {
7+
return Promise.resolve({});
8+
}
9+
}
10+
11+
export default AnalyticsAdapter;

src/Config.js

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export class Config {
4141
this.emailVerifyTokenValidityDuration = cacheInfo.emailVerifyTokenValidityDuration;
4242
this.appName = cacheInfo.appName;
4343

44+
this.analyticsController = cacheInfo.analyticsController;
4445
this.cacheController = cacheInfo.cacheController;
4546
this.hooksController = cacheInfo.hooksController;
4647
this.filesController = cacheInfo.filesController;
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import AdaptableController from './AdaptableController';
2+
import { AnalyticsAdapter } from '../Adapters/Analytics/AnalyticsAdapter';
3+
4+
export class AnalyticsController extends AdaptableController {
5+
appOpened(req) {
6+
return this.adapter.appOpened(req.body, req).then(
7+
function(response) {
8+
return { response: response };
9+
}).catch((err) => {
10+
return { response: {} };
11+
});
12+
}
13+
14+
trackEvent(req) {
15+
return this.adapter.trackEvent(req.params.eventName, req.body, req).then(
16+
function(response) {
17+
return { response: response };
18+
}).catch((err) => {
19+
return { response: {} };
20+
});
21+
}
22+
23+
expectedAdapterType() {
24+
return AnalyticsAdapter;
25+
}
26+
}
27+
28+
export default AnalyticsController;

src/ParseServer.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ import { AnalyticsRouter } from './Routers/AnalyticsRouter';
2525
import { ClassesRouter } from './Routers/ClassesRouter';
2626
import { FeaturesRouter } from './Routers/FeaturesRouter';
2727
import { InMemoryCacheAdapter } from './Adapters/Cache/InMemoryCacheAdapter';
28+
import { AnalyticsController } from './Controllers/AnalyticsController';
2829
import { CacheController } from './Controllers/CacheController';
30+
import { AnalyticsAdapter } from './Adapters/Analytics/AnalyticsAdapter';
2931
import { FileLoggerAdapter } from './Adapters/Logger/FileLoggerAdapter';
3032
import { FilesController } from './Controllers/FilesController';
3133
import { FilesRouter } from './Routers/FilesRouter';
@@ -65,6 +67,7 @@ const requiredUserFields = { fields: { ...SchemaController.defaultColumns._Defau
6567

6668
// ParseServer works like a constructor of an express app.
6769
// The args that we understand are:
70+
// "analyticsAdapter": an adapter class for analytics
6871
// "filesAdapter": a class like GridStoreAdapter providing create, get,
6972
// and delete
7073
// "loggerAdapter": a class like FileLoggerAdapter providing info, error,
@@ -96,6 +99,7 @@ class ParseServer {
9699
appId = requiredParameter('You must provide an appId!'),
97100
masterKey = requiredParameter('You must provide a masterKey!'),
98101
appName,
102+
analyticsAdapter = undefined,
99103
filesAdapter,
100104
push,
101105
loggerAdapter,
@@ -140,7 +144,6 @@ class ParseServer {
140144
// Initialize the node client SDK automatically
141145
Parse.initialize(appId, javascriptKey || 'unused', masterKey);
142146
Parse.serverURL = serverURL;
143-
144147
if ((databaseOptions || databaseURI || collectionPrefix !== '') && databaseAdapter) {
145148
throw 'You cannot specify both a databaseAdapter and a databaseURI/databaseOptions/connectionPrefix.';
146149
} else if (!databaseAdapter) {
@@ -184,6 +187,7 @@ class ParseServer {
184187
const loggerControllerAdapter = loadAdapter(loggerAdapter, FileLoggerAdapter);
185188
const emailControllerAdapter = loadAdapter(emailAdapter);
186189
const cacheControllerAdapter = loadAdapter(cacheAdapter, InMemoryCacheAdapter, {appId: appId});
190+
const analyticsControllerAdapter = loadAdapter(analyticsAdapter, AnalyticsAdapter);
187191

188192
// We pass the options and the base class for the adatper,
189193
// Note that passing an instance would work too
@@ -195,6 +199,7 @@ class ParseServer {
195199
const cacheController = new CacheController(cacheControllerAdapter, appId);
196200
const databaseController = new DatabaseController(databaseAdapter);
197201
const hooksController = new HooksController(appId, databaseController, webhookKey);
202+
const analyticsController = new AnalyticsController(analyticsControllerAdapter);
198203

199204
// TODO: create indexes on first creation of a _User object. Otherwise it's impossible to
200205
// have a Parse app without it having a _User collection.
@@ -227,6 +232,7 @@ class ParseServer {
227232
webhookKey: webhookKey,
228233
fileKey: fileKey,
229234
facebookAppIds: facebookAppIds,
235+
analyticsController: analyticsController,
230236
cacheController: cacheController,
231237
filesController: filesController,
232238
pushController: pushController,

src/Routers/AnalyticsRouter.js

+10-7
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
// AnalyticsRouter.js
22
import PromiseRouter from '../PromiseRouter';
33

4-
// Returns a promise that resolves to an empty object response
5-
function ignoreAndSucceed(req) {
6-
return Promise.resolve({
7-
response: {}
8-
});
4+
function appOpened(req) {
5+
const analyticsController = req.config.analyticsController;
6+
return analyticsController.appOpened(req);
7+
}
8+
9+
function trackEvent(req) {
10+
const analyticsController = req.config.analyticsController;
11+
return analyticsController.trackEvent(req);
912
}
1013

1114

1215
export class AnalyticsRouter extends PromiseRouter {
1316
mountRoutes() {
14-
this.route('POST','/events/AppOpened', ignoreAndSucceed);
15-
this.route('POST','/events/:eventName', ignoreAndSucceed);
17+
this.route('POST','/events/AppOpened', appOpened);
18+
this.route('POST','/events/:eventName', trackEvent);
1619
}
1720
}

0 commit comments

Comments
 (0)