Skip to content
This repository was archived by the owner on Feb 13, 2024. It is now read-only.

Add enable option to disable flushing #118

Closed
wants to merge 1 commit into from
Closed

Conversation

vadimdemedes
Copy link

Fixes #116.

Allows to silently skip sending of messages, which is useful in tests and in development. Example usage:

const client = new Client('write key', {
  enable: process.env.NODE_ENV === 'production'
})

@stephenmathieson
Copy link
Contributor

Duplicate (kinda) of #90 / #75. I'm still unconvinced the shim (see that issue) is worth the added maintenance here.

@hulbert
Copy link

hulbert commented Jul 31, 2017

We're about to add this lib to our systems, and this would be a very nice thing to have. It seems the proposed method of overriding the flush method relies on undocumented behavior that could break between versions.

@vadimdemedes
Copy link
Author

@hulbert is absolutely on point. This is something I'd want too. Recommending overriding flush() method is a hackish workaround. I think this option clearly has a practical use case and we need to support one way or another.

Alternatively we could look at NODE_ENV and flush only when it's equal to "production". This is what Bugsnag's Node.js library does and I found it a nice convention. Similar to Bugsnag, we would assume environment is production by default, if NODE_ENV is empty. If there's a custom way of setting the environment (not setting NODE_ENV), we'd have env option to override the env name:

new Client('write key', { env: 'production' })

Even though it's good, I still prefer having enable, because it doesn't make any assumptions about the environment and is explicit. It gives complete control to the user and strips us from maintenance issues with the env way.

@vadimdemedes
Copy link
Author

@stephenmathieson what do you think?

@stephenmathieson
Copy link
Contributor

we could look at NODE_ENV and flush only when it's equal to "production".

While this does seem ideal in many cases, it would break our analytics in our non-prod environments. For example, on .build, we set NODE_ENV=staging, but we still want to send analytics calls (for testing their implementation, etc).

we'd have env option to override the env name

I'm 👎 here. Setting env = "whatever" seems magical in this case. I'd much prefer something explicit.

I still prefer having enable, because it doesn't make any assumptions about the environment and is explicit.

Agreed.

what do you think?

Since there is an obvious demand here, I think this can land. If you fix up the conflicts, I'll approve/merge. 😄

@vadimdemedes
Copy link
Author

I agree on your argument against env option, @stephenmathieson. Explicit option is better. PR is fixed up and ready to get merged!

index.js Outdated
@@ -54,6 +54,7 @@ class Analytics {
this.flushAt = Math.max(options.flushAt, 1) || 20
this.flushInterval = options.flushInterval || 10000
this.flushed = false
this.enabled = options.enable === false ? false : true // eslint-disable-line no-unneeded-ternary
Copy link
Contributor

Choose a reason for hiding this comment

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

Strange use of a ternary here (seems the linter agrees with me). Any reason this.enabled = options.enable won't work?

Copy link
Author

Choose a reason for hiding this comment

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

No idea what happened here 😆

Copy link
Contributor

@stephenmathieson stephenmathieson left a comment

Choose a reason for hiding this comment

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

One small thing, but this LGTM!

@f2prateek
Copy link
Contributor

What if a customer does this:

const client = new Client('write key', {
  enable: process.env.NODE_ENV === 'production'
})

client.track(...)
client.enabled = false

client.flush() // either explicitly or as part of a timer 

Basically a customer might want to track some events, and then disable the client but still have the expectation of flushing the previously queued events.

I'd prefer if the enabled flag was not mutable. I think the JS convention is to prefix it with _?

@stephenmathieson
Copy link
Contributor

@f2prateek we could just make the enabled field immutable so that an error would be raised instead

@f2prateek
Copy link
Contributor

SGTM!

@stephenmathieson
Copy link
Contributor

@vadimdemedes think you could make the enabled field immutable? Something like this should do:

Object.defineProperty(this, 'enabled', {
  configurable: false,
  writable: false,
  value: enabled
})

@f2prateek
Copy link
Contributor

Closing this for now, feel free to re-open once the PR is updated - otherwise the libraries team will tackle it soon. Tracking here https://segment.atlassian.net/browse/LIB-118

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants