I am currently developing an internal application to monitor the performance of paid search advertising.
Imagine I have several paid search campaigns directing traffic to different websites (referred to as "target sites"). When a user clicks on a paid ad and reaches the target site, a cookie is created using JavaScript. If the cookie is detected (indicating they came from AdWords), I replace the default phone number on the page with a tracking number.
To achieve this, I include a JS file from my Rails app on the target site like this:
<script src="http://www.myapp.com/assets/application.js" type="text/javascript"></script>
Key parts of the JS script:
if(window.location.href.indexOf("gclid") > -1) {
document.cookie = "ppc=adwords; path=/";
}
function getCookie(name) {
var nameEQ = name + '=';
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1, c.length);
}
if (c.indexOf(nameEQ) == 0) {
return unescape(c.substring(nameEQ.length, c.length));
}
}
return "";
}
var value = getCookie('ppc');
if (value != '') {
// User has the cookie, indicating they came from gclid
alert("this user has the ppc cookie!");
var testswap = function(){
var swap = document.getElementById("test").textContent;
document.getElementById("test").textContent = "New Number Insert";
}
document.addEventListener("DOMContentLoaded", testswap);
} else {
alert("user doesn't have cookie");
// User does not have the cookie, meaning they came from elsewhere
}
I keep each account's paid search number in the app's database. How can I best access it to replace the number in this line from the above script
document.getElementById("test").textContent = "New Number Insert from app's database";
I initially thought using the gon gem would solve this, but I realized it cannot access the controller-set variables on the target site (I understand now that this might not be feasible, even after referencing the JS file on the target site). Should I consider using AJAX?
Disclaimer: I am relatively new to programming, so I apologize if this explanation could have been clearer. I also acknowledge that the code may not be optimal, which could complicate the resolution.
Any assistance would be highly appreciated!