Internal Campaign Tracking. Adding parameters dynamicaly. Code Example

Back to "Better Internal Campaign Tracking"

Internal Campaign Attribute detected (not blank): adds parameter

 <a href="" data-intcmp="spring-sale-10off">

Internal Campaign Attribute detected (not blank): appends parameter

 <a href="" data-intcmp="spring-sale-10off">

No Internal Campaign Attribute detected (blank): no parameter added

 <a href="" data-intcmp="">

No Internal Campaign Attribute detected: no parameter added

 <a href=""">


When you click in a link this JavaScript snippet does the following:

<script type="text/javascript">
$(document).ready(function() {
var $target = $(this);
var Url = $target.attr('href');
var sign = (Url.indexOf('?') > -1) ? '&' : '?';
var campaign = $target.attr('data-intcmp');
$target.attr('href', Url + sign + 'intcmp=' + campaign);
  1. Looks for 'intcmp' attribute
  2. If there is an intcmp attribute and it has a value, grabs that value
  3. Grabs whatever is in the href of the link
  4. Concatenates all conveniently using '?' or '&' if there are already some other parameters
  5. Replaces the original href value with the new one including the internal campaign tag