Skip to content

Commit de624eb

Browse files
authored
Merge pull request #37 from bufferapp/fix/ig-story-metrics
Add method for IG insights navigation metric [chan-361]
2 parents 94dfd49 + 8d88854 commit de624eb

File tree

2 files changed

+153
-0
lines changed

2 files changed

+153
-0
lines changed

Diff for: Facebook/Facebook.php

+27
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,33 @@ public function getInstagramStoryInsights($storyId, $metrics)
369369
return $insights;
370370
}
371371

372+
public function getInstagramStoryNavigationInsights($storyId)
373+
{
374+
$response = $this->sendRequest("GET", "/{$storyId}/insights", [
375+
"metric" => "navigation",
376+
"breakdown" => "story_navigation_action_type",
377+
]);
378+
379+
if ($response->isError()) {
380+
return null;
381+
}
382+
383+
$data = [];
384+
385+
if (!empty($response)) {
386+
$decodedBody = $response->getDecodedBody();
387+
388+
if (!empty($decodedBody) && is_array($decodedBody)) {
389+
$responseData = $decodedBody["data"];
390+
if (!empty($responseData) && isset($responseData[0]["total_value"])) {
391+
$data = $responseData[0]["total_value"]["breakdowns"][0];
392+
}
393+
}
394+
}
395+
396+
return $data;
397+
}
398+
372399
public function getMediaComment($commentId, $fields)
373400
{
374401

Diff for: tests/FacebookTest.php

+126
Original file line numberDiff line numberDiff line change
@@ -1409,6 +1409,132 @@ public function testGetInstagramStoryInsightsNullIfError()
14091409
$this->assertEquals($response, null);
14101410
}
14111411

1412+
public function testGetInstagramStoryNavigationInsightsNullIfError()
1413+
{
1414+
$mediaId = '123456789';
1415+
1416+
$params = [
1417+
'metric' => 'navigation',
1418+
'breakdown' => 'story_navigation_action_type',
1419+
];
1420+
1421+
$facebook = new Facebook();
1422+
1423+
$responseMock = m::mock('\Facebook\FacebookResponse')
1424+
->shouldReceive('isError')
1425+
->once()
1426+
->andReturn(true)
1427+
->getMock();
1428+
$facebookMock = m::mock('\Facebook\Facebook');
1429+
$facebookMock
1430+
->shouldReceive('sendRequest')
1431+
->once()
1432+
->with('GET', "/${mediaId}/insights", $params)
1433+
->andReturn($responseMock);
1434+
$facebook->setFacebookLibrary($facebookMock);
1435+
1436+
$response = $facebook->getInstagramStoryNavigationInsights($mediaId);
1437+
1438+
$this->assertEquals($response, null);
1439+
}
1440+
1441+
public function testGetInstagramStoryNavigationInsightsReturnsData()
1442+
{
1443+
$mediaId = '123456789';
1444+
1445+
$params = [
1446+
'metric' => 'navigation',
1447+
'breakdown' => 'story_navigation_action_type',
1448+
];
1449+
1450+
$facebook = new Facebook();
1451+
1452+
$decodedNavigationData = [
1453+
'data' => [
1454+
0 => [
1455+
'name' => 'navigation',
1456+
'period' => 'lifetime',
1457+
'total_value' => [
1458+
'breakdowns' => [
1459+
0 => [
1460+
'dimension_keys' => ['story_navigation_action_type'],
1461+
'results' => [
1462+
0 => [
1463+
'dimension_values' => ['swipe_forward'],
1464+
'value' => 2
1465+
],
1466+
1 => [
1467+
'dimension_values' => ['tap_exit'],
1468+
'value' => 20
1469+
],
1470+
2 => [
1471+
'dimension_values' => ['tap_back'],
1472+
'value' => 4
1473+
],
1474+
3 => [
1475+
'dimension_values' => ['tap_forward'],
1476+
'value' => 75
1477+
],
1478+
]
1479+
]
1480+
],
1481+
],
1482+
],
1483+
]
1484+
];
1485+
1486+
$responseMock = m::mock('\Facebook\FacebookResponse')
1487+
->shouldReceive('isError')
1488+
->andReturn(false)
1489+
->shouldReceive('getDecodedBody')
1490+
->andReturn($decodedNavigationData)
1491+
->getMock();
1492+
$facebookMock = m::mock('\Facebook\Facebook');
1493+
$facebookMock
1494+
->shouldReceive('sendRequest')
1495+
->once()
1496+
->with('GET', "/${mediaId}/insights", $params)
1497+
->andReturn($responseMock);
1498+
$facebook->setFacebookLibrary($facebookMock);
1499+
1500+
$response = $facebook->getInstagramStoryNavigationInsights($mediaId);
1501+
1502+
$this->assertEquals($response['results'][0]['dimension_values'][0], 'swipe_forward');
1503+
$this->assertEquals($response['results'][0]['value'], 2);
1504+
}
1505+
1506+
public function testGetInstagramStoryNavigationInsightsEmptyResponse()
1507+
{
1508+
$mediaId = '123456789';
1509+
$params = [
1510+
'metric' => 'navigation',
1511+
'breakdown' => 'story_navigation_action_type',
1512+
];
1513+
1514+
$facebook = new Facebook();
1515+
$decodedNavigationData = [
1516+
'data' => []
1517+
];
1518+
1519+
$responseMock = m::mock('\Facebook\FacebookResponse')
1520+
->shouldReceive('isError')
1521+
->andReturn(false)
1522+
->shouldReceive('getDecodedBody')
1523+
->andReturn($decodedNavigationData)
1524+
->getMock();
1525+
$facebookMock = m::mock('\Facebook\Facebook');
1526+
$facebookMock
1527+
->shouldReceive('sendRequest')
1528+
->once()
1529+
->with('GET', "/${mediaId}/insights", $params)
1530+
->andReturn($responseMock);
1531+
$facebook->setFacebookLibrary($facebookMock);
1532+
1533+
$response = $facebook->getInstagramStoryNavigationInsights($mediaId);
1534+
1535+
$this->assertEquals([], $response);
1536+
}
1537+
14121538
public function testGetAccountsWorksAsExpected()
14131539
{
14141540
$me = [

0 commit comments

Comments
 (0)