Krishnam
Krishnam

Reputation: 57

Adding google analytics code to all pages

I want to add google analytics code to all pages in my website. Can I place google analytics code in a JS file and mention the JS file in the header?

Upvotes: 3

Views: 1805

Answers (3)

حذيفة
حذيفة

Reputation: 9

Create JS file and then include it whenever you want.
Do in your HTML pages, exactly in head tag :

<script src="googleAnalytics.js"></script>

googleAnalytics.js file :

document.head.innerHTML += `
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=########"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', '########');
</script>`;

Upvotes: 0

StringBuilder
StringBuilder

Reputation: 1629

in simple html pages there is no standard way to render script on each page , you can put ur GA code into separate .js file , and then include it on each Html page in header like

<head>
   <script src="#path-to-Code-with-GA" type="text/javascript" />
</head>

That's the only solution i can suggest :-(

Upvotes: 1

Ben
Ben

Reputation: 5414

It would be easiest if your website was run in PHP or ASP.NET. If it's in PHP, place your Google Analytics code in a file, and title it however you'd like (e.g. analytics.php or analytics.html)

Then, right before the closing </head> tag in your document, place the following code:

<?php include 'analytics.php'; ?>

Otherwise, if you run your website through a CMS such as Wordpress, use a plugin like "All in one SEO pack", or if you want to hard code it in, go to the header file (header.php in Wordpress), and simply paste your code in the head section, or use the method listed above.

Upvotes: 7

Related Questions