DavidWells / DavidWells/analytics

Update method injection with standard API

Open
#289 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
JavaScript
Stars
2.7k
Forks
267
PR merge metrics
No merged PRs in 30d

Description

Custom methods have the analytics instance injected into them as last argument and into the this context if non-arrow function used. https://getanalytics.io/plugins/writing-plugins/#adding-custom-methods

TODO: Standardize this api to something like:

{
  analytics: instance,
  config, //  > current plugin config,
}

https://github.com/DavidWells/analytics/blob/master/packages/analytics-core/src/index.js#L981

 function appendArguments(fn, extra = {}) {
    return function () {
      /* Get original args */
      const args = Array.prototype.slice.call(arguments)
      /* Create clone of args */
      let newArgs = new Array(fn.length)
      for (let i = 0; i < args.length; i++) {
        newArgs[i] = args[i]
      }
      /* Append new arg to end */
      newArgs[newArgs.length] = instance
      // Set instance on extended methods
      return fn.apply({ instance, ...extra }, newArgs)
    }
  }

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in packages/analytics-core/src/index.js around line 981 and compare the current method injection with the custom-methods documentation. Trace how custom methods receive the analytics instance and plugin configuration; done means the injection follows one consistent object-based API and the documented behavior matches it.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
backend-api-design
Issue type
Refactor
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.