sendgrid / sendgrid-php

The Official Twilio SendGrid PHP API Library
https://sendgrid.com
MIT License
1.49k stars 623 forks source link

Cannot get list of recipient that has opened the email #1041

Closed cod3rshotout closed 3 years ago

cod3rshotout commented 3 years ago

I'm using Sendgrid and I started a marketing campaign, this is working well, through the Sendgrid dashboard I can see the email activity feed and check which recipient has opened my email. But how can I do this using the API?

I would like to get a list of emails address that has opened the email. So far I tried:

$sendgrid = new \SendGrid($this->_settings['sendgrid']['api_key']);

$query_params = json_decode('{ "start_date": "2021-03-17", "end_date": "2021-03-18", "aggregated_by": "day", "limit": "20", "offset": "0"}');
$response = $sendgrid->client->geo()->stats()->get(null, $query_params);

this simply returns:

`[
    {
        "date": "2021-03-17",
        "stats": [
            {
                "type": "country",
                "name": "FR",
                "metrics": {
                    "clicks": 0,
                    "opens": 12,
                    "unique_clicks": 0,
                    "unique_opens": 2
                }
            },
            {
                "type": "country",
                "name": "IT",
                "metrics": {
                    "clicks": 0,
                    "opens": 24,
                    "unique_clicks": 0,
                    "unique_opens": 9
                }
            },
            {
                "type": "country",
                "name": "NL",
                "metrics": {
                    "clicks": 0,
                    "opens": 4,
                    "unique_clicks": 0,
                    "unique_opens": 0
                }
            },
            {
                "type": "country",
                "name": "US",
                "metrics": {
                    "clicks": 23,
                    "opens": 20,
                    "unique_clicks": 4,
                    "unique_opens": 5
                }
            }
        ]
    },
    {
        "date": "2021-03-18",
        "stats": [
            {
                "type": "country",
                "name": "FR",
                "metrics": {
                    "clicks": 0,
                    "opens": 0,
                    "unique_clicks": 0,
                    "unique_opens": 0
                }
            },
            {
                "type": "country",
                "name": "IT",
                "metrics": {
                    "clicks": 0,
                    "opens": 1,
                    "unique_clicks": 0,
                    "unique_opens": 0
                }
            },
            {
                "type": "country",
                "name": "NL",
                "metrics": {
                    "clicks": 0,
                    "opens": 0,
                    "unique_clicks": 0,
                    "unique_opens": 0
                }
            },
            {
                "type": "country",
                "name": "US",
                "metrics": {
                    "clicks": 0,
                    "opens": 0,
                    "unique_clicks": 0,
                    "unique_opens": 0
                }
            }
        ]
    }
]`

how can I get a list of email address in the specified date range that has opened the email? Thanks

thinkingserious commented 3 years ago

Hello @cod3rshotout,

You can get that data using the /messages endpoint using something similar to this example.

With best regards,

Elmer

cod3rshotout commented 3 years ago

Hi @thinkingserious this return me only the total stats for state / province, is possible to knows only the email recipients that has opened the email?

Kind regards

thinkingserious commented 3 years ago

I think the filter needed is (Contains(events,"opened")), per the documentation here.

cod3rshotout commented 3 years ago

Hi @thinkingserious I tried:

$query_params = json_decode('{"end_date": "2021-03-18", "aggregated_by": "day", "limit": 100, "offset": 1, "start_date": "2021-03-17"}');
$response = $sendgrid->client->geo()->stats()->get(null, $query_params);
print $response->statusCode() . "\n";
print $response->body() . "\n";
print_r($response->headers());

I get:

[{"date":"2021-03-17","stats":[{"type":"country","name":"CH","metrics":{"clicks":0,"opens":0,"unique_clicks":0,"unique_opens":0}},{"type":"country","name":"CZ","metrics":{"clicks":0,"opens":0,"unique_clicks":0,"unique_opens":0}},{"type":"country","name":"DE","metrics":{"clicks":0,"opens":0,"unique_clicks":0,"unique_opens":0}},{"type":"country","name":"EE","metrics":{"clicks":0,"opens":0,"unique_clicks":0,"unique_opens":0}},{"type":"country","name":"FR","metrics":{"clicks":0,"opens":12,"unique_clicks":0,"unique_opens":2}},{"type":"country","name":"IT","metrics":{"clicks":0,"opens":24,"unique_clicks":0,"unique_opens":9}},{"type":"country","name":"NL","metrics":{"clicks":0,"opens":4,"unique_clicks":0,"unique_opens":0}},{"type":"country","name":"US","metrics":{"clicks":23,"opens":20,"unique_clicks":4,"unique_opens":5}}]},{"date":"2021-03-18","stats":[{"type":"country","name":"CH","metrics":{"clicks":0,"opens":1,"unique_clicks":0,"unique_opens":1}},{"type":"country","name":"CZ","metrics":{"clicks":0,"opens":12,"unique_clicks":0,"unique_opens":3}},{"type":"country","name":"DE","metrics":{"clicks":0,"opens":5,"unique_clicks":0,"unique_opens":4}},{"type":"country","name":"EE","metrics":{"clicks":0,"opens":1,"unique_clicks":0,"unique_opens":1}},{"type":"country","name":"FR","metrics":{"clicks":0,"opens":2,"unique_clicks":0,"unique_opens":2}},{"type":"country","name":"IT","metrics":{"clicks":0,"opens":3,"unique_clicks":0,"unique_opens":0}},{"type":"country","name":"NL","metrics":{"clicks":0,"opens":2,"unique_clicks":0,"unique_opens":0}},{"type":"country","name":"US","metrics":{"clicks":0,"opens":10,"unique_clicks":0,"unique_opens":9}}]}]

I would like to get a list of emails instead, eg:

email that has opened: foo@gmail.com, foo1@gmail.com, foo2@gmail.com .. ecc ..

JenniferMah commented 3 years ago

Hi @cod3rshotout I believe you need to use the /messages endpoint querying for to_email instead of using the /geo/stats endpoint.