Skip to content

Commit f2adc1b

Browse files
authored
Merge pull request #112 from Kit/add-get-stats-for-a-list-of-broadcasts-method
Add `get_broadcasts_stats` method
2 parents e0a848e + 4e90044 commit f2adc1b

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
@@ -1258,6 +1258,39 @@ public function get_broadcast_link_clicks(
12581258
);
12591259
}
12601260

1261+
/**
1262+
* List stats for a list of broadcasts.
1263+
*
1264+
* @param boolean $include_total_count To include the total count of records in the response, use true.
1265+
* @param string $after_cursor Return results after the given pagination cursor.
1266+
* @param string $before_cursor Return results before the given pagination cursor.
1267+
* @param integer $per_page Number of results to return.
1268+
*
1269+
* @since 2.2.1
1270+
*
1271+
* @see https://developers.kit.com/api-reference/broadcasts/get-stats-for-a-list-of-broadcasts
1272+
*
1273+
* @return false|mixed
1274+
*/
1275+
public function get_broadcasts_stats(
1276+
bool $include_total_count = false,
1277+
string $after_cursor = '',
1278+
string $before_cursor = '',
1279+
int $per_page = 100
1280+
) {
1281+
// Send request.
1282+
return $this->get(
1283+
'broadcasts/stats',
1284+
$this->build_total_count_and_pagination_params(
1285+
[],
1286+
$include_total_count,
1287+
$after_cursor,
1288+
$before_cursor,
1289+
$per_page
1290+
)
1291+
);
1292+
}
1293+
12611294
/**
12621295
* Updates a broadcast.
12631296
*

tests/ConvertKitAPITest.php

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

4330+
/**
4331+
* Test that get_broadcasts_stats() returns the expected data.
4332+
*
4333+
* @since 2.2.1
4334+
*
4335+
* @return void
4336+
*/
4337+
public function testGetBroadcastsStats()
4338+
{
4339+
// Get broadcasts stats.
4340+
$result = $this->api->get_broadcasts_stats(
4341+
per_page: 1
4342+
);
4343+
4344+
// Assert broadcasts and pagination exist.
4345+
$this->assertDataExists($result, 'broadcasts');
4346+
$this->assertPaginationExists($result);
4347+
4348+
// Assert a single broadcast was returned.
4349+
$this->assertCount(1, $result->broadcasts);
4350+
4351+
// Store the Broadcast ID to check it's different from the next broadcast.
4352+
$id = $result->broadcasts[0]->id;
4353+
4354+
// Assert has_previous_page and has_next_page are correct.
4355+
$this->assertFalse($result->pagination->has_previous_page);
4356+
$this->assertTrue($result->pagination->has_next_page);
4357+
4358+
// Use pagination to fetch next page.
4359+
$result = $this->api->get_broadcasts_stats(
4360+
per_page: 1,
4361+
after_cursor: $result->pagination->end_cursor
4362+
);
4363+
4364+
// Assert broadcasts and pagination exist.
4365+
$this->assertDataExists($result, 'broadcasts');
4366+
$this->assertPaginationExists($result);
4367+
4368+
// Assert a single broadcast was returned.
4369+
$this->assertCount(1, $result->broadcasts);
4370+
4371+
// Assert the broadcast ID is different from the previous broadcast.
4372+
$this->assertNotEquals($id, $result->broadcasts[0]->id);
4373+
4374+
// Assert has_previous_page and has_next_page are correct.
4375+
$this->assertTrue($result->pagination->has_previous_page);
4376+
$this->assertTrue($result->pagination->has_next_page);
4377+
4378+
// Use pagination to fetch previous page.
4379+
$result = $this->api->get_broadcasts_stats(
4380+
per_page: 1,
4381+
before_cursor: $result->pagination->start_cursor
4382+
);
4383+
4384+
// Assert broadcasts and pagination exist.
4385+
$this->assertDataExists($result, 'broadcasts');
4386+
$this->assertPaginationExists($result);
4387+
4388+
// Assert a single webhook was returned.
4389+
$this->assertCount(1, $result->broadcasts);
4390+
4391+
// Assert the broadcast ID matches the first broadcast.
4392+
$this->assertEquals($id, $result->broadcasts[0]->id);
4393+
}
4394+
4395+
/**
4396+
* Test that get_broadcasts_stats() returns the expected data
4397+
* when the total count is included.
4398+
*
4399+
* @since 2.2.1
4400+
*
4401+
* @return void
4402+
*/
4403+
public function testGetBroadcastsStatsWithTotalCount()
4404+
{
4405+
$result = $this->api->get_broadcasts_stats(
4406+
include_total_count: true
4407+
);
4408+
4409+
// Assert broadcasts and pagination exist.
4410+
$this->assertDataExists($result, 'broadcasts');
4411+
$this->assertPaginationExists($result);
4412+
4413+
// Assert total count is included.
4414+
$this->assertArrayHasKey('total_count', get_object_vars($result->pagination));
4415+
$this->assertGreaterThan(0, $result->pagination->total_count);
4416+
}
4417+
43304418
/**
43314419
* Test that update_broadcast() throws a ClientException when an invalid
43324420
* broadcast ID is specified.

0 commit comments

Comments
 (0)