Pendo NPS(Poll)結果をAPIで取得する方法

自分用メモ兼ねて。

Pendoで取得したNPSスコアのとり方について

NPSはそれぞれの結果が別々のpollとして保存されてる。
eg: 数値とテキスト

まず、該当のNPS Guide IDを元にそれぞれのpoll IDを取得する必要がある。
Guide IDはPendoアプリのNPS設定画面のURLから取得可能。/guides/に続く文字列がGuide ID

例：app.pendo.io/s/1234567890/net-promoter-system/guides/`ここ`/

Post: https://app.pendo.io/api/v1/aggregation

Body:

```json
{
    "response": {},
    "request": {
        "name": "pollIds",
        "pipeline": [
            {
                "source": {
                    "guides": null
                }
            },
            {
                "filter": "id==`GUIDE-ID`" <=== Guide IDを入力
            },
            {
                "select": {
                    "pollId1": "polls[0].id", <===結果1: 数値
                    "pollId2": "polls[1].id" <===結果2: テキスト
                }
            }
        ],
        "requestId": "pollIds"
    }
}
```

上記クエリから返ってきたpollidを元に実際のpollの内容を取得しに行きます。

bulkexpandを使うとvisitorとaccount情報が展開できる。account/visitor情報がいらいない場合は、bulkexpandを削除する。

```json
{
    "response": {
        "mimeType": "application/json"
    },
    "request": {
        "name": "NPSResponses",
        "pipeline": [
            {
                "spawn": [
                    [
                        {
                            "source": {
                                "pollsSeenEver": {
                                    "guideId": "jTTGSDbEKb-3j1oIvqFbVxPMGlU",
                                    "pollId": "jzhpz9wxxoe"
                                }
                            }
                        },
                        {
                            "identified": "visitorId"
                        },
                        {
                            "cat": null
                        },
                        {
                            "select": {
                                "visitorId": "visitorId",
                                "accountId": "accountId",
                                "poll1": "response",
                                "browserTime1": "time"
                            }
                        }
                    ],
                    [
                        {
                            "source": {
                                "pollsSeenEver": {
                                    "guideId": "jTTGSDbEKb-3j1oIvqFbVxPMGlU",
                                    "pollId": "h4scwr14cnk"
                                }
                            }
                        },
                        {
                            "identified": "visitorId"
                        },
                        {
                            "cat": null
                        },
                        {
                            "select": {
                                "visitorId": "visitorId",
                                "accountId": "accountId",
                                "poll2": "response",
                                "browserTime2": "time"
                            }
                        }
                    ]
                ]
            },
            {
                "join": {
                    "fields": [
                        "visitorId"
                    ],
                    "width": 2
                }
            },
            {
                "bulkExpand": {
                    "visitor": {
                        "visitor": "visitorId"
                    }
                }
            },
            {
                "bulkExpand": {
                    "account": {
                        "account": "accountId"
                    }
                }
            }
        ],
        "requestId": "NPSResponses"
    }
}
```

結果

```json
{
    "results": [
        {
            "browserTime2": 1640066859514,
            "visitorId": "{CCE557F5-E3F8-48CD-B62E-97D561E7BE7A}",
            "accountId": "defde67a-963a-4050-8dbb-9b56b61db249",
            "poll2": "I like it! awesome",<== Poll結果
            "poll1": 10, <== poll結果
            "browserTime1": 1640066859513,
            "visitor": {
                "auto": {
                    "accountid": "defde67a-963a-4050-8dbb-9b56b61db249",
                    "accountids": [
                        "defde67a-963a-4050-8dbb-9b56b61db249"
                    ],
                    "firstvisit": 1639412855816,
                    "id": "{CCE557F5-E3F8-48CD-B62E-97D561E7BE7A}",
                    "idhash": 312629961,
                    "lastbrowsername": "Edge",
                    "lastbrowserversion": "96.0.1054",
                    "lastoperatingsystem": "Mac OS X",
                    "lastservername": "org75206b99.crm7.dynamics.com",
                    "lastupdated": 1640066839396,
                    "lastuseragent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36 Edg/96.0.1054.57",
                    "lastvisit": 1640066838641
                },
                "agent": {
                    "full_name": "にゃー",
                    "language": "en_US"
                },
                "auto__323232": {
                    "firstvisit": 1639412855816,
                    "lastbrowsername": "Edge",
                    "lastbrowserversion": "96.0.1054",
                    "lastoperatingsystem": "Mac OS X",
                    "lastservername": "org75206b99.crm7.dynamics.com",
                    "lastuseragent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36 Edg/96.0.1054.57",
                    "lastvisit": 1640066838641
                }
            },
            "account": {
                "auto": {
                    "firstvisit": 1639412855816,
                    "id": "defde67a-963a-4050-8dbb-9b56b61db249",
                    "idhash": 3807362995,
                    "lastupdated": 1640066839332,
                    "lastvisit": 1640066838641
                },
                "agent": {
                    "name": "org29663547"
                },
                "auto__323232": {
                    "firstvisit": 1639412855816,
                    "lastupdated": 1639412856126,
                    "lastvisit": 1640066838641
                }
            }
        }
    ]
}
```