Skip to content

Commit 964bced

Browse files
committed
Add get_broadcasts_stats method
1 parent bf2b64e commit 964bced

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed

src/ConvertKit_API_Traits.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1196,6 +1196,39 @@ public function get_broadcast_stats(int $id)
11961196
return $this->get(sprintf('broadcasts/%s/stats', $id));
11971197
}
11981198

1199+
/**
1200+
* List stats for a list of broadcasts.
1201+
*
1202+
* @param boolean $include_total_count To include the total count of records in the response, use true.
1203+
* @param string $after_cursor Return results after the given pagination cursor.
1204+
* @param string $before_cursor Return results before the given pagination cursor.
1205+
* @param integer $per_page Number of results to return.
1206+
*
1207+
* @since 2.2.1
1208+
*
1209+
* @see https://developers.kit.com/api-reference/broadcasts/get-stats-for-a-list-of-broadcasts
1210+
*
1211+
* @return false|mixed
1212+
*/
1213+
public function get_broadcasts_stats(
1214+
bool $include_total_count = false,
1215+
string $after_cursor = '',
1216+
string $before_cursor = '',
1217+
int $per_page = 100
1218+
) {
1219+
// Send request.
1220+
return $this->get(
1221+
'broadcasts/stats',
1222+
$this->build_total_count_and_pagination_params(
1223+
[],
1224+
$include_total_count,
1225+
$after_cursor,
1226+
$before_cursor,
1227+
$per_page
1228+
)
1229+
);
1230+
}
1231+
11991232
/**
12001233
* Updates a broadcast.
12011234
*

tests/ConvertKitAPITest.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4190,6 +4190,71 @@ public function testGetBroadcastStatsWithInvalidBroadcastID()
41904190
$this->api->get_broadcast_stats(12345);
41914191
}
41924192

4193+
/**
4194+
* Test that get_broadcasts_stats() returns the expected data.
4195+
*
4196+
* @since 2.2.1
4197+
*
4198+
* @return void
4199+
*/
4200+
public function testGetBroadcastsStats()
4201+
{
4202+
// Get broadcasts stats.
4203+
$result = $this->api->get_broadcasts_stats(
4204+
per_page: 1
4205+
);
4206+
4207+
// Assert broadcasts and pagination exist.
4208+
$this->assertDataExists($result, 'broadcasts');
4209+
$this->assertPaginationExists($result);
4210+
4211+
// Assert a single broadcast was returned.
4212+
$this->assertCount(1, $result->broadcasts);
4213+
4214+
// Store the Broadcast ID to check it's different from the next broadcast.
4215+
$id = $result->broadcasts[0]->id;
4216+
4217+
// Assert has_previous_page and has_next_page are correct.
4218+
$this->assertFalse($result->pagination->has_previous_page);
4219+
$this->assertTrue($result->pagination->has_next_page);
4220+
4221+
// Use pagination to fetch next page.
4222+
$result = $this->api->get_broadcasts_stats(
4223+
per_page: 1,
4224+
after_cursor: $result->pagination->end_cursor
4225+
);
4226+
4227+
// Assert broadcasts and pagination exist.
4228+
$this->assertDataExists($result, 'broadcasts');
4229+
$this->assertPaginationExists($result);
4230+
4231+
// Assert a single broadcast was returned.
4232+
$this->assertCount(1, $result->broadcasts);
4233+
4234+
// Assert the broadcast ID is different from the previous broadcast.
4235+
$this->assertNotEquals($id, $result->broadcasts[0]->id);
4236+
4237+
// Assert has_previous_page and has_next_page are correct.
4238+
$this->assertTrue($result->pagination->has_previous_page);
4239+
$this->assertTrue($result->pagination->has_next_page);
4240+
4241+
// Use pagination to fetch previous page.
4242+
$result = $this->api->get_broadcasts_stats(
4243+
per_page: 1,
4244+
before_cursor: $result->pagination->start_cursor
4245+
);
4246+
4247+
// Assert broadcasts and pagination exist.
4248+
$this->assertDataExists($result, 'broadcasts');
4249+
$this->assertPaginationExists($result);
4250+
4251+
// Assert a single webhook was returned.
4252+
$this->assertCount(1, $result->broadcasts);
4253+
4254+
// Assert the broadcast ID matches the first broadcast.
4255+
$this->assertEquals($id, $result->broadcasts[0]->id);
4256+
}
4257+
41934258
/**
41944259
* Test that update_broadcast() throws a ClientException when an invalid
41954260
* broadcast ID is specified.

0 commit comments

Comments
 (0)