https://github.com/datadotworld/mpjql-cli
MixPanel JQL Command Line Interface
https://github.com/datadotworld/mpjql-cli
command-line command-line-tool jql mixpanel
Last synced: over 1 year ago
JSON representation
MixPanel JQL Command Line Interface
- Host: GitHub
- URL: https://github.com/datadotworld/mpjql-cli
- Owner: datadotworld
- License: apache-2.0
- Created: 2017-10-11T02:24:49.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2023-12-04T20:54:21.000Z (over 2 years ago)
- Last Synced: 2023-12-04T21:47:02.898Z (over 2 years ago)
- Topics: command-line, command-line-tool, jql, mixpanel
- Language: JavaScript
- Size: 40 KB
- Stars: 2
- Watchers: 27
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# MixPanel JQL Command Line Interface
Installing this package will give you a new command line tool called `jql`. This
enables you to write JQL using modern javascript techniques, including:
* Split common code into modules
* Modern ES2015 Syntax
* String Template Literals
* etc.
## Example
Move commonly used functions to their own file...
```js
// date.js
export const dateToString = e => {
return new Date(Number(e)).toISOString().substr(0, 10)
}
```
...and then import them when needed.
```js
// example.js
import { dateToString } from './date'
const today = new Date()
const yesterday = new Date()
yesterday.setDate(yesterday.getDate() - 1)
function main() {
return Events({
from_date: dateToString(yesterday),
to_date: dateToString(today)
})
.groupByUser((user, events) => (user || 0) + events.length)
.map(user => `${user.key[0]}: ${user.value}`)
}
```
The `jql` command line tool uses [Rollup.js](https://rollupjs.org/),
[Babel](https://babeljs.io/) and [Uglify](https://github.com/mishoo/UglifyJS)
under the hood to compile the script before sending it to MixPanel.
## Installation
```
npm install -g mpjql-cli
```
Visit [MixPanel](http://mixpanel.com) and copy your secret from your user
settings page.
```
export MPSECRET=
```
Set this either locally in your terminal, or in `~/.profile`.
## Basic Usage
Create a JQL query, then run it as follows:
```
jql query myQuery.js
```
Results will be streamed to the terminal. Redirect to a file or other commands.
```
jql query myQuery.js > output.json
```
# Advanced Usage
## JQ
[jq](https://stedolan.github.io/jq/) is an amazing tool for highlighting
JSON results, or doing additional filtering of your results.
```
jql query myQuery.js | jq
```
It also has the capability to produce `CSV` with selected fields on the fly from
returned JSON.
```
jql query myQuery.js | jq -r '.results[] | [.field1, .field2] | @csv' > output.csv
```
## Passing settings to your scripts
Settings can be passed on the command line to configure a script. This can be
extremely useful when dealing with scripts that can be configured to find info
about a specific user, or date range.
Settings are passed on the command line:
```
jql query myQuery.js --setting='foo=bar' --setting='baz=boo,bop'
```
The special variable `__SETTINGS__` will be replaced with an object containing
all of your settings.
```js
// myQuery.js
const settings = __SETTINGS__
function main() { /* ... */ }
```
will effectively become...
```js
// myQuery.js
const settings = {
'foo':'bar',
'baz':['boo','bop']
}
function main() { /* ... */ }
```
# CLI commands
## `query`
Compile a script and execute it against your MixPanel account. Prints the resulting
JSON into the `STDOUT`.
```
jql query myQuery.js
```
## `show-code`
You can debug how a script is being compiled by replacing `query` with
`show-code`. This prints the compiled javascript to the terminal for inspection.
```
jql show-code myQuery.js
```
## `encode`
If you are integrating your scripts with another platform (such as Klipfolio)
where you need the script URI encoded.
```
jql encode myQuery.js
```
# Contributing
See [CONTRIBUTING.md](CONTRIBUTING.md) for more information on how you can help
make this tool better.