# Use Cases

The Persistr Command Line Interface (CLI) makes it easy to create and manage your Persistr spaces directly from the terminal. It's an essential part of using Persistr.

# Analytics

Push your application events and then retrieve them later in aggregated form as analytics that make sense to you and your users.

# Push application events

await stream.events().write('Invite Sent', {
  email: 'tgulyz@altervista.org'
})

await stream.events().write('Account Created', {
  company: 'Buzzshare',
  phone: '+1-408-704-1043',
  name: {
    first: 'Terri',
    last: 'Guly'
  }
})

await stream.events().write('Trial Started', {
  plan: 'Business'
})

# Query aggregated analytics

await db
  .events('Trial Started')
  .filter({ plan: { $eq: 'Business' }})
  .group({ timestamp: 'yyyy-LL' })
  .rollup({ month: '$key', trials: '$count' })
  .each(group => console.log(group))

Query results look like this:

{ trials: 1, month: '2020-03' }

# Customer Journey

Understand all aspects of a customer's journey and make better decisions.

# Push customer events

await stream.events().write('Invite Sent', {
  email: 'tgulyz@altervista.org'
})

await stream.events().write('Account Created', {
  company: 'Buzzshare',
  phone: '+1-408-704-1043',
  name: {
    first: 'Terri',
    last: 'Guly'
  }
})

await stream.events().write('Trial Started', {
  plan: 'Business'
})

# Read events in order

await db.events({ until: 'caught-up' }).each(event => console.log(event))

You'll get back your events shaped like this:

{
  data: { email: 'tgulyz@altervista.org' },
  meta: {
    tz: 'America/Vancouver',
    ts: '2020-03-08T06:28:16.944Z',
    type: 'Invite Sent',
    id: 'f1cc708e-05c2-4479-895a-97cdc8fec027',
    stream: '89990e15-77e5-4b1e-adc0-0aa6e83a1e54',
    domain: '',
    space: 'analytics'
  }
}

{
  data: {
    company: 'Buzzshare',
    phone: '+1-408-704-1043',
    name: { first: 'Terri', last: 'Guly' }
  },
  meta: {
    tz: 'America/Vancouver',
    ts: '2020-03-08T06:28:16.945Z',
    type: 'Account Created',
    id: 'd87ee3fe-d11c-4592-a7cd-04d3d1119118',
    stream: '89990e15-77e5-4b1e-adc0-0aa6e83a1e54',
    domain: '',
    space: 'analytics'
  }
}

{
  data: { plan: 'Business' },
  meta: {
    tz: 'America/Vancouver',
    ts: '2020-03-08T06:28:16.945Z',
    type: 'Trial Started',
    id: 'a907daeb-a73a-4d61-85e3-c71a9c9bf153',
    stream: '89990e15-77e5-4b1e-adc0-0aa6e83a1e54',
    domain: '',
    space: 'analytics'
  }
}

# Microservices

Build microservices in any programming language and connect them together through a shared language-agnostic event bus.

# Push microservice events

curl {base}/projects/{project}/objects/{object}/events \
-H "Authorization: {secret_key}" \
-H 'Content-Type: application/json' \
-d '{
      "meta": {
        "type": "Invite Sent"
      },
      "data": {
        "email": "tgulyz@altervista.org"
      }
    }'

# Read events

curl {base}/projects/{project}/events?until=caught-up&limit=50 \
-H "Authorization: {secret_key}"

# Real-Time

Listen to events as they happen and react in real-time.

# Push events

await stream.events().write('Account Created', {
  company: 'Buzzshare',
  phone: '+1-408-704-1043',
  email: 'tgulyz@altervista.org',
  name: {
    first: 'Terri',
    last: 'Guly'
  }
})

# React to events in real-time

await db.events().each(event => {
  if (event.meta.type === 'Account Created') {
    // Send a welcome email (using Mailgun).
    mailgun.messages().send({
      from: '...',
      to: event.data.email,
      subject: 'Welcome!',
      text: 'We hope you enjoy our product'
    })
  }
})