简体   繁体   中英

Where do I put the Google Analytics snippet when I'm including a header and footer?

So Google tells me to do this:

Paste your snippet (unaltered, in it's entirety) into every web page you want to track. Paste it immediately before the closing </head> tag.

I understand this part, but as I'm including my header and footer, I guess this is not what I'm supposed to do.

If you use templates to dynamically generate pages for your site (like if you use PHP, ASP, or a similar technology), you can paste the tracking code snippet into its own file, then include it in your page header.

So I guess this is what I'm talking about(?), but I don't really get exactly where they want me to put the snippet. Do I put it in my header.php file once right before the closing </head> tag and then include the header.php file into every page or do I have to put the snippet directly in on every page? Incase I have to put the snippet in on every page, where exactly do I put it? Could someone please give me an example of this?

You can paste the Google Analytics Javascript snippet into its own file, call it ga.js for example.

Then in your header.php file, right before you close the tag, include that Javascript file, like so:

<script type="text/javascript" src="ga.js"></script>

This will load the GA code in your head section every time you load the header.php file. This should happen to every page that you load header.php automatically.

If you already have split your header, that I guess is included into you other html or php files, in it's own file and it includes the </head> tag, then you can put a script right above it. This would look something like this:

[...]
<script>
    <!-- Your personal google analytics snippet -->
</script>
</head>

Then all sites, that include your header.php, also load the snippet from analytics and get tracked.

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