1

A question from a complete Joomla noob, so bear with me:)

I am trying to add a piece of JavaScript code (the Adsense Auto Ads code) to the header files of a Joomla website, but my approaches seem to be suboptimal (at least ;) !).

The approaches I could come up with were:

  1. Visit every page and manually add the code (very time consuming)
  2. Paste the code somewhere in the template file...

I really do not have a thorough understanding of Joomla (or web dev in general), so I thought there should be easier/better solutions for this.

So, the question: How do I add Adsense Auto Ads (Javascript) code to the header of pages in a Joomla website?

1
  • Welcome to JSE. Please take our tour. There is no harm in asking relatively simple questions -- what is simple for some people is hard fir others. Your question is good because it is clear and you have demonstrated that you have put effort in before asking. Good job. – mickmackusa May 29 at 12:46
3

Find your templates index.php file: [httpdocs]/templates/[YOURTEMPLATE]/index.php

In the index.php file, find the <head> tag and add your snippet there using <script> tags.

Example

<head>
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<script>
(adsbygoogle=window.adsbygoogle||[]).requestNonPersonalizedAds=1;
(adsbygoogle=window.adsbygoogle||[]).push({google_ad_client: "ca-pubxxx", enable_page_level_ads: true});
</script>
</head>

Reference: https://docs.joomla.org/Adding_JavaScript

Most of the templates also offer the opportunity to add scripts via their settings, which is important for GoogleAnalytics etc.

2
  • This is a bad answer that also misrepresents the documentation. You should not hardcode script tags. Use Document API instead. – Sharky May 30 at 8:45
  • @Sharky please post an answer with your best advice or edit this answer to improve the standard (your choice). – mickmackusa May 30 at 23:27
2

In addition to the good answer by @Guitarman that specifically explains how to add script to the header in a single website's template, which is what the OP is asking for, there are other approaches that might be more suitable if you change templates often or want to handle more websites.

There are a number of extensions, mostly plugins, in the JED that can do this for you as well that would work across templates. A quick look in https://extensions.joomla.org/category/core-enhancements/coding-a-scripts-integration/?start=0 I can see the HeadTag plugin by MichaelRichley gets 5 star reviews but there are others on subsequent pages.

JoomlaShack have written this article on how to use another plugin in the JED to add JS, https://www.joomlashack.com/blog/joomla/custom-js/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.