Skip to content

Commit 2c2cdd1

Browse files
authored
Merge pull request #139 from Kit/add-broadcasts-sent-after-before-query-params
Broadcasts Stats: Add `sent_after` and `sent_before` params
2 parents 6aaff87 + 95d3942 commit 2c2cdd1

2 files changed

Lines changed: 148 additions & 9 deletions

File tree

src/ConvertKit_API_Traits.php

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1212,6 +1212,7 @@ public function remove_tag_from_subscriber_by_email(int $tag_id, string $email_a
12121212
* @param \DateTime|null $created_before Filter subscribers who have been created before this date.
12131213
* @param \DateTime|null $tagged_after Filter subscribers who have been tagged after this date.
12141214
* @param \DateTime|null $tagged_before Filter subscribers who have been tagged before this date.
1215+
* @param boolean $slim When true, omits expensive optional fields from the response.
12151216
* @param boolean $include_total_count To include the total count of records in the response, use true.
12161217
* @param string $after_cursor Return results after the given pagination cursor.
12171218
* @param string $before_cursor Return results before the given pagination cursor.
@@ -1228,13 +1229,14 @@ public function get_tag_subscriptions(
12281229
\DateTime|null $created_before = null,
12291230
\DateTime|null $tagged_after = null,
12301231
\DateTime|null $tagged_before = null,
1232+
bool $slim = false,
12311233
bool $include_total_count = false,
12321234
string $after_cursor = '',
12331235
string $before_cursor = '',
12341236
int $per_page = 100
12351237
) {
12361238
// Build parameters.
1237-
$options = [];
1239+
$options = ['slim' => $slim];
12381240

12391241
if (!empty($subscriber_state)) {
12401242
$options['status'] = $subscriber_state;
@@ -1750,6 +1752,7 @@ public function get_subscriber_tags(
17501752
*
17511753
* @param \DateTime|null $sent_after Get broadcasts sent after the given date.
17521754
* @param \DateTime|null $sent_before Get broadcasts sent before the given date.
1755+
* @param boolean $slim When true, omits expensive optional fields from the response.
17531756
* @param boolean $include_total_count To include the total count of records in the response, use true.
17541757
* @param string $after_cursor Return results after the given pagination cursor.
17551758
* @param string $before_cursor Return results before the given pagination cursor.
@@ -1762,13 +1765,14 @@ public function get_subscriber_tags(
17621765
public function get_broadcasts(
17631766
\DateTime|null $sent_after = null,
17641767
\DateTime|null $sent_before = null,
1768+
bool $slim = false,
17651769
bool $include_total_count = false,
17661770
string $after_cursor = '',
17671771
string $before_cursor = '',
17681772
int $per_page = 100
17691773
) {
17701774
// Build parameters.
1771-
$options = [];
1775+
$options = ['slim' => $slim];
17721776

17731777
if (!is_null($sent_after)) {
17741778
$options['sent_after'] = $sent_after->format('Y-m-d');
@@ -1929,10 +1933,12 @@ public function get_broadcast_link_clicks(
19291933
/**
19301934
* List stats for a list of broadcasts.
19311935
*
1932-
* @param boolean $include_total_count To include the total count of records in the response, use true.
1933-
* @param string $after_cursor Return results after the given pagination cursor.
1934-
* @param string $before_cursor Return results before the given pagination cursor.
1935-
* @param integer $per_page Number of results to return.
1936+
* @param \DateTime|null $sent_after Get broadcasts sent after the given date.
1937+
* @param \DateTime|null $sent_before Get broadcasts sent before the given date.
1938+
* @param boolean $include_total_count To include the total count of records in the response, use true.
1939+
* @param string $after_cursor Return results after the given pagination cursor.
1940+
* @param string $before_cursor Return results before the given pagination cursor.
1941+
* @param integer $per_page Number of results to return.
19361942
*
19371943
* @since 2.2.1
19381944
*
@@ -1941,16 +1947,28 @@ public function get_broadcast_link_clicks(
19411947
* @return false|mixed
19421948
*/
19431949
public function get_broadcasts_stats(
1950+
\DateTime|null $sent_after = null,
1951+
\DateTime|null $sent_before = null,
19441952
bool $include_total_count = false,
19451953
string $after_cursor = '',
19461954
string $before_cursor = '',
19471955
int $per_page = 100
19481956
) {
1957+
// Build parameters.
1958+
$options = [];
1959+
1960+
if (!is_null($sent_after)) {
1961+
$options['sent_after'] = $sent_after->format('Y-m-d');
1962+
}
1963+
if (!is_null($sent_before)) {
1964+
$options['sent_before'] = $sent_before->format('Y-m-d');
1965+
}
1966+
19491967
// Send request.
19501968
return $this->get(
1951-
'broadcasts/stats',
1969+
'broadcasts',
19521970
$this->build_total_count_and_pagination_params(
1953-
[],
1971+
$options,
19541972
$include_total_count,
19551973
$after_cursor,
19561974
$before_cursor,
@@ -1959,7 +1977,6 @@ public function get_broadcasts_stats(
19591977
);
19601978
}
19611979

1962-
19631980
/**
19641981
* Update a broadcast.
19651982
*

tests/ConvertKitAPITest.php

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2959,6 +2959,30 @@ public function testGetTagSubscriptions()
29592959
$this->assertPaginationExists($result);
29602960
}
29612961

2962+
/**
2963+
* Test that get_tag_subscriptions() returns the expected data
2964+
* when the slim parameter is specified.
2965+
*
2966+
* @since 2.5
2967+
*
2968+
* @return void
2969+
*/
2970+
public function testGetTagSubscriptionsSlim()
2971+
{
2972+
$result = $this->api->get_tag_subscriptions(
2973+
tag_id: (int) $_ENV['CONVERTKIT_API_TAG_ID'],
2974+
slim: true
2975+
);
2976+
2977+
// Assert subscribers and pagination exist.
2978+
$this->assertDataExists($result, 'subscribers');
2979+
$this->assertPaginationExists($result);
2980+
2981+
// Confirm custom field values are excluded from the data.
2982+
$broadcast = get_object_vars($result->subscribers[0]);
2983+
$this->assertArrayNotHasKey('fields', $broadcast);
2984+
}
2985+
29622986
/**
29632987
* Test that get_tag_subscriptions() returns the expected data
29642988
* when the total count is included.
@@ -5292,6 +5316,33 @@ public function testGetBroadcastsWithSentBefore()
52925316
$this->assertCount(12, $result->broadcasts);
52935317
}
52945318

5319+
/**
5320+
* Test that get_broadcasts() returns the expected data
5321+
* when the slim parameter is specified.
5322+
*
5323+
* @since 2.5
5324+
*
5325+
* @return void
5326+
*/
5327+
public function testGetBroadcastsSlim()
5328+
{
5329+
$result = $this->api->get_broadcasts(
5330+
slim: true
5331+
);
5332+
5333+
// Assert broadcasts and pagination exist.
5334+
$this->assertDataExists($result, 'broadcasts');
5335+
$this->assertPaginationExists($result);
5336+
5337+
// Confirm content, public_url, email_address, email_template and subscriber_filter are excluded from the data.
5338+
$broadcast = get_object_vars($result->broadcasts[0]);
5339+
$this->assertArrayNotHasKey('content', $broadcast);
5340+
$this->assertArrayNotHasKey('public_url', $broadcast);
5341+
$this->assertArrayNotHasKey('email_address', $broadcast);
5342+
$this->assertArrayNotHasKey('email_template', $broadcast);
5343+
$this->assertArrayNotHasKey('subscriber_filter', $broadcast);
5344+
}
5345+
52955346
/**
52965347
* Test that get_broadcasts() returns the expected data
52975348
* when pagination parameters and per_page limits are specified.
@@ -5618,6 +5669,77 @@ public function testGetBroadcastsStats()
56185669
$this->assertEquals($id, $result->broadcasts[0]->id);
56195670
}
56205671

5672+
/**
5673+
* Test that get_broadcasts_stats() returns the expected data
5674+
* when a valid sent_after date is specified.
5675+
*
5676+
* @since 2.5
5677+
*
5678+
* @return void
5679+
*/
5680+
public function testGetBroadcastsStatsWithSentAfter()
5681+
{
5682+
$date = new DateTime('now');
5683+
$date->modify('-4 years');
5684+
$result = $this->api->get_broadcasts_stats(
5685+
sent_after: $date,
5686+
);
5687+
5688+
// Assert broadcasts and pagination exist.
5689+
$this->assertDataExists($result, 'broadcasts');
5690+
$this->assertPaginationExists($result);
5691+
5692+
// Assert the expected number of broadcasts were returned.
5693+
$this->assertCount(8, $result->broadcasts);
5694+
}
5695+
5696+
/**
5697+
* Test that get_broadcasts_stats() returns no broadcasts
5698+
* when a sent_after date is specified that is after all broadcasts.
5699+
*
5700+
* @since 2.5
5701+
*
5702+
* @return void
5703+
*/
5704+
public function testGetBroadcastsStatsWithSentAfterNow()
5705+
{
5706+
$date = new DateTime('now');
5707+
$date->modify('-1 day');
5708+
$result = $this->api->get_broadcasts_stats(
5709+
sent_after: $date,
5710+
);
5711+
5712+
// Assert broadcasts and pagination exist.
5713+
$this->assertDataExists($result, 'broadcasts');
5714+
$this->assertPaginationExists($result);
5715+
5716+
// Assert no broadcasts were returned.
5717+
$this->assertCount(0, $result->broadcasts);
5718+
}
5719+
5720+
/**
5721+
* Test that get_broadcasts_stats() returns the expected data
5722+
* when a valid sent_before date is specified.
5723+
*
5724+
* @since 2.5
5725+
*
5726+
* @return void
5727+
*/
5728+
public function testGetBroadcastsStatsWithSentBefore()
5729+
{
5730+
$date = new DateTime('now');
5731+
$result = $this->api->get_broadcasts_stats(
5732+
sent_before: new DateTime('now'),
5733+
);
5734+
5735+
// Assert broadcasts and pagination exist.
5736+
$this->assertDataExists($result, 'broadcasts');
5737+
$this->assertPaginationExists($result);
5738+
5739+
// Assert the expected number of broadcasts were returned.
5740+
$this->assertCount(12, $result->broadcasts);
5741+
}
5742+
56215743
/**
56225744
* Test that get_broadcasts_stats() returns the expected data
56235745
* when the total count is included.

0 commit comments

Comments
 (0)