Automatic tagging for Google Analytics
I wrote a solution for an automatic tagging for Google Analytics and it is working on Sendy 1.1.7.3.
How does it work
Everytime the user clicks on a link in your newsletter, he first goes to your_sendy_installation_folder/l.php and then Sendy finally redirects him to the link he wants to access. My solution just append the Google Analytics query string to this link URL the user is redirected to.
Installation
Just copy & paste the code bellow right before the header("Location: $link"); in the end of the your_sendy_installation_folder/l.php file.
//Get the campaign title from database
$q6 = 'SELECT title FROM campaigns WHERE id = '.$campaign_id;
$r6 = mysqli_query($mysqli, $q6);
if ($r6)
{
$row = mysqli_fetch_array($r6);
$title = $row['title'];
if (preg_match('/\\?[^"]/', $link)) {
//If the URL has other queries already, then append the Google Analytics query starting with &
$link .= '&utm_source=Sendy&utm_campaign='.urlencode($title.'&utm_medium=email';
} else {
//If not, so these are its first URL query, then append them starting with ?
$link .= '?utm_source=Sendy&utm_campaign='.$title.'&utm_medium=email';
}
}
That's it. Cheers!
This discussion has been closed.
Comments
The code seems to take an empty result into account with if ($r6), but did trigger on an autoresponder in my setup. This resulted in an empty $title but still the UTM parameters where appended. So I think this enhancement is needed:
//Get the campaign title from database if($ares_emails_id=='') //if link does not belong to an autoresponder campaign { $q6 = 'SELECT title FROM campaigns WHERE id = '.$campaign_id; } else { $q6 = 'SELECT title FROM ares_emails WHERE id = '.$campaign_id; } $r6 = mysqli_query($mysqli, $q6);In case of an autoresponder, query the table ares_emails for $title instead of the table campaign.
I have little experience with PHP but as l.php already does a check on autoresponder with: if($ares_emails_id=='') I though I'd extend the code. However, it would be better to have it integrated in Sendy
The whole addition would be:
//Get the campaign title from database if($ares_emails_id=='') //if link does not belong to an autoresponder campaign { $q6 = 'SELECT title FROM campaigns WHERE id = '.$campaign_id; } else { $q6 = 'SELECT title FROM ares_emails WHERE id = '.$campaign_id; } $r6 = mysqli_query($mysqli, $q6); if ($r6) { $row = mysqli_fetch_array($r6); $title = $row['title']; if (preg_match('/\\?[^"]/', $link)) { //If the URL has other queries already, then append the Google Analytics query starting with & $link .= '&utm_source=Sendy&utm_campaign='.urlencode($title).'&utm_medium=email'; } else { //If not, so these are its first URL query, then append them starting with ? $link .= '?utm_source=Sendy&utm_campaign='.urlencode($title).'&utm_medium=email'; } }Thanks for sharing this, Cawe.
You are welcome.
My code has got an error, let me fix it. I forgot to close parenthesis here
I also forgot to URLENCODE() the title here
The final right code is the following:
//Get the campaign title from database $q6 = 'SELECT title FROM campaigns WHERE id = '.$campaign_id; $r6 = mysqli_query($mysqli, $q6); if ($r6) { $row = mysqli_fetch_array($r6); $title = $row['title']; if (preg_match('/\\?[^"]/', $link)) { //If the URL has other queries already, then append the Google Analytics query starting with & $link .= '&utm_source=Sendy&utm_campaign='.urlencode($title).'&utm_medium=email'; } else { //If not, so these are its first URL query, then append them starting with ? $link .= '?utm_source=Sendy&utm_campaign='.urlencode($title).'&utm_medium=email'; } }Sendy version 2.0 and up now supports Google Analytics natively. Custom modification of code is no longer needed.
Best regards,
Ben