Skip to content

Commit d5158bc

Browse files
authored
Merge pull request #38 from bufferapp/fix/support-ig-story-views-metric
fix: support metric_type for InstagramStoryInsights
2 parents de624eb + 493eae8 commit d5158bc

File tree

2 files changed

+69
-19
lines changed

2 files changed

+69
-19
lines changed

Facebook/Facebook.php

+9-4
Original file line numberDiff line numberDiff line change
@@ -353,13 +353,18 @@ public function getInstagramUserStories($userId, $fields)
353353
return $body["data"];
354354
}
355355

356-
public function getInstagramStoryInsights($storyId, $metrics)
356+
public function getInstagramStoryInsights($storyId, $metrics, $metric_type = null)
357357
{
358358
$formattedMetrics = join(",", $metrics);
359-
360-
$response = $this->sendRequest("GET", "/{$storyId}/insights", [
359+
$params = [
361360
"metric" => $formattedMetrics,
362-
]);
361+
];
362+
363+
if (!is_null($metric_type)) {
364+
$params["metric_type"] = $metric_type;
365+
}
366+
367+
$response = $this->sendRequest("GET", "/{$storyId}/insights", $params);
363368

364369
if ($response->isError()) {
365370
return null;

tests/FacebookTest.php

+60-15
Original file line numberDiff line numberDiff line change
@@ -1357,19 +1357,14 @@ public function testGetInstagramStoryInsights()
13571357

13581358
$facebook = new Facebook();
13591359

1360-
$responseMock = m::mock('\Facebook\FacebookResponse')
1361-
->shouldReceive('isError')
1362-
->once()
1363-
->andReturn(false)
1364-
->shouldReceive('getDecodedBody')
1365-
->once()
1366-
->andReturn($decodedInsightsResponseData)
1367-
->getMock();
1360+
$responseMock = m::mock('\Facebook\FacebookResponse');
1361+
$responseMock->shouldReceive('isError')->once()->andReturn(false);
1362+
$responseMock->shouldReceive('getDecodedBody')->once()->andReturn($decodedInsightsResponseData);
1363+
13681364
$facebookMock = m::mock('\Facebook\Facebook');
1369-
$facebookMock
1370-
->shouldReceive('sendRequest')
1365+
$facebookMock->shouldReceive('sendRequest')
13711366
->once()
1372-
->with('GET', "/${mediaId}/insights", ["metric" => $metrics])
1367+
->with('GET', '/' . $mediaId . '/insights', ["metric" => $metrics])
13731368
->andReturn($responseMock);
13741369
$facebook->setFacebookLibrary($facebookMock);
13751370

@@ -1400,7 +1395,7 @@ public function testGetInstagramStoryInsightsNullIfError()
14001395
$facebookMock
14011396
->shouldReceive('sendRequest')
14021397
->once()
1403-
->with('GET', "/${mediaId}/insights", ["metric" => $metrics])
1398+
->with('GET', '/' . $mediaId . '/insights', ["metric" => $metrics])
14041399
->andReturn($responseMock);
14051400
$facebook->setFacebookLibrary($facebookMock);
14061401

@@ -1429,7 +1424,7 @@ public function testGetInstagramStoryNavigationInsightsNullIfError()
14291424
$facebookMock
14301425
->shouldReceive('sendRequest')
14311426
->once()
1432-
->with('GET', "/${mediaId}/insights", $params)
1427+
->with('GET', '/' . $mediaId . '/insights', $params)
14331428
->andReturn($responseMock);
14341429
$facebook->setFacebookLibrary($facebookMock);
14351430

@@ -1493,7 +1488,7 @@ public function testGetInstagramStoryNavigationInsightsReturnsData()
14931488
$facebookMock
14941489
->shouldReceive('sendRequest')
14951490
->once()
1496-
->with('GET', "/${mediaId}/insights", $params)
1491+
->with('GET', '/' . $mediaId . '/insights', $params)
14971492
->andReturn($responseMock);
14981493
$facebook->setFacebookLibrary($facebookMock);
14991494

@@ -1526,7 +1521,7 @@ public function testGetInstagramStoryNavigationInsightsEmptyResponse()
15261521
$facebookMock
15271522
->shouldReceive('sendRequest')
15281523
->once()
1529-
->with('GET', "/${mediaId}/insights", $params)
1524+
->with('GET', '/' . $mediaId . '/insights', $params)
15301525
->andReturn($responseMock);
15311526
$facebook->setFacebookLibrary($facebookMock);
15321527

@@ -1664,4 +1659,54 @@ public function testGetTokenScopes()
16641659

16651660
$this->assertEquals(['email', 'public_profile'], $response['data']['scopes']);
16661661
}
1662+
1663+
public function testGetInstagramStoryInsightsWithMetricType()
1664+
{
1665+
$mediaId = "123456789";
1666+
$decodedInsightsResponseData = [
1667+
'data' => [
1668+
[
1669+
'name' => 'reach',
1670+
'period' => 'lifetime',
1671+
'values' => [
1672+
[
1673+
'value' => 123,
1674+
],
1675+
],
1676+
],
1677+
[
1678+
'name' => 'views',
1679+
'period' => 'lifetime',
1680+
'values' => [
1681+
[
1682+
'value' => 500,
1683+
],
1684+
],
1685+
],
1686+
]
1687+
];
1688+
1689+
$metricsArray = ['reach', 'views'];
1690+
$metrics = join(",", $metricsArray);
1691+
1692+
$facebook = new Facebook();
1693+
1694+
$responseMock = m::mock('\Facebook\FacebookResponse');
1695+
$responseMock->shouldReceive('isError')->once()->andReturn(false);
1696+
$responseMock->shouldReceive('getDecodedBody')->once()->andReturn($decodedInsightsResponseData);
1697+
1698+
$facebookMock = m::mock('\Facebook\Facebook');
1699+
$facebookMock->shouldReceive('sendRequest')
1700+
->once()
1701+
->with('GET', '/' . $mediaId . '/insights', ["metric" => $metrics, "metric_type" => "total_values"])
1702+
->andReturn($responseMock);
1703+
$facebook->setFacebookLibrary($facebookMock);
1704+
1705+
$response = $facebook->getInstagramStoryInsights($mediaId, $metricsArray, "total_values");
1706+
1707+
$this->assertEquals($response, [
1708+
'reach' => 123,
1709+
'views' => 500
1710+
]);
1711+
}
16671712
}

0 commit comments

Comments
 (0)