Configure Google Analytics to View Pages

I am trying to get a homepage with tons of content in Isotope

to show the hash change as a pageview in Google Analytics. Originally, I was going to do this as events, but it really should be pageviews.

So, I'm setting up a modified GA:

(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');

ga('create', 'UA-XXXXXXXX-X', {'allowAnchor': true});
ga('send', 'pageview', { 'page': location.pathname + location.search + location.hash});

In Google Analytics, I see a hash tag if someone is sent to a specific URL; Example: http://www.example.com/#pet-health If they reload the page, I see this hash in GA, but not if they click on the "nav" Isotope link to get to it. If they click, I just see a "/"

In isotope shooting, what doesn't seem to work for me:

//Sets up filtering on click of Isotope navigational elements 
    $('#isotopeFilters a, .subnav a, #isotopeContainer .isotopeNav a, .page-template-page-home-php #logo').click(function(){
        var selector = $(this).attr('data-filter');
        var prettyselector = selector.substr(1);
        ga('send', 'pageview', location.pathname+location.search+location.hash);

        location.hash = prettyselector;

        $('#isotopeFilters a, .subnav a').removeClass('active');
        $('a[class="' + prettyselector + '"]').addClass('active');

        $container.isotope({ 
            filter: selector,
            itemSelector: '.item',
            masonry: {
                columnWidth: 270
            },
            animationOptions: {
            duration: 750,
            easing: 'linear',
            queue: false,
        }
      });
      return false;
    });

I thought this line in the click function would do the trick:

ga('send', 'pageview', location.pathname+location.search+location.hash);

- ?

//Fires Isotope functionality when hash/URL changes
    $(window).hashchange( function(){
        if(location.hash!=''){
            var hashfilter = '.' + location.hash.substr(1);
        }else{
            var hashfilter = '.home';
        }

        $container.isotope({
            filter: hashfilter,
            itemSelector: '.item',
            masonry: {
                columnWidth: 270
            },
            animationOptions: {
                duration: 750,
                easing: 'linear',
                queue: false,
           }
        });
        isotopeSubNav();
    });

    if(location.hash!=''){
        var hashfilter = '.' + location.hash.substr(1);
        ga('send', 'pageview', location.pathname+location.search+location.hash);
        $(hashfilter).addClass('active');
    }

, , , , hashchange .

+4
4

, GA, :

ga('send', 'pageview', {'page': location.pathname+location.search+location.hash});

: https://developers.google.com/analytics/devguides/collection/analyticsjs/advanced?hl=en#fieldObject

+9

page pageview Google:

send , . , , - . , (, ), , , .

:

ga('set', 'page', location.pathname+location.search+location.hash);
ga('send', 'pageview');

Google Analytics .

+5

- Google:

(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', 'https://www.google-analytics.com/analytics.js', 'ga');

    ga('create', 'UA-XXXXXXXX-X', 'auto');
    ga('send', 'pageview', {'page': location.pathname + location.search + location.hash});
    window.addEventListener("hashchange", function (event) {
        ga('set', 'page', location.pathname + location.search + location.hash);
        ga('send', 'pageview');
    })
+1

Google Analytics Google gtag, ga, , "ga ". , Google Analytics :

<script async src="https://www.googletagmanager.com/gtag/js?id=YOUR-GA-ID"></script>
<script>
     window.dataLayer = window.dataLayer || [];
     function gtag() {
         dataLayer.push(arguments);
     }
     gtag('js', new Date());
     gtag('config', 'YOUR-GA-ID', {
         'page_path': location.pathname + location.hash
     });
 </script>

, URL (/ ) javascript, - :

window.addEventListener("hashchange", function (event) {
       gtag('event', 'pageview', {
         'page_path': location.pathname+location.search+location.hash
       });
});

https://developers.google.com/analytics/devguides/collection/gtagjs/sending-data

https://developers.google.com/gtagjs/reference/event

0

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


All Articles