Skip to content

Commit e0a848e

Browse files
authored
Merge pull request #110 from Kit/add-subscriber-stats-method
Add `get_subscriber_stats` method
2 parents e80ea8e + ad4c6a5 commit e0a848e

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
@@ -1044,6 +1044,20 @@ public function unsubscribe(int $subscriber_id)
10441044
return $this->post(sprintf('subscribers/%s/unsubscribe', $subscriber_id));
10451045
}
10461046

1047+
/**
1048+
* Get the email statistics for a specific subscriber.
1049+
*
1050+
* @param integer $id Subscriber ID.
1051+
*
1052+
* @see https://developers.kit.com/api-reference/subscribers/list-stats-for-a-subscriber
1053+
*
1054+
* @return mixed|object
1055+
*/
1056+
public function get_subscriber_stats(int $id)
1057+
{
1058+
return $this->get(sprintf('subscribers/%s/stats', $id));
1059+
}
1060+
10471061
/**
10481062
* Get a list of the tags for a subscriber.
10491063
*

tests/ConvertKitAPITest.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1775,6 +1775,49 @@ public function testUpdateTagNameWithBlankName()
17751775
);
17761776
}
17771777

1778+
/**
1779+
* Test that get_subscriber_stats() returns the expected data
1780+
* when using a valid subscriber ID.
1781+
*
1782+
* @since 2.2.1
1783+
*
1784+
* @return void
1785+
*/
1786+
public function testGetSubscriberStats()
1787+
{
1788+
$result = $this->api->get_subscriber_stats(
1789+
id: (int) $_ENV['CONVERTKIT_API_SUBSCRIBER_ID']
1790+
);
1791+
$this->assertArrayHasKey('subscriber', get_object_vars($result));
1792+
$this->assertArrayHasKey('id', get_object_vars($result->subscriber));
1793+
$this->assertArrayHasKey('stats', get_object_vars($result->subscriber));
1794+
$this->assertArrayHasKey('sent', get_object_vars($result->subscriber->stats));
1795+
$this->assertArrayHasKey('opened', get_object_vars($result->subscriber->stats));
1796+
$this->assertArrayHasKey('clicked', get_object_vars($result->subscriber->stats));
1797+
$this->assertArrayHasKey('bounced', get_object_vars($result->subscriber->stats));
1798+
$this->assertArrayHasKey('open_rate', get_object_vars($result->subscriber->stats));
1799+
$this->assertArrayHasKey('click_rate', get_object_vars($result->subscriber->stats));
1800+
$this->assertArrayHasKey('last_sent', get_object_vars($result->subscriber->stats));
1801+
$this->assertArrayHasKey('last_opened', get_object_vars($result->subscriber->stats));
1802+
$this->assertArrayHasKey('last_clicked', get_object_vars($result->subscriber->stats));
1803+
$this->assertArrayHasKey('sends_since_last_open', get_object_vars($result->subscriber->stats));
1804+
$this->assertArrayHasKey('sends_since_last_click', get_object_vars($result->subscriber->stats));
1805+
}
1806+
1807+
/**
1808+
* Test that get_subscriber_stats() throws a ClientException when an invalid
1809+
* subscriber ID is specified.
1810+
*
1811+
* @since 2.2.1
1812+
*
1813+
* @return void
1814+
*/
1815+
public function testGetSubscriberStatsWithInvalidSubscriberID()
1816+
{
1817+
$this->expectException(ClientException::class);
1818+
$result = $this->api->get_subscriber_stats(12345);
1819+
}
1820+
17781821
/**
17791822
* Test that tag_subscriber_by_email() returns the expected data.
17801823
*

0 commit comments

Comments
 (0)