Internal Campaign Tracking. Adding parameters dynamicaly. Code Example

Back to "Better Internal Campaign Tracking"

Internal Campaign Attribute detected (not blank): adds parameter

 <a href="http://dynamical.biz/blog/upload/file/intern-camp-landing.html" data-intcmp="spring-sale-10off">

Internal Campaign Attribute detected (not blank): appends parameter

 <a href="http://dynamical.biz/blog/upload/file/intern-camp-landing.html?cfm=456" data-intcmp="spring-sale-10off">

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

 <a href="http://dynamical.biz/blog/upload/file/intern-camp-landing.html" data-intcmp="">

No Internal Campaign Attribute detected: no parameter added

 <a href="http://dynamical.biz/blog/upload/file/intern-camp-landing.html"">

 

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

<script type="text/javascript">
$(document).ready(function() {
$('[data-intcmp][data-intcmp!=""]').click(function(i){
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);
});
});
</script>
  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