1
|
|
|
<?php |
2
|
|
|
namespace Azine\EmailBundle\Services; |
3
|
|
|
|
4
|
|
|
use Azine\EmailBundle\DependencyInjection\AzineEmailExtension; |
5
|
|
|
use Azine\EmailBundle\Services\AzineEmailTwigExtension; |
6
|
|
|
use Ramsey\Uuid\Uuid; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Implementation of the EmailOpenTrackingCodeBuilderInterface used to track email open events. |
10
|
|
|
* |
11
|
|
|
* This impelementation will create a html snippet with the image tracker code for |
12
|
|
|
* either GoogleAnalytics or Piwik, depending on the configured tracking url |
13
|
|
|
* ( azine_email_email_open_tracking_url) in your config.yml |
14
|
|
|
* |
15
|
|
|
* Class AzineEmailOpenTrackingCodeBuilder |
16
|
|
|
* @package Azine\EmailBundle\Services |
17
|
|
|
*/ |
18
|
|
|
class AzineEmailOpenTrackingCodeBuilder implements EmailOpenTrackingCodeBuilderInterface { |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $tracking_params_campaign_source; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected $tracking_params_campaign_medium; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
protected $tracking_params_campaign_content; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
protected $tracking_params_campaign_name; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
protected $tracking_params_campaign_term; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var $string|null |
47
|
|
|
*/ |
48
|
|
|
private $trackingUrlTemplate; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var string the html-code template |
52
|
|
|
*/ |
53
|
|
|
protected $imageHtmlCode = "<img src='%s' style='border:0' alt='' />"; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param $trackingUrlTemplate the url configured in your config.yml or null if you didn't specify a tracking url. |
57
|
|
|
* @param $parameters array with the parameter names for the campaign tracking |
58
|
|
|
*/ |
59
|
8 |
|
public function __construct($trackingUrlTemplate, $parameters){ |
60
|
8 |
|
$this->trackingUrlTemplate = $trackingUrlTemplate; |
61
|
8 |
|
$this->tracking_params_campaign_content = $parameters[AzineEmailExtension::TRACKING_PARAM_CAMPAIGN_CONTENT]; |
62
|
8 |
|
$this->tracking_params_campaign_medium = $parameters[AzineEmailExtension::TRACKING_PARAM_CAMPAIGN_MEDIUM]; |
63
|
8 |
|
$this->tracking_params_campaign_name = $parameters[AzineEmailExtension::TRACKING_PARAM_CAMPAIGN_NAME]; |
64
|
8 |
|
$this->tracking_params_campaign_source = $parameters[AzineEmailExtension::TRACKING_PARAM_CAMPAIGN_SOURCE]; |
65
|
8 |
|
$this->tracking_params_campaign_term = $parameters[AzineEmailExtension::TRACKING_PARAM_CAMPAIGN_TERM]; |
66
|
8 |
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param $templateBaseId the template used for rendering the email (without the .html.twig or .txt.twig extension) |
70
|
|
|
* @param array $campaignParams the campaign-parameters used for this email |
71
|
|
|
* @param the $messageId the id of the message |
72
|
|
|
* @param email $to to-recipient-email(s) or null |
73
|
|
|
* @param email $cc cc-recipient-email(s) or null |
74
|
|
|
* @param email $bcc bcc-recipient-email(s) or null |
75
|
|
|
* |
76
|
|
|
* @return null|string Email open tracking code for google analytics or piwik or null |
77
|
|
|
*/ |
78
|
|
|
public function getTrackingImgCode($templateBaseId, array $campaignParams, array $emailTemplateParams, $messageId, $to, $cc, $bcc){ |
79
|
|
|
if($this->trackingUrlTemplate == null){ |
80
|
|
|
return null; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$recipients = md5($this->merge($to, $cc, $bcc)); |
84
|
|
|
|
85
|
|
|
$trackingUrl = ''; |
|
|
|
|
86
|
|
|
if(strpos($this->trackingUrlTemplate, 'www.google-analytics.com') !== false) { |
87
|
|
|
$trackingUrl = $this->getGoogleAnalyticsUrl($this->trackingUrlTemplate, $templateBaseId, $campaignParams, $emailTemplateParams, $messageId, $recipients); |
88
|
|
|
} else { |
89
|
|
|
$trackingUrl = $this->getPiwikUrl($this->trackingUrlTemplate, $templateBaseId, $campaignParams, $emailTemplateParams, $messageId, $recipients); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
// return the html code for the tracking image |
93
|
|
|
$imgTrackingCode = sprintf($this->imageHtmlCode, $trackingUrl); |
94
|
|
|
return $imgTrackingCode; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* concatenate all recipients into an array and implode with ';' to a string |
99
|
|
|
* @param $to |
100
|
|
|
* @param $cc |
101
|
|
|
* @param $bcc |
102
|
|
|
* @return string |
103
|
|
|
*/ |
104
|
|
|
protected function merge($to, $cc, $bcc){ |
105
|
|
|
if($to && !is_array($to)){ |
106
|
|
|
$to = array($to); |
107
|
|
|
} |
108
|
|
|
if(!is_array($cc)){ |
109
|
|
|
$cc = array($cc); |
110
|
|
|
} |
111
|
|
|
if(!is_array($bcc)){ |
112
|
|
|
$bcc = array($bcc); |
113
|
|
|
} |
114
|
|
|
$all = array_merge($to, $cc, $bcc); |
115
|
|
|
|
116
|
|
|
return implode(";", $all); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Build tracking image code with an URL according to these sources: |
121
|
|
|
* http://dyn.com/blog/tracking-email-opens-via-google-analytics/ |
122
|
|
|
* https://developers.google.com/analytics/devguides/collection/protocol/v1/email#protocol |
123
|
|
|
* |
124
|
|
|
* @param $baseUrl string something like: https://www.google-analytics.com/collect?v=1&cm=email&t=event&ec=email&ea=open&tid=TRACKING_ID replace the TRACKING_ID with your google analytics tracking ID. |
125
|
|
|
* @param $templateBaseId string |
126
|
|
|
* @param array $campaignParams |
127
|
|
|
* @param array $emailTemplateParams |
128
|
|
|
* @param $messageId string |
129
|
|
|
* @param $recipients string |
130
|
|
|
*/ |
131
|
|
|
protected function getGoogleAnalyticsUrl($baseUrl, $templateBaseId, array $campaignParams, array $emailTemplateParams, $messageId, $recipients){ |
|
|
|
|
132
|
|
|
$url = $baseUrl. |
133
|
|
|
"&uid=".$recipients. // anonymized id generated from the concatenated string of all recipients email addresses |
134
|
|
|
"&cid=".Uuid::uuid4()->toString(). // random UUID |
135
|
|
|
"&el=".$recipients. // anonymized id generated from the concatenated string of all recipients email addresses |
136
|
|
|
"&dp=/email/".$templateBaseId. // the email-template used for this email |
137
|
|
|
"&cs=".$this->getCampaignSource($campaignParams, $templateBaseId). // campaing source for this email |
138
|
|
|
"&cn=".$this->getCampaignName($campaignParams). // campaign name for this email |
139
|
|
|
"&z=".microtime(); // cache buster |
140
|
|
|
|
141
|
|
|
return $url; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Build tracking image code with an URL according to these sources: |
146
|
|
|
* |
147
|
|
|
* |
148
|
|
|
* @param $baseUrl string something like: https://your.host.com/piwik-directory/piwik.php?&rec=1&bots=1&e_c=email&e_a=open&e_v=1&idsite=SITE_ID replace the path to your piwik.php and the SITE_ID according to your needs. |
149
|
|
|
* @param $templateBaseId string |
150
|
|
|
* @param array $campaignParams |
151
|
|
|
* @param array $emailTemplateParams |
152
|
|
|
* @param $messageId string |
153
|
|
|
* @param $recipients string |
154
|
|
|
*/ |
155
|
|
|
protected function getPiwikUrl($baseUrl, $templateBaseId, array $campaignParams, array $emailTemplateParams, $messageId, $recipients){ |
|
|
|
|
156
|
|
|
$url = $baseUrl. |
157
|
|
|
"&_id=".substr($recipients, 0, 16). // user: 16 characters hexadecimal string |
158
|
|
|
"&url=/email/".$templateBaseId. // document path |
159
|
|
|
"&rand=".microtime(). // cache buster |
160
|
|
|
"&e_n=".$this->getCampaignSource($campaignParams, $templateBaseId). // event name => will be categorized with the / => e.g. email / newsletter |
161
|
|
|
"&action_name=".$this->getCampaignName($campaignParams);// action name |
162
|
|
|
|
163
|
|
|
return $url; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @param $campaignParams |
168
|
|
|
* @param $templateId |
169
|
|
|
* @return string if no source-value is defined in the $campaignParams, $templateId will be used. |
170
|
|
|
*/ |
171
|
|
|
protected function getCampaignSource($campaignParams, $templateId){ |
172
|
|
|
if(array_key_exists($this->tracking_params_campaign_source, $campaignParams)){ |
173
|
|
|
return $campaignParams[$this->tracking_params_campaign_source]; |
174
|
|
|
} |
175
|
|
|
return $templateId; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @param $campaignParams |
180
|
|
|
* @param $templateId |
181
|
|
|
* @return string if no name-value is defined in the $campaignParams, $templateId will be used. |
182
|
|
|
*/ |
183
|
|
|
protected function getCampaignName($campaignParams){ |
184
|
|
|
if(array_key_exists($this->tracking_params_campaign_name, $campaignParams)){ |
185
|
|
|
return $campaignParams[$this->tracking_params_campaign_name]; |
186
|
|
|
} |
187
|
|
|
return date("y-m-d"); |
188
|
|
|
} |
189
|
|
|
} |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.