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

Mistake in documentation track method #210

Closed
Barik85 opened this issue Jun 20, 2023 · 1 comment · Fixed by #211
Closed

Mistake in documentation track method #210

Barik85 opened this issue Jun 20, 2023 · 1 comment · Fixed by #211

Comments

@Barik85
Copy link

Barik85 commented Jun 20, 2023

As mentioned in docs https://github.com/Iterable/iterable-web-sdk#track
importing track method should be:
import { track } from '@iterable/web-sdk/dist/events';

so I implemented 2 custom methods, one for initialize and second for sending analytics (call them in separate components):

import { initialize } from '@iterable/web-sdk'
import { track } from '@iterable/web-sdk/dist/events'
import { format } from 'date-fns'

import { getIterableToken } from '@api/user'

interface Config {
  token: string
}

export const initializeIterableSdk = ({ token }: Config) => {
  const data = initialize(
    process.env.NEXT_PUBLIC_ITERABLE_API_KEY || '',
    async () => {
      const tokenResponse = await getIterableToken(token)
      const iterableToken = tokenResponse?.data?.token

      return iterableToken
    }
  )
  return data
}

interface AnalyticsConfig {
  eventName: string
  dataFields?: Record<string, any>
}

export const sendIterableAnalytics = (data: AnalyticsConfig) => {
  if (process.env.NODE_ENV !== 'production') return

  const { eventName, dataFields = {} } = data

  const date = format(new Date(), 'yyyy-MM-dd HH:mm:ss XXX')

  track({
    eventName,
    dataFields: {
      ...dataFields,
      platform: 'web',
      date,
    },
  }).catch(() => {
    // silent
  })
}

initializeIterableSdk method works fine but sendIterableAnalytics fails with error 401 "No API key found on request". It says that initialize and track are in different scopes.
The way to fix it is import from correct place:

import { initialize, track } from '@iterable/web-sdk'
@mprew97
Copy link
Contributor

mprew97 commented Jun 20, 2023

Hi @Barik85! Thanks for this. We will take a look.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants