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

Conversation

deashay
Copy link
Contributor

@deashay deashay commented Jul 19, 2016

I want to write my own adapter for google analytics, but parse-server in current state just ignores the analytics events, so I decided to write my own implementation of that, using patterns provided for other controllers/adapters/routers.

As you can see, this PR doesn't change the default behaviour of AnalyticsRouter, so default adapter does exactly the same things like before(that is: nothing), but I think being able to provide my own AnalyticsAdapter is crucial and much easier to implement than monkeypatching the whole server to catch requests.

Please let me know if that satisfies you or if you have any suggestions on how to improve it. To be honest I'd love to see it merged asap, because of deadlines in our project. If you decide to merge it, I'll provide GA adapter when its finished.

response: {}
});
function handlePost(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

@flovilmart
Copy link
Contributor

HI @deashay, thanks for the PR.

Do you think the adapter could adopt function trackEvent(eventName, properties, req) that may help simplifie thé extraction, of all parameters while allowing other adapters to manipulate the requests if they need to?

@flovilmart
Copy link
Contributor

Also, I'd wrap the adapter call inside a promise to catch errors and exceptions without forwarding to the client as I'm not sure how the SDK handle errors on thé analytics PoV

@ghost
Copy link

ghost commented Jul 20, 2016

@deashay updated the pull request.

@deashay
Copy link
Contributor Author

deashay commented Jul 20, 2016

@flovilmart could you take a look now?

@codecov-io
Copy link

codecov-io commented Jul 20, 2016

Current coverage is 91.76%

No coverage report found for master at 8719afd.

Powered by Codecov. Last updated by 8719afd...0470dde

@flovilmart
Copy link
Contributor

Looks neat! Thanks for taking that up!

@flovilmart flovilmart merged commit d1a6cae into parse-community:master Jul 20, 2016
@fcoufour
Copy link

Hi guys,

I'm looking for a Parse Analytics replacement.

Currently, Parse Server responds 200 OK with {} body to Analytics requests (AppOpened or custom event ones).
Session Token and Installation Id are specified in the headers and event time and custom dimensions are in the body.

@deashay provided a default Analytics Adapter. Has anyone started to work on adapters specific to Google Analytics or Mixpanel ? I didn't find anything.

@deashay
Copy link
Contributor Author

deashay commented Nov 22, 2016

@fcoufour I'm no longer working on this project, but I can share my changes with you, I've created simple analytics adapter using universal-analytics for GA

var ua = require('universal-analytics');

class AnalyticsAdapter {
  constructor(tracking_id) {
    this._tracking_id = tracking_id;
  }

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

  trackEvent(eventName, parameters, req) {
    var visitor;
    if (typeof(req.auth.user) !== 'undefined') {
      visitor = ua(this._tracking_id, req.auth.user.id);
    } else {
      visitor = ua(this._tracking_id);
    }
    var params = {
      ec: eventName,
      ea: parameters.action || eventName,
      el: parameters.label || '',
      ev: parameters.value || 0,
      dp: parameters.page || req.originalUrl
    }
    visitor.event(params).send();
    return Promise.resolve({});
  }
}

exports.AnalyticsAdapter = AnalyticsAdapter;

create a file with that, put it in lib/analytics_adapter.js, then import it:

var AnalyticsAdapter = require('../lib/analytics_adapter.js').AnalyticsAdapter;

and pass to your ParseServer:

var api = new ParseServer({
  analyticsAdapter: new AnalyticsAdapter(process.env.GA_TRACKING_ID),
  (...)

you'll also need to add

"universal-analytics": "^0.4.2"

to your dependencies in package.json

@fcoufour
Copy link

Thanks Marek 👍 I was planning to do quite the same thing using universal-analytics node module as well. You can even track appOpened returning trackEvent("AppOpened", parameters, req) .

@deashay
Copy link
Contributor Author

deashay commented Nov 22, 2016

@fcoufour yes, but I'm not sure if it was fault of my application or something else, but AppOpened event seems to also trigger trackEvent, so it gets doubled

@fcoufour
Copy link

@deashay Will check that soon. I'll let you know

@flovilmart
Copy link
Contributor

I believe you're right, the appOpened is not in use

@fcoufour
Copy link

@deashay appOpened is used in the latest stable version (2.2.23) and there is no inner call to trackEvent

rsouzas pushed a commit to back4app/parse-server that referenced this pull request Mar 15, 2017
* Allow usage of analytics adapter

* Use promises in controller
rsouzas pushed a commit to back4app/parse-server that referenced this pull request Mar 16, 2017
* Allow usage of analytics adapter

* Use promises in controller
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants