Skip to content

Commit be2adda

Browse files
committed
Merge branch 'master' into add-tag-subscribers-method
2 parents beede07 + 058eb09 commit be2adda

File tree

2 files changed

+121
-0
lines changed

2 files changed

+121
-0
lines changed

src/ConvertKit_API_Traits.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1295,6 +1295,39 @@ public function get_broadcast_link_clicks(
12951295
);
12961296
}
12971297

1298+
/**
1299+
* List stats for a list of broadcasts.
1300+
*
1301+
* @param boolean $include_total_count To include the total count of records in the response, use true.
1302+
* @param string $after_cursor Return results after the given pagination cursor.
1303+
* @param string $before_cursor Return results before the given pagination cursor.
1304+
* @param integer $per_page Number of results to return.
1305+
*
1306+
* @since 2.2.1
1307+
*
1308+
* @see https://developers.kit.com/api-reference/broadcasts/get-stats-for-a-list-of-broadcasts
1309+
*
1310+
* @return false|mixed
1311+
*/
1312+
public function get_broadcasts_stats(
1313+
bool $include_total_count = false,
1314+
string $after_cursor = '',
1315+
string $before_cursor = '',
1316+
int $per_page = 100
1317+
) {
1318+
// Send request.
1319+
return $this->get(
1320+
'broadcasts/stats',
1321+
$this->build_total_count_and_pagination_params(
1322+
[],
1323+
$include_total_count,
1324+
$after_cursor,
1325+
$before_cursor,
1326+
$per_page
1327+
)
1328+
);
1329+
}
1330+
12981331
/**
12991332
* Updates a broadcast.
13001333
*

tests/ConvertKitAPITest.php

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4446,6 +4446,94 @@ public function testGetBroadcastLinkClicksWithInvalidBroadcastID()
44464446
$this->api->get_broadcast_link_clicks(12345);
44474447
}
44484448

4449+
/**
4450+
* Test that get_broadcasts_stats() returns the expected data.
4451+
*
4452+
* @since 2.2.1
4453+
*
4454+
* @return void
4455+
*/
4456+
public function testGetBroadcastsStats()
4457+
{
4458+
// Get broadcasts stats.
4459+
$result = $this->api->get_broadcasts_stats(
4460+
per_page: 1
4461+
);
4462+
4463+
// Assert broadcasts and pagination exist.
4464+
$this->assertDataExists($result, 'broadcasts');
4465+
$this->assertPaginationExists($result);
4466+
4467+
// Assert a single broadcast was returned.
4468+
$this->assertCount(1, $result->broadcasts);
4469+
4470+
// Store the Broadcast ID to check it's different from the next broadcast.
4471+
$id = $result->broadcasts[0]->id;
4472+
4473+
// Assert has_previous_page and has_next_page are correct.
4474+
$this->assertFalse($result->pagination->has_previous_page);
4475+
$this->assertTrue($result->pagination->has_next_page);
4476+
4477+
// Use pagination to fetch next page.
4478+
$result = $this->api->get_broadcasts_stats(
4479+
per_page: 1,
4480+
after_cursor: $result->pagination->end_cursor
4481+
);
4482+
4483+
// Assert broadcasts and pagination exist.
4484+
$this->assertDataExists($result, 'broadcasts');
4485+
$this->assertPaginationExists($result);
4486+
4487+
// Assert a single broadcast was returned.
4488+
$this->assertCount(1, $result->broadcasts);
4489+
4490+
// Assert the broadcast ID is different from the previous broadcast.
4491+
$this->assertNotEquals($id, $result->broadcasts[0]->id);
4492+
4493+
// Assert has_previous_page and has_next_page are correct.
4494+
$this->assertTrue($result->pagination->has_previous_page);
4495+
$this->assertTrue($result->pagination->has_next_page);
4496+
4497+
// Use pagination to fetch previous page.
4498+
$result = $this->api->get_broadcasts_stats(
4499+
per_page: 1,
4500+
before_cursor: $result->pagination->start_cursor
4501+
);
4502+
4503+
// Assert broadcasts and pagination exist.
4504+
$this->assertDataExists($result, 'broadcasts');
4505+
$this->assertPaginationExists($result);
4506+
4507+
// Assert a single webhook was returned.
4508+
$this->assertCount(1, $result->broadcasts);
4509+
4510+
// Assert the broadcast ID matches the first broadcast.
4511+
$this->assertEquals($id, $result->broadcasts[0]->id);
4512+
}
4513+
4514+
/**
4515+
* Test that get_broadcasts_stats() returns the expected data
4516+
* when the total count is included.
4517+
*
4518+
* @since 2.2.1
4519+
*
4520+
* @return void
4521+
*/
4522+
public function testGetBroadcastsStatsWithTotalCount()
4523+
{
4524+
$result = $this->api->get_broadcasts_stats(
4525+
include_total_count: true
4526+
);
4527+
4528+
// Assert broadcasts and pagination exist.
4529+
$this->assertDataExists($result, 'broadcasts');
4530+
$this->assertPaginationExists($result);
4531+
4532+
// Assert total count is included.
4533+
$this->assertArrayHasKey('total_count', get_object_vars($result->pagination));
4534+
$this->assertGreaterThan(0, $result->pagination->total_count);
4535+
}
4536+
44494537
/**
44504538
* Test that update_broadcast() throws a ClientException when an invalid
44514539
* broadcast ID is specified.

0 commit comments

Comments
 (0)