Comparing version
import { AxiosResponse } from 'axios'; | ||
import { AmplitudeOptions, AmplitudeResponseBody, AmplitudeRequestData, AmplitudeUserActivityOptions, AmplitudeExportOptions, AmplitudeRequestDataOptions, AmplitudeSegmentationOptions } from './public'; | ||
import { AmplitudeOptions, AmplitudeRequestData, AmplitudeUserActivityOptions, AmplitudeExportOptions, AmplitudeRequestDataOptions, AmplitudeSegmentationOptions } from './public'; | ||
import { AmplitudeTrackResponse, AmplitudeIdentifyResponse, AmplitudeUserSearchResponse, AmplitudeUserActivityResponse, AmplitudeResponseBody } from './responses'; | ||
export default class Amplitude { | ||
@@ -11,8 +12,8 @@ private readonly token; | ||
private _generateRequestData; | ||
identify(data: AmplitudeRequestData | [AmplitudeRequestData]): Promise<AmplitudeResponseBody>; | ||
track(data: AmplitudeRequestData | Array<AmplitudeRequestData>, options?: AmplitudeRequestDataOptions): Promise<AmplitudeResponseBody>; | ||
identify(data: AmplitudeRequestData | [AmplitudeRequestData]): Promise<AmplitudeIdentifyResponse>; | ||
track(data: AmplitudeRequestData | Array<AmplitudeRequestData>, options?: AmplitudeRequestDataOptions): Promise<AmplitudeTrackResponse>; | ||
export(options: AmplitudeExportOptions): Promise<AxiosResponse>; | ||
userSearch(userSearchId: string): Promise<AmplitudeResponseBody>; | ||
userActivity(amplitudeId: string | number, params?: AmplitudeUserActivityOptions): Promise<AmplitudeResponseBody>; | ||
userSearch(userSearchId: string): Promise<AmplitudeUserSearchResponse>; | ||
userActivity(amplitudeId: string | number, params?: AmplitudeUserActivityOptions): Promise<AmplitudeUserActivityResponse>; | ||
eventSegmentation(params: AmplitudeSegmentationOptions): Promise<AmplitudeResponseBody>; | ||
} |
@@ -19,2 +19,3 @@ "use strict"; | ||
const AMPLITUDE_DASHBOARD_ENDPOINT = 'https://amplitude.com/api/2'; | ||
axios_1.default.defaults.headers.common['User-Agent'] = `amplitude/${require('../package').version} node/${process.version} (${process.arch})`; | ||
const camelCaseToSnakeCasePropertyMap = { | ||
@@ -55,4 +56,2 @@ userId: 'user_id', | ||
const transformedKey = camelCaseToSnakeCasePropertyMap[key] || key; | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-ignore | ||
// @ts-ignore | ||
obj[transformedKey] = item[key]; | ||
@@ -59,0 +58,0 @@ return obj; |
import { AxiosError } from 'axios'; | ||
import { AmplitudeResponseBody } from './responses'; | ||
export declare class AmplitudeErrorResponse extends Error { | ||
@@ -7,2 +8,2 @@ readonly status: number; | ||
} | ||
export declare const axiosErrorCatcher: (reqPromise: Promise<object>) => Promise<object>; | ||
export declare const axiosErrorCatcher: (reqPromise: Promise<AmplitudeResponseBody>) => Promise<AmplitudeResponseBody>; |
@@ -5,2 +5,3 @@ import './types'; | ||
export * from './public'; | ||
export * from './responses'; | ||
export { AmplitudeErrorResponse } from './errors'; |
@@ -10,3 +10,2 @@ export interface AmplitudeOptions { | ||
} | ||
export declare type AmplitudeResponseBody = object; | ||
export interface AmplitudeRequestData { | ||
@@ -29,3 +28,2 @@ /** | ||
event_type?: string; | ||
eventType?: string; | ||
/** | ||
@@ -32,0 +30,0 @@ * The timestamp of the event in milliseconds since epoch. If time is not sent |
{ | ||
"name": "amplitude", | ||
"version": "5.0.1-next.4", | ||
"version": "5.0.1-next.5", | ||
"description": "A node wrapper for Amplitude analytics http api", | ||
@@ -28,3 +28,4 @@ "author": "Blade Barringer <blade@crookedneighbor.com>", | ||
"name": "Geoff Dutton", | ||
"email": "g.dutton@gmail.com" | ||
"email": "g.dutton@gmail.com", | ||
"url": "https://github.com/geoffdutton" | ||
}, | ||
@@ -67,10 +68,10 @@ { | ||
"devDependencies": { | ||
"@types/chai": "^4.2.8", | ||
"@types/chai": "^4.2.9", | ||
"@types/eslint": "^6.1.8", | ||
"@types/mocha": "^7.0.1", | ||
"@types/node": "^13.7.1", | ||
"@types/node": "^13.7.4", | ||
"@types/sinon": "^7.5.1", | ||
"@types/sinon-chai": "^3.2.3", | ||
"@typescript-eslint/eslint-plugin": "^2.19.2", | ||
"@typescript-eslint/parser": "^2.19.2", | ||
"@typescript-eslint/eslint-plugin": "^2.21.0", | ||
"@typescript-eslint/parser": "^2.21.0", | ||
"chai": "^4.2.0", | ||
@@ -84,11 +85,11 @@ "codecov": "^3.6.5", | ||
"husky": "^4.2.3", | ||
"lint-staged": "^10.0.7", | ||
"lint-staged": "^10.0.8", | ||
"mocha": "^7.0.1", | ||
"nock": "^11.8.2", | ||
"nock": "^12.0.1", | ||
"nyc": "^15.0.0", | ||
"prettier": "^1.19.1", | ||
"sinon": "^8.1.1", | ||
"sinon-chai": "^3.4.0", | ||
"sinon": "^9.0.0", | ||
"sinon-chai": "^3.5.0", | ||
"ts-node": "^8.6.2", | ||
"typescript": "^3.7.5" | ||
"typescript": "^3.8.2" | ||
}, | ||
@@ -95,0 +96,0 @@ "license": "ISC", |
124
README.md
@@ -5,4 +5,7 @@ # amplitude | ||
Server side implementation of [Amplitude](https://amplitude.com)'s http api. | ||
Server side implementation of [Amplitude](https://amplitude.com)'s HTTP API. | ||
## Version 5+ Note ## | ||
For amplitude@5+, it uses Amplitude's [V2 HTTP API](https://help.amplitude.com/hc/en-us/articles/360032842391-HTTP-API-V2), which replaces the deprecated [V1 HTTP API](https://help.amplitude.com/hc/en-us/articles/204771828-HTTP-API-Deprecated-). This only affects the [`.track` method](#track-an-event). The only potential breaking change is by default `user_id` and `device_id` require a minimum of 5 characters. | ||
## Install | ||
@@ -17,16 +20,18 @@ | ||
```javascript | ||
var Amplitude = require('src/amplitude') | ||
const Amplitude = require('amplitude') | ||
// The only required field is the api token | ||
var amplitude = new Amplitude('api-token') | ||
const amplitude = new Amplitude('api-token') | ||
``` | ||
See the [`examples/`](/examples) directory for further usage. | ||
## Track an event | ||
Pass in any keys listed on the [Amplitude http api](https://amplitude.zendesk.com/hc/en-us/articles/204771828-HTTP-API). The only required keys are `event_type` and either `user_id` or `device_id`. If you initialized the Amplitude object with a user/device id, they can be ignored when calling the track method. | ||
Pass in any keys listed on the [Amplitude V2 HTTP API](https://help.amplitude.com/hc/en-us/articles/360032842391-HTTP-API-V2). The only required keys are `event_type` and either `user_id` or `device_id`. If you initialized the Amplitude object with a user/device id, they can be ignored when calling the track method. Note: the `user_id` and `device_id` must be 5 or more characters if passed. | ||
```javascript | ||
var data = { | ||
const data = { | ||
event_type: 'some value', // required | ||
user_id: 'some id', // only required if device id is not passed in | ||
device_id: 'some id', // only required if user id is not passed in | ||
user_id: 'some-user-id', // only required if device id is not passed in | ||
device_id: 'some-device-id', // only required if user id is not passed in | ||
session_id: 1492789357923, // must be unix timestamp in ms, not required | ||
@@ -40,3 +45,7 @@ event_properties: { | ||
} | ||
amplitude.track(data) | ||
try { | ||
await amplitude.track(data) | ||
} catch (err) { | ||
console.error(err) | ||
} | ||
``` | ||
@@ -46,3 +55,3 @@ | ||
```javascript | ||
var data = [ | ||
const data = [ | ||
{ | ||
@@ -72,2 +81,5 @@ event_type: 'some value', // required | ||
amplitude.track(data) | ||
.then(res => { | ||
console.log('Amplitude response', res) | ||
}) | ||
``` | ||
@@ -80,3 +92,3 @@ | ||
```javascript | ||
var data = { | ||
const data = { | ||
user_id: 'some id', // only required if device id is not passed in | ||
@@ -92,2 +104,5 @@ device_id: 'some id', // only required if user id is not passed in | ||
amplitude.identify(data) | ||
.then(res => { | ||
console.log('Amplitude response', res) | ||
}) | ||
``` | ||
@@ -97,3 +112,3 @@ | ||
```javascript | ||
var data = [ | ||
const data = [ | ||
{ | ||
@@ -121,2 +136,5 @@ user_id: 'some id', // only required if device id is not passed in | ||
amplitude.identify(data) | ||
.then(res => { | ||
console.log('Amplitude response', res) | ||
}) | ||
``` | ||
@@ -127,3 +145,3 @@ | ||
```javascript | ||
var data = { | ||
const data = { | ||
user_id: 'some id', // only required if device id is not passed in | ||
@@ -142,4 +160,7 @@ device_id: 'some id', // only required if user id is not passed in | ||
} | ||
}; | ||
amplitude.identify(data); | ||
} | ||
amplitude.identify(data) | ||
.then(res => { | ||
console.log('Amplitude response', res) | ||
}) | ||
``` | ||
@@ -154,3 +175,3 @@ | ||
```javascript | ||
var data = { | ||
const data = { | ||
eventType: 'some value', // required | ||
@@ -168,2 +189,5 @@ userId: 'some id', // only required if device id is not passed in | ||
amplitude.track(data) | ||
.then(res => { | ||
console.log('Amplitude response', res) | ||
}) | ||
``` | ||
@@ -182,6 +206,6 @@ | ||
osName -> os_name | ||
osVersion -> os_version | ||
deviceBrand -> device_brand | ||
deviceManufacturer -> device_manufacturer | ||
deviceModel -> device_model | ||
deviceType -> device_type | ||
locationLat -> location_lat | ||
@@ -196,12 +220,18 @@ locationLng -> location_lng | ||
```javascript | ||
var amplitude = new Amplitude('api-token', { user_id: 'some-user-id' }) | ||
const amplitude = new Amplitude('api-token', { user_id: 'some-user-id' }) | ||
// or | ||
var amplitude = new Amplitude('api-token', { device_id: 'some-device-id' }) | ||
const amplitude = new Amplitude('api-token', { device_id: 'some-device-id' }) | ||
// or | ||
var amplitude = new Amplitude('api-token', { session_id: 1492789357923 }) | ||
const amplitude = new Amplitude('api-token', { session_id: 1492789357923 }) | ||
amplitude.track({ | ||
event_type: 'some value' | ||
}) | ||
try { | ||
await amplitude.track({ | ||
event_type: 'some value' | ||
}) | ||
} catch (err) { | ||
console.error(err) | ||
} | ||
// Or... | ||
amplitude.track({ | ||
@@ -211,2 +241,5 @@ event_type: 'some value', | ||
}) | ||
.then(res => { | ||
console.log('Amplitude response', res) | ||
}) | ||
``` | ||
@@ -225,2 +258,13 @@ | ||
}) | ||
// Or.. | ||
try { | ||
const result = await amplitude.track({ | ||
event_type: 'some value' | ||
}) | ||
//... do something with result | ||
} catch (error) { | ||
console.error(error) | ||
//... do something with the error | ||
} | ||
``` | ||
@@ -237,6 +281,6 @@ | ||
```javascript | ||
var fs = require('fs') | ||
var stream = fs.createWriteStream('./may-2016-export.zip') | ||
const fs = require('fs') | ||
const stream = fs.createWriteStream('./may-2016-export.zip') | ||
var amplitude = new Amplitude('api-token', { secretKey: 'secret' }) | ||
const amplitude = new Amplitude('api-token', { secretKey: 'secret' }) | ||
@@ -256,6 +300,6 @@ amplitude.export({ | ||
```javascript | ||
var amplitude = new Amplitude('api-token', { secretKey: 'secret' }) | ||
const amplitude = new Amplitude('api-token', { secretKey: 'secret' }) | ||
amplitude.userSearch('user/device/amplitude id or user id prefix').then(function (res) { | ||
var matches = res.matches // Array of matches | ||
amplitude.userSearch('user/device/amplitude id or user id prefix').then(res => { | ||
const matches = res.matches // Array of matches | ||
@@ -267,3 +311,3 @@ // How the match was made | ||
// If no match was made, type === 'nomatch' | ||
var type = res.type | ||
const type = res.type | ||
}) | ||
@@ -279,7 +323,7 @@ ``` | ||
```javascript | ||
var amplitude = new Amplitude('api-token', { secretKey: 'secret' }) | ||
const amplitude = new Amplitude('api-token', { secretKey: 'secret' }) | ||
amplitude.userActivity('Amplitude ID').then(function (res) { | ||
var userData = res.userData // data about the user | ||
var events = res.events // an array of events associated with the user | ||
const userData = res.userData // data about the user | ||
const events = res.events // an array of events associated with the user | ||
}) | ||
@@ -312,3 +356,3 @@ ``` | ||
```javascript | ||
var amplitude = new Amplitude('api-token', { secretKey: 'secret' }) | ||
const amplitude = new Amplitude('api-token', { secretKey: 'secret' }) | ||
@@ -318,8 +362,8 @@ amplitude.userSearch('user-id').then(function (res) { | ||
// you may need to handle the case where there is not a match | ||
var match = res.matches[0] | ||
const match = res.matches[0] | ||
return amplitude.userActivity(match.amplitude_id) | ||
}).then(function (res) { | ||
var userData = res.userData // data about the user | ||
var events = res.events // an array of events associated with the user | ||
const userData = res.userData // data about the user | ||
const events = res.events // an array of events associated with the user | ||
}) | ||
@@ -335,3 +379,3 @@ ``` | ||
```javascript | ||
var amplitude = new Amplitude('api-token', { secretKey: 'secret' }) | ||
const amplitude = new Amplitude('api-token', { secretKey: 'secret' }) | ||
@@ -346,3 +390,3 @@ amplitude.eventSegmentation({ | ||
.then((res) => { | ||
var segmentationData = res.data | ||
const segmentationData = res.data | ||
}) | ||
@@ -377,3 +421,3 @@ ``` | ||
View the [releases page](https://github.com/geoffdutton/amplitude/releases) for changes in each version. | ||
View the [CHANGELOG](/CHANGELOG.md) for changes in each version. | ||
@@ -388,4 +432,4 @@ <!--- | ||
+ [Matthew Keesan](http://keesan.net) | ||
+ Geoff Dutton | ||
+ [Geoff Dutton](https://github.com/geoffdutton) | ||
+ Matt Pardee | ||
+ [Chase Seibert](http://chase-seibert.github.io/blog/) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
40897
7.28%25
13.64%519
5.49%409
12.05%