简体   繁体   中英

Track domain from subdomain (Google analytics)

I've added this code on a subdomain of my domain to track some pages but I don't know why it doesn't work.

I would like to mention that the tracked pages are on the domain, not on the subdomain.

<script type="text/javascript">
window.onload = function () {
    var _gaq = _gaq || []; 
    _gaq.push(['_setAccount', 'UA-15318659-1']); 
    _gaq.push(['_setDomainName', 'savoyhotel.ro']); 
    var hdStep = parseInt(document.getElementById('hdStep').value); 
    switch (hdStep) { 
        case 1: { _gaq.push(['_trackPageview', '/alege-data']); if(document.location.hash == '#test-lucian-20130401') { alert('alege-data') } break; }
        case 2: { _gaq.push(['_trackPageview', '/alege-camera']); if(document.location.hash == '#test-lucian-20130401') { alert('alege-camera') } break; }
        case 3: { _gaq.push(['_trackPageview', '/date-personale']); if(document.location.hash == '#test-lucian-20130401') { alert('date-personale') } break; }
        case 4: { _gaq.push(['_trackPageview', '/finalizare-fara-garantare']); if(document.location.hash == '#test-lucian-20130401') { alert('finalizare-fara-garantare') } break; }
        case 5: { _gaq.push(['_trackPageview', '/finalizare']); if(document.location.hash == '#test-lucian-20130401') { alert('finalizare') } break; }
    } 
    (function() { var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); })();

}
</script>

Anybody have any idea?

You will need to track your base domain '.savoyhotel.ro' (notice the extra . At the beginning), and inside analytics set up rules to filter domains and subdomains. For example on some of our sites we have filters in analytics which permit us to display traffic for main domain, sub domain, and combined, while using a singular analytics tag.

On iPad right now so a bit of a pain to elaborate but if no one posts a more detailed answer ill post the steps to follow in a few hours when I get home to my pc

But it basically boils down to what is said here Setting up sub domain include filter in google analytics


Update:

1st, switch back to using normal analytics code, and add the . to the front of the domain

<script>
    var _gaq = _gaq || [];
    _gaq.push(['_setAccount'   , 'UA-15318659-1']);
    _gaq.push(['_setDomainName', '.savoyhotel.ro']);
    _gaq.push(['_trackPageview']);

    (function() {
        var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
        ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
        var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
    })();
</script>

Now if you like create a little wrapper function to facilitate calling trackPageView from you code whenever you need to

function trackPageView(page) {
    window._gaq = window._gaq || [];
    window._gaq.push(['_trackPageview', page]);
}

This is the only thing you need to track ALL pageviews from your main domain + subdomains

Now if you like you can setup profiles in analytics to have 1 profile that only shows analytics from you main domain, and another profile from your subdomains, and thats where Setting up sub domain include filter in google analytics come in.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM