Guide AlertをAggregation APIでとる方法

自分用メモシリーズ
過去180日のガイドアラートを取ってくる



```python
#prepare rest api call
import requests
import json
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import json
# pendo aggregation api
pendo_url = 'https://app.pendo.io/api/v1/aggregation/'
pendo_token = "xxxxxx"
pendo_headers = {'x-pendo-integration-key': pendo_token, 'content-type': 'application/json'}
query =  {"request": {
        "name": "Guide Alerts",
        "pipeline": [
            {
                "source": {
                    "guideEvents": {
                        "blacklist": "apply",
                        "appId": "-323232"
                    },
                    "timeSeries": {
                        "period": "dayRange",
                        "first": "now()",
                        "count": -180
                    }
                }
            },
            {
                "filter": "type==\"guideTimeout\""
            },
            {
                "merge": {
                    "fields": [
                        "guideId",
                        "guideStepId"
                    ],
                    "mappings": {
                        "numTotalVisitors": "numTotalVisitors"
                    },
                    "pipeline": [
                        {
                            "source": {
                                "guideEvents": {
                                    "blacklist": "apply",
                                    "appId": "expandAppIds(\"*\")"
                                },
                                "timeSeries": {
                                    "period": "dayRange",
                                    "first": now(),
                                    "count": -180
                                }
                            }
                        },
                        {
                            "filter": "type==\"guideSeen\" || type==\"guideTimeout\""
                        },
                        {
                            "group": {
                                "group": [
                                    "guideStepId",
                                    "guideId"
                                ],
                                "fields": [
                                    {
                                        "numTotalVisitors": {
                                            "count": "visitorId"
                                        }
                                    }
                                ]
                            }
                        }
                    ]
                }
            },
            {
                "group": {
                    "group": [
                        "guideId",
                        "guideStepId",
                        "guideSeenReason"
                    ],
                    "fields": [
                        {
                            "numTimeoutVisitors": {
                                "count": "visitorId"
                            }
                        },
                        {
                            "numTotalVisitors": {
                                "first": "numTotalVisitors"
                            }
                        },
                        {
                            "lastOccurrence": {
                                "max": "browserTime"
                            }
                        },
                        {
                            "firstOccurrence": {
                                "min": "browserTime"
                            }
                        }
                    ]
                }
            },
            {
                "merge": {
                    "fields": [
                        "guideId"
                    ],
                    "pipeline": [
                        {
                            "source": {
                                "guides": {
                                    "appId": "expandAppIds(\"*\")"
                                }
                            }
                        },
                        {
                            "eval": {
                                "guideId": "id"
                            }
                        },
                        {
                            "unwind": {
                                "field": "steps",
                                "prefix": True
                            }
                        }
                    ]
                }
            },
            {
                "eval": {
                    "percentVisitorsImpacted": "round(10000 * (numTimeoutVisitors / numTotalVisitors)) / 100"
                }
            },
            {
                "select": {
                    "guideName": "name",
                    "guideId": "guideId",
                    "appId": "appId",
                    "lastOccurrence": "lastOccurrence",
                    "firstOccurrence": "firstOccurrence",
                    "lastUpdatedByUser": "lastUpdatedByUser.username",
                    "lastUpdatedAt": "lastUpdatedAt",
                    "audienceFilter": "audienceUiHint.filters",
                    "launchMethod": "launchMethod",
                    "visitorsImpacted": "numTimeoutVisitors",
                    "totalVisitors": "numTotalVisitors",
                    "percentVisitorsImpacted": "percentVisitorsImpacted",
                    "displayAlertType": "guideSeenReason",
                    "steps": "steps",
                    "impactedStepId": "guideStepId"
                }
            }
        ],
        "params": {
            "appId": "-323232",
            "blacklist": "apply",
            "timeSeries": {
                "period": "dayRange",
                "first": "now()",
                "count": -180
            }
        }
    }
}
body = json.dumps(query)

# send request as post
response = requests.post(pendo_url, headers=pendo_headers, data=body)

df = pd.DataFrame(response.json()['results'])
```

## ErrorになっているStep IDを抜き出す
```python
steps = df['steps']
for i in range(len(steps)):
    for j in range(len(steps[i])):
        #if step id matches record
        if steps[i][j]['id'] == df['impactedStepId'][i]:
            df.loc[i, 'stepnum'] = j


df[['percentVisitorsImpacted','visitorsImpacted','guideName','displayAlertType','stepnum','impactedStepId']]
```

## 結果
影響を受けたvisitor％、影響を受けたvisitor#, ガイドの名前, エラータイプ, ステップ番号, ステップID
![CleanShotuJkC25OZ.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/227518/a84df027-3d83-a498-114d-65afea7134d5.png)