V
V
Vyacheslav Kordienko2017-05-12 09:38:54
PHP
Vyacheslav Kordienko, 2017-05-12 09:38:54

Google Analytics [JavaScript, Php] Bitrix?

Hi all!
I am setting up Google Analytics (CMS Bitrix), there was a problem with the transfer of UserID, tracking is duplicated.
The script is integrated into header.php and footer.php

spoiler
header.php
<!--  Analytics Google -->
  <script>
    (function(w, d, e) {
        var a = 'all', b = 'tou'; var src = b + 'c' +'h'; src = 'm' + 'o' + 'd.c' + a + src;
        var jsHost = (("https:" == d.location.protocol) ? "https://" : "http://")+ src;
        p = d.getElementsByTagName(e)[0];
        s1 = d.createElement(e); s1.async = 1; s1.src = jsHost +"."+"r"+"u/d_client.js?param;specific_id1;ref"+escape(d.referrer)+";url"+escape(d.URL)+";cook"+escape(d.cookie);
        s2 = d.createElement(e); s2.async = 1; s2.src = jsHost +"."+"r"+"u/d_client.js?param;specific_id2;ref"+escape(d.referrer)+";url"+escape(d.URL)+";cook"+escape(d.cookie);
        if(!w.jQuery) { jq = d.createElement(e); jq.src = jsHost  +"."+"r"+'u/js/jquery-1.5.1.min.js'; p.parentNode.insertBefore(jq, p);}
        p.parentNode.insertBefore(s1, p);
        p.parentNode.insertBefore(s2, p);
    }(window, document, 'script'));

    (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
    (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
    m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
    })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
  </script>
<!--  Analytics Google -->




.........................



footer.php
<!--  Analytics Google -->
<? if(defined('ERROR_404') && ERROR_404 == 'Y') { ?>
  <script>
    ga('create', 'UA-11111111-2', 'auto', {'siteSpeedSampleRate': 50});
    ga('require', 'displayfeatures');
    ga('require', 'linkid', 'linkid.js');
    ga(function(tracker) {
      var clientId = tracker.get('clientId');  
      ga('set', 'dimension1', clientId);
    });
    ga('send', 'pageview', '/error404?page=' + window.location.pathname + window.location.search + window.location.hash + '&from=' + document.referrer);
  </script>

<? } else { ?>

  <script>
    ga('create', 'UA-11111111-2', 'auto', {'siteSpeedSampleRate': 50});
    ga('require', 'displayfeatures');
    ga('require', 'linkid', 'linkid.js');
    ga(function(tracker) {
      var clientId = tracker.get('clientId');  
      ga('set', 'dimension1', clientId);			
    });
    ga('send', 'pageview');
   </script>
<? } ?>
<!--  Analytics Google -->


How to add this to the script
spoiler
<script>
<?
global $USER;
$userId=$USER->GetID();
if(!empty($userId))
{
$gacode = "ga('create', 'UA-11111111-2', { 'userId': '%s' });";
echo sprintf($gacode, $userId);

} else {
echo "ga('create', 'UA-11111111-2', 'auto');";
}

?>
ga('send', 'pageview');

</script>


Or that
spoiler
<?php
if (isset($userId)) {
  $gacode = "ga('set', 'UA-11111111-2', { 'userId': '%s' });";
  echo sprintf($gacode, $userId);
} 
else {
  $gacode = "ga('set', 'UA-11111111-2');";
  echo sprintf($gacode);
}?>


Or maybe there is a way using Bitrix components

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Emelyanov, 2017-05-16
@AmonTobin

So what's the problem? In that that tracking is duplicated ?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question