Skip to content

Commit c473daa

Browse files
authored
Adding OAuth information to Analytics Node doc
1 parent 05c6dfa commit c473daa

File tree

1 file changed

+32
-0
lines changed
  • src/connections/sources/catalog/libraries/server/node

1 file changed

+32
-0
lines changed

src/connections/sources/catalog/libraries/server/node/index.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,38 @@ const analytics = new Analytics({
635635
})
636636
```
637637
638+
### OAuth 2
639+
640+
In order to guarantee authorized communication between your server environment and Segment's Tracking API, you can enable OAuth 2 in your Segment workspace. To support the non-interactive server environment, the OAuth workflow used is a signed client assertion JWT. You will need a public and private key pair where the public key is uploaded to the segment dashboard and the private key is kept in your server environment to be used by this SDK. Your server will verify its identity by signing a token request and will receive a token that is used to to authorize all communication with the Segment Tracking API.
641+
642+
You will also need to provide the OAuth Application ID and the public key's ID, both of which are provided in the Segment dashboard. There are also options available to specify the authorization server, custom scope, maximum number of retries, or a custom HTTP client if your environment has special rules for separate segment endpoints.
643+
644+
You should ensure that you are implementing handling for Analytics SDK errors. Good logging will help distinguish any configuration issues.
645+
646+
For more information, see the [Segment OAuth 2.0 documentation](/docs/connections/oauth/)
647+
648+
```ts
649+
import { Analytics, OAuthSettings } from '@segment/analytics-node';
650+
import { readFileSync } from 'fs'
651+
652+
const privateKey = readFileSync('private.pem', 'utf8')
653+
654+
const settings: OAuthSettings = {
655+
clientId: '<CLIENT_ID_FROM_DASHBOARD>',
656+
clientKey: privateKey,
657+
keyId: '<PUB_KEY_ID_FROM_DASHBOARD>',
658+
}
659+
660+
const analytics = new Analytics({
661+
writeKey: '<MY_WRITE_KEY>',
662+
oauthSettings: settings,
663+
})
664+
665+
analytics.on('error', (err) => { console.error(err) })
666+
667+
analytics.track({ userId: 'foo', event: 'bar' })
668+
669+
```
638670
## Troubleshooting
639671
640672
{% include content/troubleshooting-intro.md %}

0 commit comments

Comments
 (0)