Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow usage of analytics adapter #2327

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -39,6 +39,7 @@ export class Config {
this.preventLoginWithUnverifiedEmail = cacheInfo.preventLoginWithUnverifiedEmail;
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 @@ -95,6 +98,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 @@ -137,7 +141,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 @@ -183,6 +186,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 @@ -194,6 +198,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 @@ -225,6 +230,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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Usually we try not to forward express request directly to the controllers, and be a bit more explicit

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);
}
}