Skip to content

Commit

Permalink
Merge pull request #37 from bufferapp/fix/ig-story-metrics
Browse files Browse the repository at this point in the history
Add method for IG insights navigation metric [chan-361]
  • Loading branch information
dinostheo authored Jan 9, 2024
2 parents 94dfd49 + 8d88854 commit de624eb
Show file tree
Hide file tree
Showing 2 changed files with 153 additions and 0 deletions.
27 changes: 27 additions & 0 deletions Facebook/Facebook.php
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,33 @@ public function getInstagramStoryInsights($storyId, $metrics)
return $insights;
}

public function getInstagramStoryNavigationInsights($storyId)
{
$response = $this->sendRequest("GET", "/{$storyId}/insights", [
"metric" => "navigation",
"breakdown" => "story_navigation_action_type",
]);

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

$data = [];

if (!empty($response)) {
$decodedBody = $response->getDecodedBody();

if (!empty($decodedBody) && is_array($decodedBody)) {
$responseData = $decodedBody["data"];
if (!empty($responseData) && isset($responseData[0]["total_value"])) {
$data = $responseData[0]["total_value"]["breakdowns"][0];
}
}
}

return $data;
}

public function getMediaComment($commentId, $fields)
{

Expand Down
126 changes: 126 additions & 0 deletions tests/FacebookTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1409,6 +1409,132 @@ public function testGetInstagramStoryInsightsNullIfError()
$this->assertEquals($response, null);
}

public function testGetInstagramStoryNavigationInsightsNullIfError()
{
$mediaId = '123456789';

$params = [
'metric' => 'navigation',
'breakdown' => 'story_navigation_action_type',
];

$facebook = new Facebook();

$responseMock = m::mock('\Facebook\FacebookResponse')
->shouldReceive('isError')
->once()
->andReturn(true)
->getMock();
$facebookMock = m::mock('\Facebook\Facebook');
$facebookMock
->shouldReceive('sendRequest')
->once()
->with('GET', "/${mediaId}/insights", $params)
->andReturn($responseMock);
$facebook->setFacebookLibrary($facebookMock);

$response = $facebook->getInstagramStoryNavigationInsights($mediaId);

$this->assertEquals($response, null);
}

public function testGetInstagramStoryNavigationInsightsReturnsData()
{
$mediaId = '123456789';

$params = [
'metric' => 'navigation',
'breakdown' => 'story_navigation_action_type',
];

$facebook = new Facebook();

$decodedNavigationData = [
'data' => [
0 => [
'name' => 'navigation',
'period' => 'lifetime',
'total_value' => [
'breakdowns' => [
0 => [
'dimension_keys' => ['story_navigation_action_type'],
'results' => [
0 => [
'dimension_values' => ['swipe_forward'],
'value' => 2
],
1 => [
'dimension_values' => ['tap_exit'],
'value' => 20
],
2 => [
'dimension_values' => ['tap_back'],
'value' => 4
],
3 => [
'dimension_values' => ['tap_forward'],
'value' => 75
],
]
]
],
],
],
]
];

$responseMock = m::mock('\Facebook\FacebookResponse')
->shouldReceive('isError')
->andReturn(false)
->shouldReceive('getDecodedBody')
->andReturn($decodedNavigationData)
->getMock();
$facebookMock = m::mock('\Facebook\Facebook');
$facebookMock
->shouldReceive('sendRequest')
->once()
->with('GET', "/${mediaId}/insights", $params)
->andReturn($responseMock);
$facebook->setFacebookLibrary($facebookMock);

$response = $facebook->getInstagramStoryNavigationInsights($mediaId);

$this->assertEquals($response['results'][0]['dimension_values'][0], 'swipe_forward');
$this->assertEquals($response['results'][0]['value'], 2);
}

public function testGetInstagramStoryNavigationInsightsEmptyResponse()
{
$mediaId = '123456789';
$params = [
'metric' => 'navigation',
'breakdown' => 'story_navigation_action_type',
];

$facebook = new Facebook();
$decodedNavigationData = [
'data' => []
];

$responseMock = m::mock('\Facebook\FacebookResponse')
->shouldReceive('isError')
->andReturn(false)
->shouldReceive('getDecodedBody')
->andReturn($decodedNavigationData)
->getMock();
$facebookMock = m::mock('\Facebook\Facebook');
$facebookMock
->shouldReceive('sendRequest')
->once()
->with('GET', "/${mediaId}/insights", $params)
->andReturn($responseMock);
$facebook->setFacebookLibrary($facebookMock);

$response = $facebook->getInstagramStoryNavigationInsights($mediaId);

$this->assertEquals([], $response);
}

public function testGetAccountsWorksAsExpected()
{
$me = [
Expand Down

0 comments on commit de624eb

Please sign in to comment.