Google Analytics - using GET instead of POST

Including in the problem when I need to use GET vs POST for the form method, but the GATC cookie data is not added to the URL correctly because the form data skips the Google GATC data (using linkByPost).

I read the potential solution posted here , but it seems like an insane amount of work to get the GET to behave. I also stumbled upon another solution here , but IE does not respect anything after the "anchor" part of the URL.

Does anyone have any other ideas? If I can’t deal with this through JS, I will have to go to the script processing the form action and massage the request manually (provided that the GATC data is in the $ _REQUEST array). FTR, GATC data is not available through the $ _REQUEST array when using get.

+1
source share
2 answers

In the future, if someone encounters the same problem, this is the solution I implemented. I took the code from the response to this SO post and combined it with the idea of this post , where it localizes the GATC data and adds hidden fields to the form for each of them.

Final 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(); }
    });
});
+2
source

jQuery , _getLinkerUrl

$('#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);
});
0

Source: https://habr.com/ru/post/1753369/


All Articles