-
Notifications
You must be signed in to change notification settings - Fork 69
Link GA tracking example
Gorrie Coe edited this page Jul 8, 2016
·
1 revision
Create a file named CustomLink.php in mysite/code
<?php
class CustomLink extends DataExtension
{
private static $db = array(
'TrackingLabel' => 'varchar'
);
public function updateCMSFields(FieldList $fields)
{
$fields->addFieldsToTab(
'Root.Main',
array(
TextField::create(
'TrackingLabel',
'Tracking label'
)
)
);
return $fields;
}
public function getTrackingAttr()
{
if($this->owner->TrackingLabel){
return " data-ga-label='$this->owner->TrackingLabel' ";
}
}
In your config.yml
...
Link:
extensions:
- CustomLink
...
Create a file named Link.ss in theme directory
<a href='{$LinkURL}'{$TargetAttr}{$ClassAttr}{$TrackingAttr}>
{$Title}
</a>
Add the following script in your theme js
$(document).ready(function() {
$('[data-ga-label]').each(function(){
$(this).on('click', function() {
var $trackingName = $(this).data('ga-label');
ga('send', 'event', 'button', 'click', $trackingName);
});
});
});