Skip to content

Commit fb42001

Browse files
authored
feat(PR-40): add insights summary (#106)
Add support for form insights endpoint https://www.typeform.com/developers/responses/reference/retrieve-form-insights/
1 parent b50d27b commit fb42001

File tree

5 files changed

+78
-0
lines changed

5 files changed

+78
-0
lines changed

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,14 @@ Each one of them encapsulates the operations related to it (like listing, updati
310310
- `fields`: Limit request to only responses for the specified fields. Accepts either a string or an array of strings.
311311
- For parameter details check [the documentation](https://developer.typeform.com/responses/reference/retrieve-responses/)
312312

313+
### Insights
314+
315+
#### `insights.summary({ uid })`
316+
317+
- Returns form level and individual question level insights for a given form.
318+
- `uid`: Unique ID for the form.
319+
320+
313321
### Webhooks
314322

315323
#### `webhooks.create({ uid, tag, url, enabled = false, secret, verifySSL })`

src/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { Workspaces } from './workspaces'
66
import { Responses } from './responses'
77
import { Webhooks } from './webhooks'
88
import { Typeform } from './typeform-types'
9+
import { Insights } from './insights'
910

1011
export { Typeform } from './typeform-types'
1112

@@ -19,5 +20,6 @@ export const createClient = (args: Typeform.ClientArg = { token: null }) => {
1920
workspaces: new Workspaces(http),
2021
responses: new Responses(http),
2122
webhooks: new Webhooks(http),
23+
insights: new Insights(http),
2224
}
2325
}

src/insights.ts

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { Typeform } from './typeform-types'
2+
3+
export class Insights {
4+
constructor(private _http: Typeform.HTTPClient) {}
5+
6+
public summary(args: { uid: string }): Promise<Typeform.Insights> {
7+
const { uid } = args
8+
9+
return this._http.request({
10+
method: 'get',
11+
url: `/insights/${uid}/summary`,
12+
})
13+
}
14+
}

src/typeform-types.ts

+31
Original file line numberDiff line numberDiff line change
@@ -1408,6 +1408,37 @@ export namespace Typeform {
14081408
role: 'owner' | 'member'
14091409
}[]
14101410
}
1411+
/**
1412+
* Field level insights.
1413+
*/
1414+
export interface FieldInsights {
1415+
id: string
1416+
ref: string
1417+
title: string
1418+
type: Type
1419+
dropoffs: number
1420+
views: number
1421+
}
1422+
/**
1423+
* Counters and calculations for the results of form.
1424+
*/
1425+
interface FormInsights {
1426+
average_time: number
1427+
completion_rate: number
1428+
responses_count: number
1429+
total_visits: number
1430+
unique_visits: number
1431+
}
1432+
interface PlatformFormInsights extends FormInsights {
1433+
platform: 'desktop' | 'other' | 'phone' | 'tablet'
1434+
}
1435+
export interface Insights {
1436+
fields: FieldInsights[]
1437+
form: {
1438+
platforms: PlatformFormInsights[]
1439+
summary: FormInsights
1440+
}
1441+
}
14111442
export class ApiError extends Error {
14121443
code: string
14131444
details: Object[]

tests/unit/insights.test.ts

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { axios } from '../common'
2+
import { clientConstructor } from '../../src/create-client'
3+
import { Insights } from '../../src/insights'
4+
import { API_BASE_URL } from '../../src/constants'
5+
6+
beforeEach(() => {
7+
axios.reset()
8+
axios.onAny().replyOnce(200, {})
9+
})
10+
11+
const http = clientConstructor({
12+
token: '123',
13+
})
14+
const insightsRequest = new Insights(http)
15+
16+
test('get insights summary has correct method and path', async () => {
17+
const uid = 'foobar'
18+
await insightsRequest.summary({ uid })
19+
expect(axios.history.get[0].url).toBe(
20+
`${API_BASE_URL}/insights/${uid}/summary`
21+
)
22+
expect(axios.history.get[0].method).toBe('get')
23+
})

0 commit comments

Comments
 (0)