Skip to content

Commit

Permalink
Allow usage of analytics adapter (parse-community#2327)
Browse files Browse the repository at this point in the history
* Allow usage of analytics adapter

* Use promises in controller
  • Loading branch information
deashay authored and Rafael Santos committed Mar 15, 2017
1 parent fbd934d commit 40eb70e
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 8 deletions.
11 changes: 11 additions & 0 deletions src/Adapters/Analytics/AnalyticsAdapter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export class AnalyticsAdapter {
appOpened(parameters, req) {
return Promise.resolve({});
}

trackEvent(eventName, parameters, req) {
return Promise.resolve({});
}
}

export default AnalyticsAdapter;
1 change: 1 addition & 0 deletions src/Config.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export class Config {
this.emailVerifyTokenValidityDuration = cacheInfo.emailVerifyTokenValidityDuration;
this.appName = cacheInfo.appName;

this.analyticsController = cacheInfo.analyticsController;
this.cacheController = cacheInfo.cacheController;
this.hooksController = cacheInfo.hooksController;
this.filesController = cacheInfo.filesController;
Expand Down
28 changes: 28 additions & 0 deletions src/Controllers/AnalyticsController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import AdaptableController from './AdaptableController';
import { AnalyticsAdapter } from '../Adapters/Analytics/AnalyticsAdapter';

export class AnalyticsController extends AdaptableController {
appOpened(req) {
return this.adapter.appOpened(req.body, req).then(
function(response) {
return { response: response };
}).catch((err) => {
return { response: {} };
});
}

trackEvent(req) {
return this.adapter.trackEvent(req.params.eventName, req.body, req).then(
function(response) {
return { response: response };
}).catch((err) => {
return { response: {} };
});
}

expectedAdapterType() {
return AnalyticsAdapter;
}
}

export default AnalyticsController;
8 changes: 7 additions & 1 deletion src/ParseServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ import { AnalyticsRouter } from './Routers/AnalyticsRouter';
import { ClassesRouter } from './Routers/ClassesRouter';
import { FeaturesRouter } from './Routers/FeaturesRouter';
import { InMemoryCacheAdapter } from './Adapters/Cache/InMemoryCacheAdapter';
import { AnalyticsController } from './Controllers/AnalyticsController';
import { CacheController } from './Controllers/CacheController';
import { AnalyticsAdapter } from './Adapters/Analytics/AnalyticsAdapter';
import { FileLoggerAdapter } from './Adapters/Logger/FileLoggerAdapter';
import { FilesController } from './Controllers/FilesController';
import { FilesRouter } from './Routers/FilesRouter';
Expand Down Expand Up @@ -65,6 +67,7 @@ const requiredUserFields = { fields: { ...SchemaController.defaultColumns._Defau

// ParseServer works like a constructor of an express app.
// The args that we understand are:
// "analyticsAdapter": an adapter class for analytics
// "filesAdapter": a class like GridStoreAdapter providing create, get,
// and delete
// "loggerAdapter": a class like FileLoggerAdapter providing info, error,
Expand Down Expand Up @@ -96,6 +99,7 @@ class ParseServer {
appId = requiredParameter('You must provide an appId!'),
masterKey = requiredParameter('You must provide a masterKey!'),
appName,
analyticsAdapter = undefined,
filesAdapter,
push,
loggerAdapter,
Expand Down Expand Up @@ -140,7 +144,6 @@ class ParseServer {
// Initialize the node client SDK automatically
Parse.initialize(appId, javascriptKey || 'unused', masterKey);
Parse.serverURL = serverURL;

if ((databaseOptions || databaseURI || collectionPrefix !== '') && databaseAdapter) {
throw 'You cannot specify both a databaseAdapter and a databaseURI/databaseOptions/connectionPrefix.';
} else if (!databaseAdapter) {
Expand Down Expand Up @@ -184,6 +187,7 @@ class ParseServer {
const loggerControllerAdapter = loadAdapter(loggerAdapter, FileLoggerAdapter);
const emailControllerAdapter = loadAdapter(emailAdapter);
const cacheControllerAdapter = loadAdapter(cacheAdapter, InMemoryCacheAdapter, {appId: appId});
const analyticsControllerAdapter = loadAdapter(analyticsAdapter, AnalyticsAdapter);

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

// TODO: create indexes on first creation of a _User object. Otherwise it's impossible to
// have a Parse app without it having a _User collection.
Expand Down Expand Up @@ -227,6 +232,7 @@ class ParseServer {
webhookKey: webhookKey,
fileKey: fileKey,
facebookAppIds: facebookAppIds,
analyticsController: analyticsController,
cacheController: cacheController,
filesController: filesController,
pushController: pushController,
Expand Down
17 changes: 10 additions & 7 deletions src/Routers/AnalyticsRouter.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
// AnalyticsRouter.js
import PromiseRouter from '../PromiseRouter';

// Returns a promise that resolves to an empty object response
function ignoreAndSucceed(req) {
return Promise.resolve({
response: {}
});
function appOpened(req) {
const analyticsController = req.config.analyticsController;
return analyticsController.appOpened(req);
}

function trackEvent(req) {
const analyticsController = req.config.analyticsController;
return analyticsController.trackEvent(req);
}


export class AnalyticsRouter extends PromiseRouter {
mountRoutes() {
this.route('POST','/events/AppOpened', ignoreAndSucceed);
this.route('POST','/events/:eventName', ignoreAndSucceed);
this.route('POST','/events/AppOpened', appOpened);
this.route('POST','/events/:eventName', trackEvent);
}
}

0 comments on commit 40eb70e

Please sign in to comment.