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.

Mike Purcell
  • 19,847
  • 10
  • 52
  • 89
  • Try to use [one of these methods](http://stackoverflow.com/questions/316781/how-to-build-query-string-with-javascript) to get the query string of your form and use `_link()`. – nwellnhof Mar 27 '13 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. – Mike Purcell Mar 27 '13 at 23:47

2 Answers2

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(); }
    });
});
Community
  • 1
  • 1
Mike Purcell
  • 19,847
  • 10
  • 52
  • 89
  • 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)); – GPMorgan Dec 10 '13 at 14:53
  • 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? – Mike Purcell Dec 10 '13 at 18:43
  • 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 :) – GPMorgan Dec 18 '13 at 10:07
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);
});
mike
  • 7,137
  • 2
  • 23
  • 27
  • 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. – Mike Purcell Mar 28 '13 at 20:03