1

Ran into an issue where I need to use GET vs POST on a form method, but GATC cookie data is not being appended to the URL correctly, because the form's data is trumping Google's GATC data (using linkByPost).

I've read up on a potential solution posted here, but seems like an insane amount of work to make GET behave. I also stumbled upon another solution here, but IE doesn't respect anything after the 'anchor' portion of the url.

Anyone have any other ideas? If I can't handle this via JS, I will have to go into the script handling the form action and massage the querystring manually (assuming that GATC data is in $_REQUEST array). FTR, GATC data is not available via the $_REQUEST array, when using get.

asked Mar 27, 2013 at 21:58
2
  • Try to use one of these methods to get the query string of your form and use _link(). Commented Mar 27, 2013 at 22:17
  • Thanks for the suggest, but I needed to localize the GATC data before I could build out querystring using your suggestion. Not sure why there is a vote to close, as the marked duplicate did not answer my question. Commented Mar 27, 2013 at 23:47

2 Answers 2

2

For future reference, in case anyone runs into the same issue, this is the solution I implemented. I lifted some code from the answer to this SO post, and combined it with the idea behind this post, where it localizes the GATC data, and adds hidden fields to the form for each one.

Resulting code:

$(document).ready(function() {
 $('#formId').submit(function(e) {
 try {
 e.preventDefault();
 var form = this;
 if (typeof _gat !== 'undefined') {
 _gaq.push(['_linkByPost', this]);
 var pageTracker = _gat._getTrackerByName();
 var url = pageTracker._getLinkerUrl(form.action);
 var match = url.match(/[^=&?]+\s*=\s*[^&#]*/g);
 for ( var i = match.length; i--; ) {
 var spl = match[i].split("=");
 var name = spl[0].replace("[]", "");
 var value = spl[1];
 $('<input>').attr({
 type: 'hidden',
 name: name,
 value: value
 }).appendTo(form);
 }
 }
 setTimeout(function() { form.submit(); }, 400);
 } catch (e) { form.submit(); }
 });
});
answered Mar 27, 2013 at 23:43
Sign up to request clarification or add additional context in comments.

3 Comments

A good answer, thank you - though var spl = match[i].split("="); does not account for tag values which could contain more than one "=", i.e. more than one part. The value should also be encoded to account for this eventuality (to preserver the name-value pairs): var str = match[i]; var index = str.indexOf("="); var name = str.substr(0, index); var value = encodeURIComponent(str.substr(index + 1, str.length - 1));
Agreed, the url regex is expecting k/v pairs with no nesting, are you saying that your issue had something like posting an array of values?
Yes, exactly that - the utm tags (which I was interested in) are sometimes nested, so it was necessary for me to use the previously mentioned extension to pass the correct number of parameters. Thanks again though, otherwise I might never have been able to obtain all of the utm tags :)
0

You can use jQuery serialize to get the form's elements, then _getLinkerUrl to append the cross-domain tracking data

$('#formID').submit(function(e) {
 var pageTracker = _gat._getTrackerByName();
 var url = this.action + '?' + $(this).serialize();
 url = pageTracker._getLinkerUrl(url);
 if (this.target != '_blank') location.href = url;
 else window.open(url);
});
answered Mar 28, 2013 at 14:15

1 Comment

Thanks for the suggestion, but I prefer not to change the behavior of the .submit() functionality. In my specific case I have two .submit() function calls linked to the same formId, fortunately jQuery is able to namespace it so one submit will link to the other. This allows me to keep various JS snippets decoupled from one another. If I change my submit() to not call .submit() and someone adds another .submit() function later on, it may never be realized.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.