Skip to content

Commit

Permalink
Merge pull request #46 from soranoo/master
Browse files Browse the repository at this point in the history
Added Publisher ID Verification
  • Loading branch information
btk authored Oct 20, 2023
2 parents 03ca15b + cc4dabb commit 7c9830e
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions src/components/GoogleAdSense.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import React from "react";
import Script, { ScriptProps } from "next/script";

const PUBLISHER_ID_REGEX = /^pub-\d{16}$/;

type GoogleAdSenseProps = {
publisherId: string;
strategy?: ScriptProps["strategy"];
Expand All @@ -10,23 +12,32 @@ type GoogleAdSenseProps = {
export function GoogleAdSense({
publisherId,
strategy = "afterInteractive",
debug = false
debug = false,
}: GoogleAdSenseProps): JSX.Element | null {
const _publisherId =
process.env.NEXT_PUBLIC_ADSENSE_PUBLISHER_ID ?? publisherId;
process.env.NEXT_PUBLIC_ADSENSE_PUBLISHER_ID ?? publisherId;

if(!_publisherId){
// TODO: check the format of publisherId here, some peeps new to adsense might enter a wrong id here.
console.error("nextjs-google-adsense: publisherId can't be empty for GoogleAdSense component");
if (!_publisherId) {
console.error(
"nextjs-google-adsense: publisherId can't be empty for GoogleAdSense component"
);
return null;
} else if (!PUBLISHER_ID_REGEX.test(_publisherId)) {
console.error(
"nextjs-google-adsense: publisherId is not in the correct format. It should be like this: pub-xxxxxxxxxxxxxxxx, there is a total of 16 digits behind pub-"
);
return null;
}

return (
<>
<Script
id="nextjs-google-adsense"
src={`https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-${_publisherId}${debug ? `google_console=1`: ``}`}
strategy={strategy} />
src={`https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-${_publisherId}${
debug ? `google_console=1` : ``
}`}
strategy={strategy}
/>
</>
);
}

0 comments on commit 7c9830e

Please sign in to comment.