Skip to content

Instantly share code, notes, and snippets.

@osvik
Last active January 29, 2017 22:29
Show Gist options
  • Save osvik/b6d183c9c9b33bbc04514c1e3dadc0ae to your computer and use it in GitHub Desktop.
Save osvik/b6d183c9c9b33bbc04514c1e3dadc0ae to your computer and use it in GitHub Desktop.
Class to create a GA email tracking pixel
<?php
/**
* @file Class to create a Google Analytics email tracking pixel.
* It will be used to integrate our html email generator "Fabricador"
*/
class EmailTrack {
public $GAtrackingID = 'UA-7467053-1'; // Replace by your Google Analytics Tracking ID
public $supporterID = '{user_data~supporter_id}'; // Engaging Networks tag to insert user ID. Shoud be a tag that's unique for every user. You can't use information that allows others to identify the user. (No phone, email, full name, id number...)
public $medium = 'email'; // Utm medium
public $category = 'email'; // Analytics event category
public $action = 'open'; // Analytics event action
/**
* Creates a new email pixel with all defaults
* @private
* @param string $subject Subject of the email
* @param string $source Utm source that identifies the segment you are emailing too
* @param string $campaign Utm campaign that identifies the main campaign of the email
*/
function __construct( $subject = 'EmptySubject', $source = 'DefaultSource', $campaign = 'DefaultCampaign') {
$this->subject = $subject;
$this->source = $source;
$this->campaign = $campaign;
}
/**
* Calculates the path from some object proprieties
* @return string Path string
*/
private function getPath() {
return '%2F' . urlencode($this->source) . '%2F' . urlencode($this->campaign) .'%2F' . urlencode($this->subject);
}
/**
* Returns a pageview URL pixel
* @return string URL of the pixel
*/
public function getPageviewUrl () {
$urlPixel = 'https://www.google-analytics.com/collect?v=1'
. '&tid=' . $this->GAtrackingID
. '&t=pageview'
. '&dp=' . $this->getPath()
. '&dt=' . urlencode($this->subject)
. '&cn=' . urlencode($this->campaign)
. '&cs=' . urlencode($this->source)
. '&cm=' . urlencode($this->medium)
. '&cc=' . urlencode($this->subject)
. '&cid=' . $this->supporterID;
return $urlPixel;
}
/**
* Returns an event URL pixel
* @return string URL of the pixel
*/
public function getEventUrl () {
$urlPixel = 'https://www.google-analytics.com/collect?v=1'
. '&tid=' . $this->GAtrackingID
. '&t=event'
. '&ec=' . urlencode($this->category)
. '&ea=' . urlencode($this->action)
. '&cn=' . urlencode($this->campaign)
. '&cs=' . urlencode($this->source)
. '&cm=' . urlencode($this->medium)
. '&cc=' . urlencode($this->subject)
. '&cid=' . $this->supporterID;
return $urlPixel;
}
/**
* Transforms an url segment in an image tag
* @param string $urlPixel URL of the pixel
* @return string Html image tag to put in the email
*/
public static function getImageTag ($urlPixel) {
return '<img src="' . $urlPixel . '" />';
}
/**
* Transforms an url segment in an image tag, to display the html
* @param string $urlPixel URL of the pixel
* @return string Html image tag to put in the email
*/
public static function getHtmlImageTag ($urlPixel) {
return '&lt;img src="' . $urlPixel . '" /&gt;';
}
}
?>
<?php
require_once('emailTrack.php');
// INSTANTIATE PAGEVIEW TACKER
// Email subject, traffic source (utm), campaign (utm)
// For the Spanish office traffic source can be newsletter-socios or newsletter-leads
$myPageviewTracker = new EmailTrack('Add email subject here', 'newsletter-socios', 'Campaign');
// Get a URL with the pageview type
$urlPixelPageview = $myPageviewTracker->getPageviewUrl();
// Get a visible html tag with the URL
$imageTag = EmailTrack::getHtmlImageTag($urlPixelPageview);
// INSTANTIATE EVENT TACKER
// Email subject, traffic source (utm), campaign (utm)
// For the Spanish office traffic source can be newsletter-socios or newsletter-leads
$myEventTracker = new EmailTrack('Add email subject here', 'newsletter-socios', 'Campaign');
// Get a URL with the pageview type
$urlPixelEvent = $myEventTracker->getEventUrl();
// Get a visible html tag with the URL
$imageTagOther = EmailTrack::getHtmlImageTag($urlPixelEvent);
echo("<html>\n\n<head><meta charset='utf-8' /></head>\n\n<body>\n\n");
// DISPLAY PAGEVIEW TRACKER
echo("<pre>\n\n");
echo("URL of the pageview pixel: \n");
print_r($urlPixelPageview);
echo ("\n\n");
echo("Html tag with the pageview pixel: \n");
print_r($imageTag);
echo ("\n\n");
echo("</pre>");
// DISPLAY EVENT TRACKER
echo("<pre>\n\n");
echo("URL of the event pixel: \n");
print_r($urlPixelEvent);
echo ("\n\n");
echo("Html tag with the event pixel: \n");
print_r($imageTagOther);
echo ("\n\n");
echo("</pre>");
echo("\n\n</body>\n</html>");
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment