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

Polish the plugin a bit #6

Closed
wants to merge 5 commits into from
Closed
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
61 changes: 2 additions & 59 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,73 +4,16 @@ logs
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/

# Dependency directories
node_modules/
jspm_packages/

# Typescript v1 declaration files
typings/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# environment variables file
.env
.environment

# next.js build output
.next

# editors
# Editors
.vscode
.idea

# yalc
# Yalc
.yalc
yalc*

# macOS
.DS_Store

# output
dist/
33 changes: 24 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,38 @@
# PostHog Plugin Starter Kit: Hello World
# PostHog Plugin: Hello World Starter Kit

[![npm package](https://img.shields.io/npm/v/posthog-plugin-hello-world?style=flat-square)](https://www.npmjs.com/package/posthog-plugin-hello-world)
[![License: MIT](https://img.shields.io/badge/License-MIT-red.svg?style=flat-square)](https://opensource.org/licenses/MIT)

This is an exemplary PostHog plugin. It adds property `"greeting"` to every event, with a configurable value (by default: `"Hello world!"`).
This is a basic exemplary PostHog plugin. It adds property `"greeting"` to every event, with a configurable value (default: `"Hello world!"`).

Feel free to use it as a base for your own plugins!

You can also add a `logo.png` file to give this plugin its own logo.
## How to develop

Need more information on developing plugins? Check out [the Plugins Overview](https://posthog.com/docs/plugins/build/overview)
All of the plugin's code is located in the `index.js` file, which is JavaScript ran inside of PostHog.
To get yourself up to speed with this environment, we sincerely recommend checking out our [Plugins overview in PostHog Docs]([the Plugins Overview](https://posthog.com/docs/plugins/build/overview).
For a crash course, read our [plugin building tutorial in PostHog Docs](https://posthog.com/docs/plugins/build/tutorial).

## Installation
## How to test

To test the plugin, you'll need to install a few `npm` dependencies already specified in `package.json`:
```bash
npm install
```

This will get you the testing library Jest and some our test helpers.
Then to run tests it's just:

```bash
npm test
```

## How to install

1. Open PostHog.
1. Head to the Plugins page from the sidebar.
1. Install from URL using this repository's URL.
1. Open the Plugins page from the sidebar.
1. Head to the Advanced tab.
1. "Install from GitHub, GitLab or npm" using this repository's URL.

More information in [the Plugins Tutorial](https://posthog.com/docs/plugins/build/tutorial)
## Questions?

### [Join our Slack community.](https://join.slack.com/t/posthogusers/shared_invite/enQtOTY0MzU5NjAwMDY3LTc2MWQ0OTZlNjhkODk3ZDI3NDVjMDE1YjgxY2I4ZjI4MzJhZmVmNjJkN2NmMGJmMzc2N2U3Yjc3ZjI5NGFlZDQ)
Expand Down
12 changes: 5 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
// Some internal library function
async function getRandomNumber() {
return 4 // remove this line to get an actual random number from random.org – caution, rate limited to 10 events/s!
const response = await fetch(
'https://www.random.org/integers/?num=1&min=1&max=1000000000&col=1&base=10&format=plain&rnd=new'
)
return parseInt(await response.text())
return 4
}

// Plugin method that runs on plugin load
async function setupPlugin({ config }) {
console.log(`Setting up the plugin:\n${config.greeting}`)
console.log(config.greeting)
}

// Plugin method that processes event
async function processEvent(event, { config, cache }) {
const counterValue = (await cache.get('greeting_counter', 0))
cache.set('greeting_counter', counterValue + 1)
Expand All @@ -21,7 +19,7 @@ async function processEvent(event, { config, cache }) {
return event
}

// The famed Hello World plugin itself
// The plugin itself
module.exports = {
setupPlugin,
processEvent
Expand Down
9 changes: 5 additions & 4 deletions index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const { createEvent, createIdentify, getMeta, resetMeta, clone } = require("@pos
const { processEvent } = require("./index");

beforeEach(() => {
// Making sure plugin meta has our custom test config
resetMeta({
config: {
greeting: "Dzień dobry!",
Expand All @@ -11,10 +12,10 @@ beforeEach(() => {
});

test("processEvent adds properties", async () => {
// create a random event
// Create a random event
const event0 = createEvent({ event: "booking completed", properties: { amount: "20", currency: "USD" } });

// must clone the event since `processEvent` will mutate it otherwise
// Must clone the event since `processEvent` will mutate it
const event1 = await processEvent(clone(event0), getMeta());
expect(event1).toEqual({
...event0,
Expand Down Expand Up @@ -51,9 +52,9 @@ test("processEvent does not crash with identify", async () => {
greeting_counter: 0,
random_number: 4,
});
// create a random event
// Create a random event
const event0 = createIdentify();
// must clone the event since `processEvent` will mutate it otherwise
// Must clone the event since `processEvent` will mutate it
const { properties, ...restOfEvent1 } = await processEvent(clone(event0), getMeta());
expect(restOfEvent1).toEqual(event0);
expect(properties).toEqual(defaultHelloWorldProperties);
Expand Down
Binary file added logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading