Skip to content

Commit

Permalink
Merge pull request #24 from SFDigitalServices/feat/sendgrid_filter_cl…
Browse files Browse the repository at this point in the history
…icktrack

Add `clicktrack` filter
```json
"filters" : {
    "clicktrack" : {
      "settings" : {
        "enable" : 0,
        "enable_text" : false
      }
    }
  }
```
  • Loading branch information
hshaosf authored Oct 23, 2023
2 parents 9659355 + 7d0fb62 commit 336a6bc
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ brew services list
```

#### Environment and Dependencies

Install Pipenv (if needed)

```bash
Expand Down Expand Up @@ -161,6 +162,38 @@ You can provide attachments either inline (with the base64-encoded `content` fie
}
```

### Filter: clicktrack

[docs.sendgrid.com](https://docs.sendgrid.com/for-developers/sending-email/smtp-filters#filter-clicktrack)

```json
{
"subject": "Hi Diddly Ho",
"to": [{
"email": "[email protected]",
"name": "Homer Simpson"
}],
"from": {
"email": "[email protected]",
"name": "Ned Flanders"
},
"content": [
{
"type": "text/plain",
"value": "Try search with https://google.com"
}
],
"filters" : {
"clicktrack" : {
"settings" : {
"enable" : 0,
"enable_text" : false
}
}
}
}
```

## Testing

Code coverage command with missing statement line numbers
Expand Down
15 changes: 14 additions & 1 deletion tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import celery
from python_http_client.exceptions import HTTPError
import sendgrid
from sendgrid.helpers.mail import (Mail, From, To, Cc, Bcc, Personalization, Subject, Asm, GroupId, GroupsToDisplay)
from sendgrid.helpers.mail import (Mail, From, To, Cc, Bcc, Personalization, Subject, Asm, GroupId, GroupsToDisplay, TrackingSettings, ClickTracking)
from jinja2 import Environment, BaseLoader
from jinja2.filters import FILTERS, pass_environment
from jinja2.exceptions import TemplateNotFound
Expand Down Expand Up @@ -155,6 +155,19 @@ def generate_message(data):
message.asm = Asm(GroupId(data['asm']['group_id']),
GroupsToDisplay(data['asm']['groups_to_display']))

# https://docs.sendgrid.com/for-developers/sending-email/smtp-filters#filter-clicktrack
if 'filters' in data \
and 'clicktrack' in data['filters'] \
and 'settings' in data['filters']['clicktrack']:

clicktrack_settings = data['filters']['clicktrack']['settings']
clicktrack_enable = bool(clicktrack_settings.get('enable', False))
clicktrack_enable_text = bool(clicktrack_settings.get('enable_text', False))

tracking_settings = TrackingSettings()
tracking_settings.click_tracking = ClickTracking(clicktrack_enable, clicktrack_enable_text)
message.tracking_settings = tracking_settings

func_switcher = {
"content": HelperService.get_content,
"attachments": HelperService.get_attachments,
Expand Down
8 changes: 8 additions & 0 deletions tests/mocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@
"asm": {
"group_id": 1,
"groups_to_display": [1, 2]
},
"filters" : {
"clicktrack" : {
"settings" : {
"enable" : 0,
"enable_text" : False
}
}
}
}

Expand Down

0 comments on commit 336a6bc

Please sign in to comment.