npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

visitor-segments

v1.1.6

Published

Hellobar Segments.

Downloads

18

Readme

Hellobar.Segments

Installation

Install with either yarn or npm:

yarn add visitor-segments
#or
npm install --save visitor-segments

Configure

In order to use pro.ip-api.com configure IPApiProvider. Otherwise, it will be using public ip-api.com with the limit of 45 HTTP requests per minute from an IP address.

  IPApiProvider.key = "pro.ip-api.com secret key"

Usage

  import Segments from 'visitor-segments'
  
  const scope = 'default'
  const segments = new Segments(scope)
  
  await segments.visit()

  segments.visits.count === 1
  segments.geolocation.city === 'Miami'
  segments.page.path == window.location.pathname
  segments.page.device == 'computer'

  await segments.visit()
  segments.visits.count == 2

  // custom data
  segments.set('coupon', 'SPECIAL10')

  const message = segments.interpolate("Here's your coupon: {{ coupon }}")
  message ===  "Here's your coupon: SPECIAL10"

Modules

import {segments} from "visitor-segments";

// Visits
segments.visits.count
segments.visits.first
segments.visits.firstDate
segments.visits.last
segments.visits.lastDate
segments.visits.daysFromFirst
segments.visits.daysFromLast

// Session
segments.session.count
segments.session.uuid

// Referrer
segments.referrer.originalReferrer
segments.referrer.referrer
segments.referrer.referrerDomain
segments.referrer.referrerTerms
segments.referrer.previousPage

// Page
segments.page.device // mobile, tablet or computer
segments.page.cookies // cookies object
segments.page.date // current date as string 2000-01-01 
segments.page.dayOfWeek  
segments.page.url //location.href
segments.page.path // location.pathname 
segments.page.urlKeywords // words from the current url  

// Params

// utm_* params, stored forever once appeared
// so that even if utm_* isn't in the current url you may have it here 
segments.params.campaign   
segments.params.content  
segments.params.medium   
segments.params.source   
segments.params.term 

// all current params  
segments.params.all 

// Conversions
segments.conversions.count 
segments.conversions.first 
segments.conversions.firstDate 
segments.conversions.last  
segments.conversions.lastDate

// Geolocation
segments.geolocation.city
segments.geolocation.region
segments.geolocation.regionName
segments.geolocation.country
segments.geolocation.countryName
segments.geolocation.timezone
segments.geolocation.mobileCell // true or false

// Callbacks 
segments.onUpdate((key, value) => {
  console.log(key, value)
})

// Additional in memory store in case you want to use it for interpolation
interface DataStorage {
  set(key: string, value: any): void;
  get(key: string): string | number;
  onUpdate(handler: UpdateKeyHandler): void;
}
const extraStorage = new DataStorage()
const segments = new Segments(scope, { extraStorage })
extraStorage.set('testKey', 1)
segments.get('testKey') === 1
segments.interpolate('in memory value: {{testKey}}') === 'in memory value: 1'
// testKey will disappear after page reload

Build your own Segments

import {Segment, SegmentsAdapters, AbstractSegments, SegmentClassMap, ValueStorageInterface} from "visitor-segments";

class FooSegment extends Segment {
  constructor(visitor: ValueStorageInterface) {
    super(SEGMENT_KEYS.FOO, visitor);
  }

  setValue() {
    //super.setValue(Math.random());
    super.setValueOnce(Math.random());
  }
}

export enum SEGMENT_KEYS {
  FOO = 'f'
}

export const SEGMENTS: SegmentClassMap = {
  [SEGMENT_KEYS.FOO]: FooSegment
}

export class MySegments extends AbstractSegments {
  #foo: FooSegment;

  constructor(scope: string, options?: SegmentsAdapters) {
    super(scope, {...options, segmentsMap: SEGMENTS})
    this.#foo = this.getSegmentByKey(SEGMENT_KEYS.FOO)
  }

  get foo(): number {
    return this.#foo.value
  }
  
  async visit() {
    await super.visit()
    this.#foo.setValue()
  }
}

const segments = new MySegments('default')
segments.visit()

Development

Run tests

yarn watch
  1. Write your code.
  2. All tests must pass.
  3. Commit the code.
  4. Publishing New Version This package follows semantic versioning. To publish a new version:
yarn publish

License

visitor-segments is released under the MIT license.