Skip to content

Commit 8679d6c

Browse files
authored
Merge pull request #166 from line/feature/endpoints_for_friend_statistics
Support user interaction statistics API
2 parents 72faea0 + c274f60 commit 8679d6c

File tree

3 files changed

+141
-0
lines changed

3 files changed

+141
-0
lines changed

examples/kitchensink/app.rb

+30
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,17 @@ def reply_text(event, texts)
2727
)
2828
end
2929

30+
def broadcast(messages)
31+
client.broadcast(messages)
32+
end
33+
3034
def reply_content(event, messages)
3135
res = client.reply_message(
3236
event['replyToken'],
3337
messages
3438
)
3539
logger.warn res.read_body unless Net::HTTPOK === res
40+
res
3641
end
3742

3843
post '/callback' do
@@ -532,6 +537,31 @@ def handle_message(event)
532537
client.leave_room(event['source']['roomId'])
533538
end
534539

540+
when 'stats'
541+
response = broadcast({
542+
type: 'template',
543+
altText: 'stats',
544+
template: {
545+
type: 'buttons',
546+
thumbnailImageUrl: THUMBNAIL_URL,
547+
title: 'stats sample',
548+
text: 'Hello, my stats',
549+
actions: [
550+
{ label: 'Go to line.me', type: 'uri', uri: 'https://line.me', altUri: {desktop: 'https://line.me#desktop'} },
551+
{ label: 'Send postback', type: 'postback', data: 'hello world' },
552+
{ label: 'Send postback2', type: 'postback', data: 'hello world', text: 'hello world' },
553+
{ label: 'Send message', type: 'message', text: 'This is message' }
554+
]
555+
}
556+
})
557+
request_id = response.header["X-Line-Request-Id"]
558+
reply_text(event, "RequestId: #{request_id}")
559+
560+
when /\Astats\s+(?<request_id>.+)/
561+
request_id = Regexp.last_match[:request_id]
562+
stats = client.get_user_interaction_statistics(request_id)
563+
reply_text(event, "[STATS]\n#{stats.body}")
564+
535565
else
536566
reply_text(event, "[ECHO]\n#{event.message['text']}")
537567

lib/line/bot/client.rb

+12
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,18 @@ def get_number_of_message_deliveries(date)
563563
get(endpoint, endpoint_path, credentials)
564564
end
565565

566+
# Returns statistics about how users interact with narrowcast messages or broadcast messages sent from your LINE Official Account.
567+
#
568+
# @param [String] request_id
569+
#
570+
# @return [Net::HTTPResponse]
571+
def get_user_interaction_statistics(request_id)
572+
channel_token_required
573+
574+
endpoint_path = "/bot/insight/message/event?requestId=#{request_id}"
575+
get(endpoint, endpoint_path, credentials)
576+
end
577+
566578
# Returns the number of followers
567579
#
568580
# @param [String] date (Format:yyyyMMdd, Example:20191231)

spec/line/bot/client_get_spec.rb

+99
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,52 @@
143143
}
144144
EOS
145145

146+
USER_INTERACTION_STATISTICS_CONTENT = <<EOS
147+
{
148+
"overview": {
149+
"requestId": "f70dd685-499a-4231-a441-f24b8d4fba21",
150+
"timestamp": 1568214000,
151+
"delivered": 32,
152+
"uniqueImpression": 4,
153+
"uniqueClick": null,
154+
"uniqueMediaPlayed": 2,
155+
"uniqueMediaPlayed100Percent": -1
156+
},
157+
"messages": [
158+
{
159+
"seq": 1,
160+
"impression": 18,
161+
"mediaPlayed": 11,
162+
"mediaPlayed25Percent": -1,
163+
"mediaPlayed50Percent": -1,
164+
"mediaPlayed75Percent": -1,
165+
"mediaPlayed100Percent": -1,
166+
"uniqueMediaPlayed": 2,
167+
"uniqueMediaPlayed25Percent": -1,
168+
"uniqueMediaPlayed50Percent": -1,
169+
"uniqueMediaPlayed75Percent": -1,
170+
"uniqueMediaPlayed100Percent": -1
171+
}
172+
],
173+
"clicks": [
174+
{
175+
"seq": 1,
176+
"url": "https://example.com/1",
177+
"click": -1,
178+
"uniqueClick": -1,
179+
"uniqueClickOfRequest": -1
180+
},
181+
{
182+
"seq": 1,
183+
"url": "https://example.com/2",
184+
"click": -1,
185+
"uniqueClick": -1,
186+
"uniqueClickOfRequest": -1
187+
}
188+
]
189+
}
190+
EOS
191+
146192
WebMock.allow_net_connect!
147193

148194
describe Line::Bot::Client do
@@ -354,4 +400,57 @@ def generate_client
354400
]
355401
)
356402
end
403+
404+
it 'get user interaction statistics' do
405+
uri_template = Addressable::Template.new Line::Bot::API::DEFAULT_ENDPOINT + '/bot/insight/message/event?requestId=f70dd685-499a-4231-a441-f24b8d4fba21'
406+
stub_request(:get, uri_template).to_return(body: USER_INTERACTION_STATISTICS_CONTENT, status: 200)
407+
408+
client = generate_client
409+
response = client.get_user_interaction_statistics('f70dd685-499a-4231-a441-f24b8d4fba21')
410+
411+
json = JSON.parse(response.body, symbolize_names: true)
412+
expect(json).to eq(
413+
overview: {
414+
requestId: 'f70dd685-499a-4231-a441-f24b8d4fba21',
415+
timestamp: 1568214000,
416+
delivered: 32,
417+
uniqueImpression: 4,
418+
uniqueClick: nil,
419+
uniqueMediaPlayed: 2,
420+
uniqueMediaPlayed100Percent: -1
421+
},
422+
messages: [
423+
{
424+
seq: 1,
425+
impression: 18,
426+
mediaPlayed: 11,
427+
mediaPlayed25Percent: -1,
428+
mediaPlayed50Percent: -1,
429+
mediaPlayed75Percent: -1,
430+
mediaPlayed100Percent: -1,
431+
uniqueMediaPlayed: 2,
432+
uniqueMediaPlayed25Percent: -1,
433+
uniqueMediaPlayed50Percent: -1,
434+
uniqueMediaPlayed75Percent: -1,
435+
uniqueMediaPlayed100Percent: -1
436+
}
437+
],
438+
clicks: [
439+
{
440+
seq: 1,
441+
url: 'https://example.com/1',
442+
click: -1,
443+
uniqueClick: -1,
444+
uniqueClickOfRequest: -1
445+
},
446+
{
447+
seq: 1,
448+
url: 'https://example.com/2',
449+
click: -1,
450+
uniqueClick: -1,
451+
uniqueClickOfRequest: -1
452+
}
453+
]
454+
)
455+
end
357456
end

0 commit comments

Comments
 (0)