Skip to content

Commit 522723b

Browse files
committed
Add get_subscriber_stats method
1 parent bf2b64e commit 522723b

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

src/ConvertKit_API_Traits.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,6 +1027,20 @@ public function unsubscribe(int $subscriber_id)
10271027
return $this->post(sprintf('subscribers/%s/unsubscribe', $subscriber_id));
10281028
}
10291029

1030+
/**
1031+
* Get the email statistics for a specific subscriber.
1032+
*
1033+
* @param integer $id Subscriber ID.
1034+
*
1035+
* @see https://developers.kit.com/api-reference/subscribers/list-stats-for-a-subscriber
1036+
*
1037+
* @return mixed|object
1038+
*/
1039+
public function get_subscriber_stats(int $id)
1040+
{
1041+
return $this->get(sprintf('subscribers/%s/stats', $id));
1042+
}
1043+
10301044
/**
10311045
* Get a list of the tags for a subscriber.
10321046
*

tests/ConvertKitAPITest.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1722,6 +1722,49 @@ public function testCreateTagsThatExist()
17221722
$this->assertEquals($result->tags[0]->name, $_ENV['CONVERTKIT_API_TAG_NAME_2']);
17231723
}
17241724

1725+
/**
1726+
* Test that get_subscriber_stats() returns the expected data
1727+
* when using a valid subscriber ID.
1728+
*
1729+
* @since 2.2.1
1730+
*
1731+
* @return void
1732+
*/
1733+
public function testGetSubscriberStats()
1734+
{
1735+
$result = $this->api->get_subscriber_stats(
1736+
id: (int) $_ENV['CONVERTKIT_API_SUBSCRIBER_ID']
1737+
);
1738+
$this->assertArrayHasKey('subscriber', get_object_vars($result));
1739+
$this->assertArrayHasKey('id', get_object_vars($result->subscriber));
1740+
$this->assertArrayHasKey('stats', get_object_vars($result->subscriber));
1741+
$this->assertArrayHasKey('sent', get_object_vars($result->subscriber->stats));
1742+
$this->assertArrayHasKey('opened', get_object_vars($result->subscriber->stats));
1743+
$this->assertArrayHasKey('clicked', get_object_vars($result->subscriber->stats));
1744+
$this->assertArrayHasKey('bounced', get_object_vars($result->subscriber->stats));
1745+
$this->assertArrayHasKey('open_rate', get_object_vars($result->subscriber->stats));
1746+
$this->assertArrayHasKey('click_rate', get_object_vars($result->subscriber->stats));
1747+
$this->assertArrayHasKey('last_sent', get_object_vars($result->subscriber->stats));
1748+
$this->assertArrayHasKey('last_opened', get_object_vars($result->subscriber->stats));
1749+
$this->assertArrayHasKey('last_clicked', get_object_vars($result->subscriber->stats));
1750+
$this->assertArrayHasKey('sends_since_last_open', get_object_vars($result->subscriber->stats));
1751+
$this->assertArrayHasKey('sends_since_last_click', get_object_vars($result->subscriber->stats));
1752+
}
1753+
1754+
/**
1755+
* Test that get_subscriber_stats() throws a ClientException when an invalid
1756+
* subscriber ID is specified.
1757+
*
1758+
* @since 2.2.1
1759+
*
1760+
* @return void
1761+
*/
1762+
public function testGetSubscriberStatsWithInvalidSubscriberID()
1763+
{
1764+
$this->expectException(ClientException::class);
1765+
$result = $this->api->get_subscriber_stats(12345);
1766+
}
1767+
17251768
/**
17261769
* Test that tag_subscriber_by_email() returns the expected data.
17271770
*

0 commit comments

Comments
 (0)