From b7ab7f962738c25d3d142264ab60dad6826e5ec3 Mon Sep 17 00:00:00 2001 From: Tim Carr Date: Tue, 30 Sep 2025 18:57:34 +0800 Subject: [PATCH 1/2] Add `get_broadcast_link_clicks` method --- src/ConvertKit_API_Traits.php | 35 +++++++++++++ tests/ConvertKitAPITest.php | 95 +++++++++++++++++++++++++++++++++++ 2 files changed, 130 insertions(+) diff --git a/src/ConvertKit_API_Traits.php b/src/ConvertKit_API_Traits.php index 71f2175..44cac1b 100644 --- a/src/ConvertKit_API_Traits.php +++ b/src/ConvertKit_API_Traits.php @@ -1196,6 +1196,41 @@ public function get_broadcast_stats(int $id) return $this->get(sprintf('broadcasts/%s/stats', $id)); } + /** + * List link clicks for a specific broadcast. + * + * @param integer $id Broadcast ID. + * @param boolean $include_total_count To include the total count of records in the response, use true. + * @param string $after_cursor Return results after the given pagination cursor. + * @param string $before_cursor Return results before the given pagination cursor. + * @param integer $per_page Number of results to return. + * + * @since 2.2.1 + * + * @see https://developers.kit.com/api-reference/broadcasts/get-link-clicks-for-a-broadcast + * + * @return false|mixed + */ + public function get_broadcast_link_clicks( + int $id, + bool $include_total_count = false, + string $after_cursor = '', + string $before_cursor = '', + int $per_page = 100 + ) { + // Send request. + return $this->get( + sprintf('broadcasts/%s/clicks', $id), + $this->build_total_count_and_pagination_params( + [], + $include_total_count, + $after_cursor, + $before_cursor, + $per_page + ) + ); + } + /** * Updates a broadcast. * diff --git a/tests/ConvertKitAPITest.php b/tests/ConvertKitAPITest.php index e05a025..e49b927 100644 --- a/tests/ConvertKitAPITest.php +++ b/tests/ConvertKitAPITest.php @@ -4190,6 +4190,101 @@ public function testGetBroadcastStatsWithInvalidBroadcastID() $this->api->get_broadcast_stats(12345); } + /** + * Test that get_broadcast_link_clicks() returns the expected data. + * + * @since 2.2.1 + * + * @return void + */ + public function testGetBroadcastLinkClicks() + { + // Get broadcast link clicks. + $result = $this->api->get_broadcast_link_clicks( + $_ENV['CONVERTKIT_API_BROADCAST_ID'], + per_page: 1 + ); + + // Assert broadcasts and pagination exist. + $this->assertDataExists($result, 'broadcast'); + $this->assertPaginationExists($result); + + // Assert a single broadcast was returned. + $this->assertCount(1, $result->broadcasts); + + // Assert has_previous_page and has_next_page are correct. + $this->assertFalse($result->pagination->has_previous_page); + $this->assertTrue($result->pagination->has_next_page); + + // Use pagination to fetch next page. + $result = $this->api->get_broadcasts_stats( + per_page: 1, + after_cursor: $result->pagination->end_cursor + ); + + // Assert broadcasts and pagination exist. + $this->assertDataExists($result, 'broadcast'); + $this->assertPaginationExists($result); + + // Assert a single broadcast was returned. + $this->assertCount(1, $result->broadcasts); + + // Assert has_previous_page and has_next_page are correct. + $this->assertTrue($result->pagination->has_previous_page); + $this->assertTrue($result->pagination->has_next_page); + + // Use pagination to fetch previous page. + $result = $this->api->get_broadcasts_stats( + per_page: 1, + before_cursor: $result->pagination->start_cursor + ); + + // Assert broadcasts and pagination exist. + $this->assertDataExists($result, 'broadcasts'); + $this->assertPaginationExists($result); + + // Assert a single webhook was returned. + $this->assertCount(1, $result->broadcasts); + } + + /** + * Test that get_broadcast_link_clicks() returns the expected data + * when the total count is included. + * + * @since 2.2.1 + * + * @return void + */ + public function testGetBroadcastLinkClicksWithTotalCount() + { + $result = $this->api->get_broadcast_link_clicks( + $_ENV['CONVERTKIT_API_BROADCAST_ID'], + include_total_count: true + ); + + // Assert broadcasts and pagination exist. + $this->assertDataExists($result, 'broadcast'); + $this->assertPaginationExists($result); + + // Assert total count is included. + $this->assertArrayHasKey('total_count', get_object_vars($result->pagination)); + $this->assertGreaterThan(0, $result->pagination->total_count); + } + + /** + * Test that get_broadcast_link_clicks() throws a ClientException when an invalid + * broadcast ID is specified. + * + * @since 2.2.1 + * + * @return void + */ + public function testGetBroadcastLinkClicksWithInvalidBroadcastID() + { + $this->expectException(ClientException::class); + $this->api->get_broadcast_link_clicks(12345); + } + /** * Test that update_broadcast() throws a ClientException when an invalid * broadcast ID is specified. From 4f088b7001b084f4d09ef53fb0b20330abb790ce Mon Sep 17 00:00:00 2001 From: Tim Carr Date: Tue, 30 Sep 2025 21:13:48 +0800 Subject: [PATCH 2/2] Remove `include_total_count`, as not supported --- src/ConvertKit_API_Traits.php | 18 ++++------ tests/ConvertKitAPITest.php | 64 +++-------------------------------- 2 files changed, 12 insertions(+), 70 deletions(-) diff --git a/src/ConvertKit_API_Traits.php b/src/ConvertKit_API_Traits.php index 44cac1b..381746f 100644 --- a/src/ConvertKit_API_Traits.php +++ b/src/ConvertKit_API_Traits.php @@ -1199,11 +1199,10 @@ public function get_broadcast_stats(int $id) /** * List link clicks for a specific broadcast. * - * @param integer $id Broadcast ID. - * @param boolean $include_total_count To include the total count of records in the response, use true. - * @param string $after_cursor Return results after the given pagination cursor. - * @param string $before_cursor Return results before the given pagination cursor. - * @param integer $per_page Number of results to return. + * @param integer $id Broadcast ID. + * @param string $after_cursor Return results after the given pagination cursor. + * @param string $before_cursor Return results before the given pagination cursor. + * @param integer $per_page Number of results to return. * * @since 2.2.1 * @@ -1213,7 +1212,6 @@ public function get_broadcast_stats(int $id) */ public function get_broadcast_link_clicks( int $id, - bool $include_total_count = false, string $after_cursor = '', string $before_cursor = '', int $per_page = 100 @@ -1222,11 +1220,9 @@ public function get_broadcast_link_clicks( return $this->get( sprintf('broadcasts/%s/clicks', $id), $this->build_total_count_and_pagination_params( - [], - $include_total_count, - $after_cursor, - $before_cursor, - $per_page + after_cursor: $after_cursor, + before_cursor: $before_cursor, + per_page: $per_page ) ); } diff --git a/tests/ConvertKitAPITest.php b/tests/ConvertKitAPITest.php index e49b927..cb83d34 100644 --- a/tests/ConvertKitAPITest.php +++ b/tests/ConvertKitAPITest.php @@ -4205,70 +4205,16 @@ public function testGetBroadcastLinkClicks() per_page: 1 ); - // Assert broadcasts and pagination exist. - $this->assertDataExists($result, 'broadcast'); + // Assert clicks and pagination exist. + $this->assertDataExists($result->broadcast, 'clicks'); $this->assertPaginationExists($result); - // Assert a single broadcast was returned. - $this->assertCount(1, $result->broadcasts); + // Assert a single click was returned. + $this->assertCount(1, $result->broadcast->clicks); // Assert has_previous_page and has_next_page are correct. $this->assertFalse($result->pagination->has_previous_page); - $this->assertTrue($result->pagination->has_next_page); - - // Use pagination to fetch next page. - $result = $this->api->get_broadcasts_stats( - per_page: 1, - after_cursor: $result->pagination->end_cursor - ); - - // Assert broadcasts and pagination exist. - $this->assertDataExists($result, 'broadcast'); - $this->assertPaginationExists($result); - - // Assert a single broadcast was returned. - $this->assertCount(1, $result->broadcasts); - - // Assert has_previous_page and has_next_page are correct. - $this->assertTrue($result->pagination->has_previous_page); - $this->assertTrue($result->pagination->has_next_page); - - // Use pagination to fetch previous page. - $result = $this->api->get_broadcasts_stats( - per_page: 1, - before_cursor: $result->pagination->start_cursor - ); - - // Assert broadcasts and pagination exist. - $this->assertDataExists($result, 'broadcasts'); - $this->assertPaginationExists($result); - - // Assert a single webhook was returned. - $this->assertCount(1, $result->broadcasts); - } - - /** - * Test that get_broadcast_link_clicks() returns the expected data - * when the total count is included. - * - * @since 2.2.1 - * - * @return void - */ - public function testGetBroadcastLinkClicksWithTotalCount() - { - $result = $this->api->get_broadcast_link_clicks( - $_ENV['CONVERTKIT_API_BROADCAST_ID'], - include_total_count: true - ); - - // Assert broadcasts and pagination exist. - $this->assertDataExists($result, 'broadcast'); - $this->assertPaginationExists($result); - - // Assert total count is included. - $this->assertArrayHasKey('total_count', get_object_vars($result->pagination)); - $this->assertGreaterThan(0, $result->pagination->total_count); + $this->assertFalse($result->pagination->has_next_page); } /**