question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using Google Adsense in a Preact-CLI project

See original GitHub issue

Hi guys!

I have an issue and I’ve spent A LOT OF TIME trying to solve this. I am trying to add the Google Adsense code to my project. My status stays stuck at: “Your site needs work”. The additional information explains that Google can’t find the code. Eventhough I’ve added it (in multiple ways).

What I’ve found out is that everytime this code: <script data-ad-client="ca-pub-#####" async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>

gets transformed to this code: <script data-ad-client="ca-pub-#####" async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js" data-checked-head="true"></script>

This is how I’m trying to add the code:

Option 1: In my app.js file

componentDidMount() {
        //SetAdsense
        let script = document.createElement('script');
        script.setAttribute('data-ad-client','ca-pub-########' );
        script.setAttribute('async');
        script.src = "https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js";           
        document.getElementsByTagName('head')[0].appendChild(script);
}

Option 2: Using Helmet

<Helmet>
                <script data-ad-client="ca-pub-#####" async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
</Helmet>

Any idea how I can make Google see the provided Adsense code in my Head?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
developitcommented, Jul 21, 2020

(we discussed elsewhere but I’ll echo here for others)

In 2.2.1, the --template argument was relative to the src directory. Here’s how to use it:

package.json:

{
  "scripts": {
    "dev": "preact watch --template template.html",
    "build": "preact build --template template.html"
  }
}

src/template.html:

<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="utf-8">
		<title><%= htmlWebpackPlugin.options.title %></title>
		<meta name="viewport" content="width=device-width,initial-scale=1">
		<meta name="mobile-web-app-capable" content="yes">
		<meta name="apple-mobile-web-app-capable" content="yes">
		<link rel="manifest" href="<%= htmlWebpackPlugin.files.publicPath %>manifest.json">
		<% if (htmlWebpackPlugin.options.manifest.theme_color) { %>
			<meta name="theme-color" content="<%= htmlWebpackPlugin.options.manifest.theme_color %>">
		<% } %>
		<% for (var chunk of webpack.chunks) { %>
		 <% if (chunk.names.length === 1 && chunk.names[0] === 'polyfills') continue; %>
			<% for (var file of chunk.files) { %>
				<% if (htmlWebpackPlugin.options.preload && file.match(/\.(js|css)$/)) { %>
					<link rel="preload" href="<%= htmlWebpackPlugin.files.publicPath + file %>" as="<%= file.match(/\.css$/)?'style':'script' %>">
				<% } else if (file.match(/manifest\.json$/)) { %>
					<link rel="manifest" href="<%= htmlWebpackPlugin.files.publicPath + file %>">
				<% } %>
			<% } %>
		<% } %>
	</head>
	<body>
		<%= htmlWebpackPlugin.options.ssr({
			url: '/'
		}) %>
		<script defer src="<%= htmlWebpackPlugin.files.chunks['bundle'].entry %>"></script>
		<script>window.fetch||document.write('<script src="<%= htmlWebpackPlugin.files.chunks["polyfills"].entry %>"><\/script>')</script>
	</body>
</html>
1reaction
ForsakenHarmonycommented, Jul 21, 2020

https://github.com/preactjs-templates/netlify/blob/master/template/src/template.html If you update to preact-cli v3 it should generate the template in your project dir you can insert stuff into

Read more comments on GitHub >

github_iconTop Results From Across the Web

Implementing Google Adsense inside a p(react) project using ...
I am trying to add the Google Adsense code to my Preact project which works the same as React. My status stays stuck...
Read more >
Preact CLI
Preact CLI takes the pain out of starting a new project, getting you up and running instantly with a minimal and understandable project...
Read more >
PreactCasts #1 - Preact CLI - YouTube
PreactCasts #1 - Preact CLI. ... Build Your Own Stock Portfolio Tracker On Google Sheets. Think Stocks. Think Stocks.
Read more >
React Component for Google Ads – Adam Gamboa G
For this purpose, we can add a third-party library to the project that already provides support for google ads and use its components....
Read more >
When a DevOps Engineer Enters the JAMStack — Functional ...
The concept of Preact habitat is pretty straight forward and can be summarised as : > Scan the DOM when the widget's script...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found