Integrating Google Adwords tracking with Contact Form 7

Posted in wordpress

In order to integrate the javascript for Google Adwords tracking with Contact Form 7 – I’ve pieced together some advice from the thread in this link: http://wordpress.org/support/topic/plugin-contact-form-7-contact-form7-google-adwords-conversion-code.

This is for multiple forms. You only need to add one function if you have one form.

In your Contact 7 “Additional Settings” at the bottom of the page for each form you put the function you want to use. You use a different naming for each form you want to track and can name it whatever you want.

on_sent_ok: "tracking1();"

on_sent_ok: "tracking2();"

on_sent_ok: "tracking3();"

on_sent_ok: "tracking4();"

on_sent_ok: "tracking5();"

But now you need to create a tracking.js file (or whatever you want to name it) and put the following code there five times changing the name and the tracking data provided by Google Adwords.

Be sure to update the values for “conversion ID” and “label”.

function tracking1(){
    var img = document.createElement("img");
    var conversionId = 1234567890;
    var randomNum = new Date().getMilliseconds();
    var value = 0;
    var label = "LabDKV4jdieAo7_v5wH";
    var url = encodeURI(location.href);
    var googleUrl = "http://www.googleadservices.com/pagead/conversion/"+conversionId+"/?random="+randomNum+"&value="+value+"&label="+label+"&guid=ON&script=0&url="+url;
    img.src = googleUrl;
    document.body.appendChild(img);
}

var trackUrl = "http://www.googleadservices.com/pagead/conversion/"+goalId+"/?random="+randomNum+"&value="+value+"&label="+label+"&guid=ON&script=0&url="+url;
img.src = trackUrl;
document.body.appendChild(img);
}
function tracking2(){
    var img = document.createElement("img");
    var conversionId = 1234567890;
    var randomNum = new Date().getMilliseconds();
    var value = 0;
    var label = "LabDKV4jdieAo7_v5wH";
    var url = encodeURI(location.href);
    var googleUrl = "http://www.googleadservices.com/pagead/conversion/"+conversionId+"/?random="+randomNum+"&value="+value+"&label="+label+"&guid=ON&script=0&url="+url;
    img.src = googleUrl;
    document.body.appendChild(img);
}

var trackUrl = "http://www.googleadservices.com/pagead/conversion/"+goalId+"/?random="+randomNum+"&value="+value+"&label="+label+"&guid=ON&script=0&url="+url;
img.src = trackUrl;
document.body.appendChild(img);
}

and so on…

Once you have this done you simply need to include the .js file in your header.php.

<?php include ‘tracking.js’; ?>

header.php is called on every page of your website so this one include will work for your entire site.