🏟️

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

2022/11/23に公開約1,900字

今回は、こちらのツイートで紹介した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をセグメント配信ごとに振り分けることで各々の分析が可能になります。

Discussion

ログインするとコメントできます