Skip to content
This repository was archived by the owner on Aug 18, 2023. It is now read-only.

Commit 8f946ae

Browse files
committed
Merge branch 'master' into 202104_deprecate_misnamed_methods
2 parents 797685b + 2d277fe commit 8f946ae

5 files changed

Lines changed: 101 additions & 8 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
## CHANGELOG
2-
### Unreleased
2+
### [Unreleased]
3+
- Update - Add people/search endpoint.
4+
- Update - Add data-privacy/deletion-request endpoint.
35
- Deprecate - Rename List API methods: getListDetails, updateListDetails, subscribeMembersToList, unsubscribeMembersFromList, getGroupMemberIdentifiers, getAllExclusionsOnList
46
- Deprecate - Rename Metric API methods: getMetricTimeline, exportMetricData
57

@@ -41,3 +43,5 @@
4143

4244
### 1.0
4345
- As this changelog was created after 2.0.0, please refer to the README for version 1 https://github.com/klaviyo/php-klaviyo/tree/d388ca998dff55b2a7e420c2c7aa2cd221365d1c
46+
47+
[Unreleased]: https://github.com/klaviyo/php-klaviyo/compare/2.2.5...HEAD

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,21 @@ $client->profiles->getAllProfileMetricsTimeline( 'ProfileId' );
149149

150150
#Get a specific metric for a profile
151151
$client->profiles->getProfileMetricTimeline( 'ProfileId', 'MetricId' );
152+
153+
#Get a profile's ID by its email address
154+
$client->profiles->getProfileIdByEmail('[email protected]');
155+
```
156+
157+
### You can request a privacy-compliant profile deletion given an identifying property
158+
```php
159+
#Request profile deletion by email
160+
$client->dataprivacy->requestProfileDeletion('[email protected]');
161+
162+
#Request profile deletion by phone number
163+
$client->dataprivacy->requestProfileDeletion('1-234-567-8910', 'phone_number');
164+
165+
#Request profile deletion by person ID
166+
$client->dataprivacy->requestProfileDeletion('abc123', 'person_id');
152167
```
153168

154169
## Exceptions

src/DataPrivacy.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace Klaviyo;
4+
5+
use Klaviyo\Exception\KlaviyoException;
6+
7+
/**
8+
* Class DataPrivacy
9+
* @package Klaviyo
10+
*/
11+
class DataPrivacy extends KlaviyoAPI
12+
{
13+
/**
14+
* Data Privacy endpoint constants
15+
*/
16+
const ENDPOINT_DATA_PRIVACY = 'data-privacy';
17+
const ENDPOINT_DELETION_REQUEST = 'deletion-request';
18+
19+
/**
20+
* Data Privacy arguments
21+
*/
22+
const PHONE_NUMBER = 'phone_number';
23+
const PERSON_ID = 'person_id';
24+
25+
const PROFILE_DELETION_VALID_ID_TYPES = [
26+
self::EMAIL,
27+
self::PHONE_NUMBER,
28+
self::PERSON_ID,
29+
];
30+
31+
/**
32+
* Request a data privacy-compliant deletion for the person record corresponding
33+
* to an email address, phone number, or person identifier. If multiple person
34+
* records exist for the provided identifier, only one of them will be deleted.
35+
*
36+
* @link https://www.klaviyo.com/docs/api/v2/data-privacy#post-deletion-request
37+
*
38+
* @param $identifier string Value by which to identify the profile being deleted.
39+
* @param $idType string Identifier type e.g. email, phone_number, person_id.
40+
* @return mixed
41+
* @throws KlaviyoException
42+
*/
43+
public function requestProfileDeletion($identifier, $idType = self::EMAIL)
44+
{
45+
if (!in_array($idType, self::PROFILE_DELETION_VALID_ID_TYPES)) {
46+
throw new KlaviyoException(
47+
sprintf(
48+
'Invalid id_type provided, must be one of: %s',
49+
implode(', ', self::PROFILE_DELETION_VALID_ID_TYPES)
50+
)
51+
);
52+
}
53+
54+
$options = $this->createParams($idType, $identifier);
55+
$path = sprintf('%s/%s', self::ENDPOINT_DATA_PRIVACY, self::ENDPOINT_DELETION_REQUEST);
56+
57+
return $this->v2Request($path, $options, self::HTTP_POST);
58+
}
59+
}

src/KlaviyoAPI.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,10 @@ abstract class KlaviyoAPI
5858
/**
5959
* Klaviyo API arguments
6060
*/
61-
const PROFILES = 'profiles';
6261
const COUNT = 'count';
62+
const EMAIL = 'email';
6363
const PAGE = 'page';
64+
const PROFILES = 'profiles';
6465
const SINCE = 'since';
6566
const SORT = 'sort';
6667

@@ -123,7 +124,7 @@ protected function v1Request( $path, $options = [], $method = self::HTTP_GET )
123124
/**
124125
* Make private v2 API request
125126
*
126-
* @param $path Endpoint to call
127+
* @param string $path Endpoint to call
127128
* @param array $options API params to add to request
128129
* @param string $method HTTP method for request
129130
* @return mixed
@@ -176,13 +177,11 @@ private function handleResponse( $response, $statusCode, $isPublic )
176177
{
177178
if ( $statusCode == 403 ) {
178179
throw new KlaviyoAuthenticationException(self::ERROR_INVALID_API_KEY, $statusCode);
179-
} else if ( $statusCode == 404 ) {
180-
throw new KlaviyoResourceNotFoundException( self::ERROR_RESOURCE_DOES_NOT_EXIST, $statusCode);
181180
} else if ( $statusCode == 429 ) {
182181
throw new KlaviyoRateLimitException(
183-
$this->returnRateLimit( $this->decodeJsonResponse( $response ), $statusCode )
182+
$this->returnRateLimit( $this->decodeJsonResponse( $response ) )
184183
);
185-
} else if ( $statusCode != 200 ) {
184+
} else if ($statusCode < 200 || $statusCode >= 300) {
186185
throw new KlaviyoApiException($this->decodeJsonResponse( $response )['detail'], $statusCode);
187186
}
188187

@@ -354,7 +353,7 @@ private function returnRateLimit ( $response )
354353
* Return formatted options.
355354
*
356355
* @param string $paramName Name of API Param to create
357-
* @param $paramValue Value of API params to create
356+
* @param mixed $paramValue Value of API params to create
358357
*/
359358
protected function createParams( $paramName, $paramValue )
360359
{

src/Profiles.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ class Profiles extends KlaviyoAPI
1111
const PERSON = 'person';
1212
const METRICS = 'metrics';
1313
const METRIC = 'metric';
14+
const PEOPLE = 'people';
15+
const SEARCH = 'search';
1416
const TIMELINE = 'timeline';
1517

1618
/**
@@ -130,4 +132,18 @@ public function getProfileMetricTimeline( $personId, $metricId, $since = null, $
130132
return $this->v1Request( $path, $params );
131133
}
132134

135+
/**
136+
* Get ID of profile by email address.
137+
*
138+
* @param string $email Email address of desired profile.
139+
* @return mixed
140+
* @throws Exception\KlaviyoException
141+
*/
142+
public function getProfileIdByEmail($email)
143+
{
144+
$params = $this->createRequestBody([self::EMAIL => $email]);
145+
$path = sprintf('%s/%s', self::PEOPLE, self::SEARCH);
146+
147+
return $this->v2Request($path, $params);
148+
}
133149
}

0 commit comments

Comments
 (0)