Manually Add Adsense Code and Google GA4 Tag

Ben
Manually Add Adsense Code and Google GA4 Tag
Ben@49a5762e Wednesday 9th August 2023, 03:53:57

Add Adsense Code Without a Plugin

Here's an easy way to add Adsense to your forum without a plugin needed. Tried the Adsense plug-in but could not get it to work, so instead manually entered this code in the theme/bootstrap/header.tpl.php file.

This needs to be done on the web server so FTP is necessary along with a file editor. Much easier to do from a PC than a phone but it can be done on a phone as well with an FTP app.

You need to open the header.tpl.php file in an editor and paste the following code above anything else on that page. Included the ability to remove certain pages or sections from your forum from having any ads show. I excluded forum directories and also the blog because it looks better and loads much faster without ads. With this method, it will auto-place the ads in the best location, on the page, but if you notice a section or page you rather them not show, just enter the link in one of the 8 slots. You can remove any slots you don't need, just be extra careful you don't remove a "or , or it will break the code.

Recommend you edit the URL address on the last line and delete any full lines above it that you don't need instead. Finally, be sure to add the numbers of your AdSense account number to the commented-out section that starts with ***YOUR*ADSENSE*NUM*** at the end of this line there needs to be a " (quote symbol) don't accidentally overwrite or delete it when entering your account number.

Here's the Code For Adding Adsense to theme/bootstrap/header.tpl.php File

<!-- Google AdSense Code -->
<?php
$excluded_pages = [
"/flatboard/excluded-page1.php",
"/flatboard/excluded-page2.php",
"/flatboard/excluded-page3.php",
"/flatboard/excluded-page4.php",
"/flatboard/excluded-page5.php",
"/flatboard/excluded-page6.php",
"/flatboard/excluded-page7.php",
"/flatboard/excluded-page8.php"
// ... Add more excluded pages as needed
];

$current_page = $_SERVER["PHP_SELF"];

if (!in_array($current_page, $excluded_pages)) {
?>
<div class="text-center">
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-***YOUR*ADSENSE*NUM***"
crossorigin="anonymous"></script>
<!-- Insert your AdSense ad code below -->
<script>
(adsbygoogle = window.adsbygoogle || []).push({
google_ad_client: "ca-pub-***YOUR*ADSENSE*NUM***",
enable_page_level_ads: true
});
</script>
</div>
<?php
}
?>
<!-- End Google AdSense Code -->

<!-- PLACE CODE ABOVE HERE -->

<?php # Vérification si le forum est privé
// ... the rest of your header code ...
?>
<!DOCTYPE html>
<html lang="<?php echo $config['lang']?>">


How to add your Google Analytics 4 (GA4) Code

In the same file as above, right before the closing </head> copy and paste the following code. Be sure to update this with your GA_Tracking_ID number.


<!-- Google Analytics Tracking Code -->
<script async src="https://www.googletagmanager.com/gtag/js?id=YOUR_GA_TRACKING_ID"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());

gtag('config', 'YOUR_GA_TRACKING_ID');
</script>
<!-- End Google Analytics Tracking Code -->

<!-- ... other content ... -->
</div>

Last modified by Ben@49a5762e on Thursday 17th August 2023, 03:47:00

Suggested Topics
started
8.5K 0
Want to read more? Browse all categories or view latest topics.