Repository: line/line-bot-sdk-nodejs (ts)

Issue:
Support get statistics per aggregation unit
News: https://developers.line.biz/en/news/2021/03/10/messaging-api-update-march-2021/#202103-02
Doc: https://developers.line.biz/en/reference/partner-docs/#get-statistics-per-unit

Pull request description:
Implement getStatisticsPerUnit
This should close #280 

Patch:
diff --git a/docs/api-reference/client.md b/docs/api-reference/client.md
index 399200b..e1e87b0 100644
--- a/docs/api-reference/client.md
+++ b/docs/api-reference/client.md
@@ -84,6 +84,7 @@ class Client {
   getNumberOfFollowers(date: string): Promise<Types.NumberOfFollowersResponse>
   getFriendDemographics(): Promise<Types.FriendDemographics>
   getUserInteractionStatistics(requestId: string): Promise<Types.UserInteractionStatistics>
+  getStatisticsPerUnit(customAggregationUnit: string, from: string, to: string): Promise<Types.StatisticsPerUnit>
 
   // AudienceGroup
   createUploadAudienceGroup(uploadAudienceGroup: {
@@ -751,6 +752,14 @@ It corresponds to the [Get number of followers](https://developers.line.biz/en/r
 
 It corresponds to the [Get friend demographics](https://developers.line.biz/en/reference/messaging-api/#get-demographic) API.
 
+#### `getUserInteractionStatistics(): Promise<Types.UserInteractionStatistics>`
+
+It corresponds to the [Get user interaction statistics](https://developers.line.biz/en/reference/messaging-api/#get-message-event) API.
+
+#### `getStatisticsPerUnit(): Promise<Types.StatisticsPerUnit>`
+
+It corresponds to the [Get statistics per unit](https://developers.line.biz/en/reference/messaging-api/#get-statistics-per-unit) API.
+
 ### Bot
 
 #### `getBotInfo(): Promise<BotInfoResponse>`
diff --git a/lib/client.ts b/lib/client.ts
index 3b49e45..f2907d9 100644
--- a/lib/client.ts
+++ b/lib/client.ts
@@ -515,6 +515,17 @@ export default class Client {
     return ensureJSON(res);
   }
 
+  public async getStatisticsPerUnit(
+    customAggregationUnit: string,
+    from: string,
+    to: string,
+  ): Promise<Types.StatisticsPerUnit> {
+    const res = await this.http.get<Types.StatisticsPerUnit>(
+      `${MESSAGING_API_PREFIX}/insight/message/event/aggregation?customAggregationUnit=${customAggregationUnit}&from=${from}&to=${to}`,
+    );
+    return ensureJSON(res);
+  }
+
   public async createUploadAudienceGroup(uploadAudienceGroup: {
     description: string;
     isIfaAudience?: boolean;
diff --git a/lib/types.ts b/lib/types.ts
index 1920db4..569fc51 100644
--- a/lib/types.ts
+++ b/lib/types.ts
@@ -2499,6 +2499,20 @@ export type UserInteractionStatistics = {
   clicks: UserInteractionStatisticsOfEachURL[];
 };
 
+/**
+ * https://developers.line.biz/en/reference/messaging-api/#get-statistics-per-unit
+ */
+export type StatisticsPerUnit = {
+  overview: {
+    uniqueImpression: number;
+    uniqueClick: number;
+    uniqueMediaPlayed: number;
+    uniqueMediaPlayed100Percent: number;
+  };
+  messages: UserInteractionStatisticsOfEachMessage[];
+  clicks: UserInteractionStatisticsOfEachURL[];
+};
+
 type FilterOperatorObject<T> = {
   type: "operator";
 } & (


Test patch:
diff --git a/test/client.spec.ts b/test/client.spec.ts
index 6f60afa..07e9a27 100644
--- a/test/client.spec.ts
+++ b/test/client.spec.ts
@@ -744,6 +744,24 @@ describe("client", () => {
     equal(scope.isDone(), true);
   });
 
+  it("getStatisticsPerUnit", async () => {
+    const customAggregationUnit = "promotion_a";
+    const from = "20210301";
+    const to = "20210331";
+    const scope = mockGet(
+      MESSAGING_API_PREFIX,
+      "/insight/message/event/aggregation",
+      {
+        customAggregationUnit,
+        from,
+        to,
+      },
+    );
+
+    await client.getStatisticsPerUnit(customAggregationUnit, from, to);
+    equal(scope.isDone(), true);
+  });
+
   it("createUploadAudienceGroup", async () => {
     const requestBody = {
       description: "audienceGroupName",