Skip to content

Commit b27fb4f

Browse files
authored
Merge pull request #214 from intercom/timlim/add-visitors
Add support for visitors endpoint
2 parents 5854836 + 8c3a726 commit b27fb4f

File tree

4 files changed

+169
-0
lines changed

4 files changed

+169
-0
lines changed

README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,44 @@ $client->leads->convertLead([
128128
]
129129
]);
130130
```
131+
## Visitors
132+
Retrieve `user_id` of a visitor via [the Javscript API](https://developers.intercom.com/docs/intercom-javascript#section-intercomgetvisitorid)
133+
134+
```php
135+
// Update a visitor
136+
$client->visitors->update([
137+
"user_id" => "8a88a590-e1c3-41e2-a502-e0649dbf721c",
138+
"custom_attributes" => ['foo' => 'bar']
139+
]);
140+
141+
// Find a visitor by ID
142+
$client->visitors->getVisitor("570680a8a1bcbca8a90000a9");
143+
144+
// Find a visitor by User ID
145+
$client->visitors->getVisitor("", ["user_id" => "8a88a590-e1c3-41e2-a502-e0649dbf721c"]);
146+
147+
// Delete a visitor by ID
148+
$client->visitors->deleteVisitor("570680a8a1bcbca8a90000a9");
149+
150+
// Convert a Visitor to a Lead
151+
$client->visitors->convertVisitor([
152+
"visitor" => [
153+
"user_id" => "8a88a590-e1c3-41e2-a502-e0649dbf721c"
154+
],
155+
"type" => "lead"
156+
]);
157+
158+
// Convert a Visitor to a User
159+
$client->visitors->convertVisitor([
160+
"visitor" => [
161+
"user_id" => "8a88a590-e1c3-41e2-a502-e0649dbf721c"
162+
],
163+
"user" => [
164+
"email" => "[email protected]"
165+
],
166+
"type" => "user"
167+
]);
168+
```
131169

132170
## Tags
133171

src/IntercomClient.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ public function __construct($usernamePart, $passwordPart)
6666
$this->messages = new IntercomMessages($this);
6767
$this->conversations = new IntercomConversations($this);
6868
$this->leads = new IntercomLeads($this);
69+
$this->visitors = new IntercomVisitors($this);
6970
$this->admins = new IntercomAdmins($this);
7071
$this->tags = new IntercomTags($this);
7172
$this->segments = new IntercomSegments($this);

src/IntercomVisitors.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
namespace Intercom;
4+
5+
class IntercomVisitors {
6+
7+
/** @var IntercomClient */
8+
private $client;
9+
10+
/**
11+
* IntercomVisitors constructor.
12+
* @param IntercomClient $client
13+
*/
14+
public function __construct($client)
15+
{
16+
$this->client = $client;
17+
}
18+
19+
/**
20+
* Updates Visitor.
21+
* @see https://developers.intercom.com/reference#update-a-visitor
22+
* @param array $options
23+
* @return mixed
24+
* @throws \GuzzleHttp\Exception\GuzzleException
25+
*/
26+
public function update($options)
27+
{
28+
return $this->client->put("visitors", $options);
29+
}
30+
31+
32+
/**
33+
* Returns single Visitor.
34+
* @see https://developers.intercom.com/reference#view-a-visitor
35+
* @param string $id
36+
* @param array $options
37+
* @return mixed
38+
* @throws \GuzzleHttp\Exception\GuzzleException
39+
*/
40+
public function getVisitor($id, $options = [])
41+
{
42+
$path = $this->visitorPath($id);
43+
return $this->client->get($path, $options);
44+
}
45+
46+
/**
47+
* Deletes Visitor.
48+
* @see https://developers.intercom.com/reference#delete-a-visitor
49+
* @param string $id
50+
* @param array $options
51+
* @return mixed
52+
* @throws \GuzzleHttp\Exception\GuzzleException
53+
*/
54+
public function deleteVisitor($id, $options = [])
55+
{
56+
$path = $this->visitorPath($id);
57+
return $this->client->delete($path, $options);
58+
}
59+
60+
/**
61+
* Converts Visitor.
62+
* @see https://developers.intercom.io/reference#convert-a-lead
63+
* @param $options
64+
* @return mixed
65+
* @throws \GuzzleHttp\Exception\GuzzleException
66+
*/
67+
public function convertVisitor($options)
68+
{
69+
return $this->client->post("visitors/convert", $options);
70+
}
71+
72+
/**
73+
* Returns endpoint path to Visitor with given ID.
74+
* @param string $id
75+
* @return string
76+
*/
77+
public function visitorPath($id)
78+
{
79+
return "visitors/" . $id;
80+
}
81+
}

test/IntercomVisitorsTest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
use Intercom\IntercomVisitors;
4+
5+
class IntercomVisitorsTest extends PHPUnit_Framework_TestCase {
6+
7+
public function testVisitorUpdate()
8+
{
9+
$stub = $this->getMockBuilder('Intercom\IntercomClient')->disableOriginalConstructor()->getMock();
10+
$stub->method('put')->willReturn('foo');
11+
12+
$visitors = new IntercomVisitors($stub);
13+
$this->assertEquals('foo', $visitors->update([]));
14+
}
15+
16+
public function testVisitorPath()
17+
{
18+
$stub = $this->getMockBuilder('Intercom\IntercomClient')->disableOriginalConstructor()->getMock();
19+
$visitors = new IntercomVisitors($stub);
20+
$this->assertEquals($visitors->visitorPath("foo"), "visitors/foo");
21+
}
22+
23+
public function testVisitorsGet()
24+
{
25+
$stub = $this->getMockBuilder('Intercom\IntercomClient')->disableOriginalConstructor()->getMock();
26+
$stub->method('get')->willReturn('foo');
27+
28+
$visitors = new IntercomVisitors($stub);
29+
$this->assertEquals('foo', $visitors->getVisitor("bar"));
30+
}
31+
32+
public function testVisitorsConvert()
33+
{
34+
$stub = $this->getMockBuilder('Intercom\IntercomClient')->disableOriginalConstructor()->getMock();
35+
$stub->method('post')->will($this->returnArgument(0));
36+
37+
$visitors = new IntercomVisitors($stub);
38+
$this->assertEquals('visitors/convert', $visitors->convertVisitor([]));
39+
}
40+
41+
public function testVisitorsDelete()
42+
{
43+
$stub = $this->getMockBuilder('Intercom\IntercomClient')->disableOriginalConstructor()->getMock();
44+
$stub->method('delete')->willReturn('foo');
45+
46+
$visitors = new IntercomVisitors($stub);
47+
$this->assertEquals('foo', $visitors->deleteVisitor("bar"));
48+
}
49+
}

0 commit comments

Comments
 (0)