⬆️ ⬇️

Transfer of Google Analytics tracks to third-party domains without javascript





What is the article about?





Why transmit?



In order not to lose information about the user during his transition to the payment system for making purchases, as well as his trips to our ecosystem of sites.



Why without javascript?





The essence of the method is simple. We take all __utm cookies and pass them, as parameters, in the URL to another domain. Nothing happens. Disagree. Add the last parameter __utmk, which is a special hash of all other Google analytics cookies. Congratulations, we did what the _link function does in the well-known Google Analytics ga.js file.

And so, if you have a transition to other sites processed by the controller, then you do not need to add a type of construction to all links:

')

onclick="_gaq.push(['_link', 'https://www.payment-system.com?contractId=example']); return false;" 




If you need a source for generating a hash for the utmk key, please in github .

Further history of the process of obtaining this method.







1. Task received:

"Prokin Google analytics for all transitions from the site to the payment system"

Description:

“We are losing information [where I came from, what I was looking for, how I got to the buy button] about the most important users who bought our software.



Documentation analytics advises to use the construction of the form:



 onclick="_gaq.push(['_link', 'http://another-domain.com']); return false;" 


And put it on all the links that lead to another domain.

That's the problem - we have no links to the payment system. There is a basket with a few steps. And a controller that determines which payment system to use and with which parameters. Then he redirects the user to the payment system.

"Lazy ideas" come to mind. You can also make an intermediate page on which the user will click the confirmation button, with a call to the analytics function from the docks. Or javascript will form the link / form and redirect the user. But we immediately discard these ideas as flawed and go read the dock further.



A session from one domain can only be transferred, or via _getLinkerURL .




So. We are available: user click, form submit, javascript method. Only? Only! Realy?







Let's see what happens when a user clicks on a link that has an onclick attribute set.



We will search our site through a search engine, click on the link, we have data from analytics cookies:

 __utma 31500988.382379344.1389384850.1389388916.1389389624.3 __utmb 31500988.1.10.1389389624 __utmc 31500988 __utmz 31500988.1389389624.3.2.utmcsr=yandex|utmccn=(organic)|utmcmd=organic|utmctr=_ 


More about each of the cooks here .



Let's switch to the payment system through a test html link that calls _gaq.push ('link' ... described in the documentation.

Using the browser console (this is more convenient), we learn that the parameters have been added to the URL:



 __utma 31500988.382379344.1389384850.1389388916.1389389624.3 __utmb 31500988.1.10.1389389624 __utmc 31500988 __utmz 31500988.1389389624.3.2.utmcsr=yandex|utmccn=(organic)|utmcmd=organic|utmctr=_ __utmk 88022362 __utmv - __utmx - 


The cookies (already on another domain) were also set utma, utmb, utmc, and most importantly utmz, into which the value utmcsr = yandex | utmccn = (organic) | utmcmd = organic | utmctr = our_query was transferred



Then I reached a dead end, trying to transfer 4 values ​​of known cookies as parameters to the client without a client, but they stubbornly did not want to be set by analytics as tracks.

An analysis of ga.js, ga_debug.js, or rather their minification, did not produce results, an attempt to make beautifality ga.js didn’t come to anything either.



Attentive ones have already noticed that after javascript processing there appear 3 new parameters in the request:



 __utmk 88022362 __utmv - __utmx - 


I noticed it, but a little later. Having dug a little, I came across this comment to a similar article on an article on Habré.



It turned out to be on Google Analytics Tracking For Adobe Flash.

It remains the case for small. Find out that a hash is formed on the basis of all cookies and is written in __utmk and rewrite
java hash generation function:
 /** * Generate hash for input string. This is a global method, since it does not need * to access any instance variables, and it is being used everywhere in the GATC module. * @param input Input string to generate hash value on. * @return Hash value of input string. If input string is undefined, or empty, return hash value of 1. */ public function generateHash( input:String ):int { var hash:int = 1; // hash buffer var leftMost7:int = 0; // left-most 7 bits var pos:int; // character position in string var current:int; // current character in string // if input is undef or empty, hash value is 1 if(input != null && input != "") { hash = 0; // hash function for( pos = input.length - 1 ; pos >= 0 ; pos-- ) { current = input.charCodeAt(pos); hash = ((hash << 6) & 0xfffffff) + current + (current << 14); leftMost7 = hash & 0xfe00000; if(leftMost7 != 0) { hash ^= leftMost7 >> 21; } } } return hash; } 




in PHP .



Finally, we check that for the same track values ​​both options (javascript - ga.js - link () and php generateHash) generate the same key for __utmk and the required tracks are set up on the third-party domain.



Conclusion:



We have a new way to cross-domain tracking. It is not better, not worse, just different (server).

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



All Articles