Skip to content

Latest commit

 

History

History
103 lines (71 loc) · 2.05 KB

README.md

File metadata and controls

103 lines (71 loc) · 2.05 KB

@retentioneering/datalayer

This package provides access to the retentioneering datalayer. With the package you can push events to the datalayer, read from it and subscribe.

Install

install package:

npm install @retentioneering/datalayer

Usage

Push events

import { getDatalayer } from '@retentioneering/datalayer'

const reteDatalayer = getDatalayer()

reteDatalayer.push({
  type: 'custom-event',
  name: 'my-event-name',
  data: { // some stringified data
    some_property: 'some property',
  },
})

Get current events

import { getDatalayer } from '@retentioneering/datalayer'

const reteDatalayer = getDatalayer()
reteDatalayer.getEvents()

Subscribe

Obserbable-style

import { getDatalayer } from '@retentioneering/datalayer'

const reteDatalayer = getDatalayer()
const stream = reteDatalayer
    .createStream()

stream.subscribe((event) => {
  // do something
})

to unsubscribe, use:

const subs = stream.subscribe(handler)
subs.unsubscribe()

createStream function returns an Observable instance. This means that you can use .filter, .reduce and other methods to transform the stream:

const specificEventStream = 
  .filter((event) => event.name === 'some-event')

specificEventStream.subscribe((event) => {
  console.log(event.name) // "some-event" 
})

For more details see the zen-observable.

EventEmitter style

import { getDatalayer } from '@retentioneering/datalayer'

const reteDatalayer = getDatalayer()
const { emitter } = reteDatalayer 

const onEvent = (event) => {
  // do something
}

// add listener
emitter.addEventListener(onEvent)

// remove listener
emitter.removeEventListener(onEvent)

Clear datalayer

import { getDatalayer } from '@retentioneering/datalayer'

const reteDatalayer = getDatalayer()
reteDatalayer.clear()

This method only cleans up the state: removes all events from the datalayer. It has no effect on your streams, subscriptions and listeners. You should take care of it yourself.