All files client.ts

100% Statements 36/36
100% Branches 6/6
100% Functions 8/8
100% Lines 27/27

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 991x 1x 1x 1x   1x                   1x       11x   9x 9x 9x   9x 3x 3x             9x         1x 1x                     9x 2x 2x                       9x 1x 1x   1x                   9x   9x                   1x 1x            
import queryString from 'query-string'
import createAPI from './api'
import createCache from './cache'
import createUser from './user'
 
const defaultConfig = {
  apiURL: 'https://pas.put.io/api',
  cache: {
    domain: '.put.io',
    expires: 365,
  },
}
 
export type Config = typeof defaultConfig
 
export const createClientFactoryWithDependencies = (
  cacheFactory: typeof createCache,
  userFactory: typeof createUser,
  apiFactory: typeof createAPI,
) => (config = defaultConfig) => {
  {
    const cache = cacheFactory(config.cache)
    const user = userFactory(cache)
    const api = apiFactory(config.apiURL, cache)
 
    const alias = (params: { id: string | number; hash: string }) => {
      const attributes = user.alias(params)
      api.post('/alias', {
        previous_id: attributes.anonymousId,
        user_id: attributes.id,
        user_hash: attributes.hash,
      })
    }
 
    const identify = (params: {
      id: string | number
      hash: string
      properties: Record<string, any>
    }) => {
      const attributes = user.identify(params)
      api.post('/users', {
        users: [
          {
            id: attributes.id,
            hash: attributes.hash,
            properties: attributes.properties,
          },
        ],
      })
    }
 
    const track = (name: string, properties: Record<string, any> = {}) => {
      const attributes = user.attributes.getValue()
      api.post('/events', {
        events: [
          {
            user_id: attributes.id || attributes.anonymousId,
            user_hash: attributes.hash,
            name,
            properties,
          },
        ],
      })
    }
 
    const pageView = () => {
      const { search, origin, pathname } = window.location
      const { utm_source, utm_medium, utm_campaign } = queryString.parse(search)
 
      return track('page_viewed', {
        domain: origin,
        path: pathname,
        referrer: document.referrer,
        utm_source,
        utm_medium,
        utm_campaign,
      })
    }
 
    const clear = () => user.clear()
 
    return {
      alias,
      identify,
      track,
      pageView,
      clear,
    }
  }
}
 
export const createClientFactory = () => {
  return createClientFactoryWithDependencies(createCache, createUser, createAPI)
}
 
export type PutioAnalyticsClient = ReturnType<
  ReturnType<typeof createClientFactory>
>