I'm doing some conversion and revenue tracking using Virtual Website Optimizer. To track revenues, they instruct you to add a piece of Javascript code on the thank you page to determine the actual revenue earned.
<script type="text/javascript">
var _vis_opt_revenue = 0;
//zero should be replaced by the actual revenue figure
</script>
I'm using a Wufoo form and made it so that I can add a URL parameter that totals up their order, so for example if their order has a total of $280 they'll be sent to:
http://mysite.com/thank-you?total=280
I'm wondering: how can I make it so that URL parameter is inserted into the Javascript tracking code from Visual Website Optimizer?
Thanks in advance.
Function taken from here
Add this
function getParameterByName(name)
{
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regexS = "[\\?&]" + name + "=([^&#]*)";
var regex = new RegExp(regexS);
var results = regex.exec(window.location.search);
if(results == null)
return "";
else
return decodeURIComponent(results[1].replace(/\+/g, " "));
}
Then change
var _vis_opt_revenue = 0;
to
var _vis_opt_revenue = getParameterByName("total");
Try this
var _vis_opt_revenue =
'$' + window.location.pathname.split('?')[1].split('=')[1];
Related
using jQuery; to add something to a url after the last /
for example add sale to:
/gender/category/brand/
so it becomes:
/gender/category/brand/sale
However due to the way the URL's are generated and built I can't just always say 'add it to the end of a URL' as there are sometimes ?query strings on the end for example:
/gender/category/brand/?collection=short&colour=red
I just can't figure out how I can add sale after the final / and always before a ?query string if one exists.
Searching through stackoverflow I've seen some bits about extracting content after the last / but not this, is this possible? I really would appreciate help getting this sorted.
EDIT - The solution
Thanks too all for your help but I was able to adapt Shree's answer the easiest to get this which did what I needed:
if(window.location.href.indexOf("sale") > -1) {
} else {
var raw = window.location.href;
var add = 'sale';
var rest = raw.substring(0, raw.lastIndexOf("/") + 1);
var last = raw.substring(raw.lastIndexOf("/") + 1, raw.length);
var newUrl = rest + add + last;
window.location.href = newUrl;
}
Use substring with lastIndexOf.
var raw = '/gender/category/brand/?collection=short&colour=red';
var add = 'sale';
var rest = raw.substring(0, raw.lastIndexOf("/") + 1);
var last = raw.substring(raw.lastIndexOf("/") + 1, raw.length);
var newUrl = rest + add + last;
console.log(newUrl);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
In vanilla javascript
var a = "/gender/category/brand/?collection=short&colour=red";
var lastIndexPosition = a.lastIndexOf('/');
a = a.substring(0,lastIndexPosition+1)
+"sale"
+a.substring(lastIndexPosition+1 , a.length);
console.log(a);
By using a reusable function in Javascript:
You can use lastIndexOf and get the last '/' index position and append your new data there.
The lastIndexOf() method returns the position of the last occurrence
of a specified value in a string.
Using this you can send any parameter into function there by it is reusable.
function insert(main_string, ins_string, pos) {
return main_string.slice(0, pos) + ins_string + main_string.slice(pos);
}
var url = "/gender/category/brand/?collection=short&colour=red"
url = insert(url, 'sale', url.lastIndexOf("/") + 1)
console.log(url)
Here is a working DEMO
An alternative, use .split("?") to separate at the ? then combine them back, eg:
// Example with querystring
var url = '/gender/category/brand/?collection=short&colour=red'
var parts = url.split("?");
var newurl = parts[0] + "sale" + "?" + (parts[1]||"")
console.log(newurl)
// Test without querystring
var url = '/gender/category/brand/'
var parts = url.split("?");
var newurl = parts[0] + "sale" + (parts[1]||"")
console.log(newurl)
The (parts[1]||"") handles the case where there isn't a querystring.
i have builded a java script code(here he is) :
function gup(name) {
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regexS = "[\\?&]" + name + "=([^&#]*)";
var regex = new RegExp(regexS);
var results = regex.exec(window.location.href);
if (results == null)
return "";
else
return results[1];
}
this code gets the permart from the head of the url and puts it into a span, now the problem is when i do "Something like that" its happend to get like that :
%22hey%20hey%22..
what can i do?
EDIT1:
I mean its takes parameters like that www.google.co.il?per="grg" and when i do www.google.co.il?per="Hey Hey" its like that %22hey%20hey%22.
try with
return decodeURIComponent(results[1]);
You will get decoded string (Readable string in the sense)
I'd like to change the state of the .hide() function based on a URL string or something? Basically I have the following script which shows a sign up form and when you click login the login form displays and the sign up form disapears. I'd like to give a link to current users that shows the login form first. Is there a way to do that with a url string? Or am I stuck?
I have to use js/jquery.
$(function () {
$("#oo-loginContainer").hide();
$(".link1, .link2").bind("click", function () {
$("#form-content, #oo-loginContainer").hide();
if ($(this).attr("class") == "link1"){
$("#form-content").show();
} else {
$("#oo-loginContainer").show();
}
});
});
You absolutely can do this. Many frameworks support the HTML5 history api. History.js is an example.
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regexS = "[\\?&]" + name + "=([^&#]*)";
var regex = new RegExp(regexS);
var results = regex.exec(window.location.href);
if (results == null)
return "";
else
return decodeURIComponent(results[1].replace(/\+/g, " "));
}
Let's assume that the URL is www.google.com?foo=bar
That would give you:
console.log(getParameterByName('foo')); //bar
Is this what you're asking for?
When enter/writing a name in a inputfield (id=btnCloseDeal), and after that I click my GoToPayBtn Im coming to a new page. On my new page I wanna get the value from my inputfield (id=btnCloseDeal) and display it. What should I write in my jQuery function to get the value from my inputfield (id=btnCloseDeal) and display it on another page?
<input type="button" class="artikel" id="btnCloseDeal" value="Pay" onclick="check();";"/>
$("#btnCloseDeal ").click(function (){}
As I understand your question, the way you are thinking of implementing it is not optimal.
I would suggest using your field (#btnCloseDeal) as a query parameter and then fetching it with jQuery.
Here is a function that will get you a query-param in pure JS
function getParameterByName(name)
{
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regexS = "[\\?&]" + name + "=([^&#]*)";
var regex = new RegExp(regexS);
var results = regex.exec(window.location.search);
if(results == null)
return "";
else
return decodeURIComponent(results[1].replace(/\+/g, " "));
}
you can set the value of the input to the url of a page you want to redirect to, then get the url parameter with javascript on that page:
// get the url
var url = window.location.protocol + "//" + window.location.host;
and use:
var param = location.search;
I would like to insert a parameter value found the url into my javascript code.. So basically i have the following url www.rene-zamm.com/mp-dthanks.asp?gglid=123123123
Now i have the following javascript code in the same page and i want that number in the url to be visible also in the javascript code below:
var gwoTracker=_gat._getTracker("UA-1639156-3");
gwoTracker._trackPageview("/**NUMBER OF URL HERE**/goal");
Can someone help me please?
You need to parse the querystring. Javascript has no inbuilt way of doing this easily, but there are some nice functions you can use:
function gup( name )
{
name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regexS = "[\\?&]"+name+"=([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec( window.location.href );
if( results == null )
return "";
else
return results[1];
}
http://www.netlobo.com/url_query_string_javascript.html
Then in your example you could do:
var queryData = gup("gglid");
var gwoTracker=_gat._getTracker("UA-1639156-3"); gwoTracker._trackPageview("/" + queryData + "/goal");