🏟️

【LINE API】配信分析APIの使用方法をメモ

2022/11/24に公開

今回は、こちらのツイートで紹介した API の使用方法について紹介します。

一斉配信の分析 API
https://developers.line.biz/ja/reference/messaging-api/#get-message-event

セグメント配信の分析 API
https://developers.line.biz/ja/reference/messaging-api/#get-statistics-per-unit

使用方法

API

export const getLineInsight = async (
  unitId: string,
  from: string,
  to: string
): Promise<LineInsight | null> => {
  const url = "https://api.line.me/v2/bot/insight/message/event/aggregation";
  const params = {
    customAggregationUnit: unitId,
    from,
    to,
  };
  const headers = {
    Authorization: `Bearer ${LINE_MESSAGING_CHANNEL_ACCESS_TOKEN}`,
  };

  try {
    const { data } = await axios.get(url, {
      params,
      headers,
    });
    return data;
  } catch (err) {
    console.error(err);
    return null;
  }
};

interface LineInsight {
  overview: {
    uniqueImpression: number | null;
    uniqueClick: number | null;
    uniqueMediaPlayed: number | null;
    uniqueMediaPlayed100Percent: number | null;
  };
  messages: [
    {
      seq: number;
      impression: number | null;
      mediaPlayed: number | null;
      mediaPlayed25Percent: number | null;
      mediaPlayed50Percent: number | null;
      mediaPlayed75Percent: number | null;
      mediaPlayed100Percent: number | null;
      uniqueMediaPlayed: number | null;
      uniqueMediaPlayed25Percent: number | null;
      uniqueMediaPlayed50Percent: number | null;
      uniqueMediaPlayed75Percent: number | null;
      uniqueMediaPlayed100Percent: number | null;
    }
  ];
  clicks: {
    seq: number;
    url: string;
    click: number;
    uniqueClick: number | null;
    uniqueClickOfRequest: number | null;
  }[];
}

unitIdというのが LINE ドキュメントにも説明があるグループ ID(ユニット名)のことです。
https://developers.line.biz/ja/docs/messaging-api/unit-based-statistics-aggregation/#assign-names-to-units-when-sending-messages

この ID をセグメント配信ごとに振り分けることで各々の分析が可能になります。

GitHubで編集を提案
YOSHINANI

Discussion