Skip to content

Commit 2e7d3fc

Browse files
Add stats [GET] endpoint
1 parent ebf68af commit 2e7d3fc

4 files changed

Lines changed: 149 additions & 0 deletions

File tree

example_v3.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,31 @@
99

1010
/*
1111
12+
$start_date = "2015-12-01";
13+
$response = $sendgrid->global_stats->get($start_date);
14+
print("Status Code: " . $response->getStatusCode() . "\n");
15+
print("Body: " . $response->getBody() . "\n\n");
16+
17+
$end_date = "2015-12-14";
18+
$response = $sendgrid->global_stats->get($start_date, $end_date);
19+
print("Status Code: " . $response->getStatusCode() . "\n");
20+
print("Body: " . $response->getBody() . "\n\n");
21+
22+
$aggregated_by = "day";
23+
$response = $sendgrid->global_stats->get($start_date, $end_date, $aggregated_by);
24+
print("Status Code: " . $response->getStatusCode() . "\n");
25+
print("Body: " . $response->getBody() . "\n\n");
26+
27+
$aggregated_by = "week";
28+
$response = $sendgrid->global_stats->get($start_date, $end_date, $aggregated_by);
29+
print("Status Code: " . $response->getStatusCode() . "\n");
30+
print("Body: " . $response->getBody() . "\n\n");
31+
32+
$aggregated_by = "month";
33+
$response = $sendgrid->global_stats->get($start_date, $end_date, $aggregated_by);
34+
print("Status Code: " . $response->getStatusCode() . "\n");
35+
print("Body: " . $response->getBody() . "\n\n");
36+
1237
$group_id = 70;
1338
$email = 'example@example.com';
1439
$response = $sendgrid->asm_suppressions->delete($group_id, $email);

lib/Client.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
require 'resources/api_keys.php';
44
require 'resources/asm_groups.php';
55
require 'resources/asm_suppressions.php';
6+
require 'resources/global_stats.php';
67

78
class Client
89
{
@@ -48,6 +49,7 @@ public function __construct($apiKey, $options = array())
4849
$this->api_keys = new APIKeys($this);
4950
$this->asm_groups = new ASMGroups($this);
5051
$this->asm_suppressions = new ASMSuppressions($this);
52+
$this->global_stats = new GlobalStats($this);
5153
}
5254

5355
/**

lib/resources/global_stats.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
class GlobalStats
4+
{
5+
protected
6+
$base_endpoint,
7+
$endpoint,
8+
$client;
9+
10+
public function __construct($client, $options=NULL)
11+
{
12+
$this->name = NULL;
13+
$this->base_endpoint = "/v3/stats";
14+
$this->endpoint = "/v3/stats";
15+
$this->client = $client;
16+
}
17+
18+
public function getBaseEndpoint(){
19+
return $this->base_endpoint;
20+
}
21+
22+
public function getEndpoint(){
23+
return $this->endpoint;
24+
}
25+
26+
public function setEndpoint($endpoint){
27+
$this->endpoint = $endpoint;
28+
}
29+
30+
public function get($start_date, $end_date=Null, $aggregated_by=Null){
31+
$this->endpoint = $this->base_endpoint . "?start_date=" . $start_date;
32+
if($end_date != Null){
33+
$this->endpoint = $this->endpoint . "&end_date=" . $end_date;
34+
}
35+
if($aggregated_by != Null){
36+
$this->endpoint = $this->endpoint . "&aggregated_by=" . $aggregated_by;
37+
}
38+
return $this->client->getRequest($this);
39+
}
40+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
require_once __DIR__.'/baseTest.php';
3+
4+
class SendGridTest_GlobalStats extends baseTest
5+
{
6+
public function testGET()
7+
{
8+
$code = 200;
9+
$headers = array('Content-Type' => 'application/json');
10+
$body = '[
11+
{
12+
"date": "2015-01-01",
13+
"stats": [
14+
{
15+
"metrics": {
16+
"blocks": 1,
17+
"bounce_drops": 0,
18+
"bounces": 0,
19+
"clicks": 0,
20+
"deferred": 1,
21+
"delivered": 1,
22+
"invalid_emails": 1,
23+
"opens": 1,
24+
"processed": 2,
25+
"requests": 3,
26+
"spam_report_drops": 0,
27+
"spam_reports": 0,
28+
"unique_clicks": 0,
29+
"unique_opens": 1,
30+
"unsubscribe_drops": 0,
31+
"unsubscribes": 0
32+
}
33+
}
34+
]
35+
},
36+
{
37+
"date": "2015-01-02",
38+
"stats": [
39+
{
40+
"metrics": {
41+
"blocks": 0,
42+
"bounce_drops": 0,
43+
"bounces": 0,
44+
"clicks": 0,
45+
"deferred": 0,
46+
"delivered": 0,
47+
"invalid_emails": 0,
48+
"opens": 0,
49+
"processed": 0,
50+
"requests": 0,
51+
"spam_report_drops": 0,
52+
"spam_reports": 0,
53+
"unique_clicks": 0,
54+
"unique_opens": 0,
55+
"unsubscribe_drops": 0,
56+
"unsubscribes": 0
57+
}
58+
}
59+
]
60+
}
61+
]';
62+
63+
$sendgrid = $this->buildClient($code, $headers, $body);
64+
$start_date = "2015-01-01";
65+
$response = $sendgrid->global_stats->get($start_date);
66+
$this->assertEquals($code, $response->getStatusCode());
67+
$this->assertEquals($body, $response->getBody());
68+
69+
$sendgrid = $this->buildClient($code, $headers, $body);
70+
$end_date = "2015-01-02";
71+
$response = $sendgrid->global_stats->get($start_date, $end_date);
72+
$this->assertEquals($code, $response->getStatusCode());
73+
$this->assertEquals($body, $response->getBody());
74+
75+
$sendgrid = $this->buildClient($code, $headers, $body);
76+
$aggregated_by = "day";
77+
$response = $sendgrid->global_stats->get($start_date, $end_date, $aggregated_by);
78+
$this->assertEquals($code, $response->getStatusCode());
79+
$this->assertEquals($body, $response->getBody());
80+
}
81+
82+
}

0 commit comments

Comments
 (0)