Hi,

I just installed the module and I have the following code in my page:
<script><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>

Where there is an extra <script> tag...

Did I made mistake in the module configuration?

Thanks

Comments

Prez13 created an issue. See original summary.

Prez13’s picture

Issue summary: View changes
pvsureshmca’s picture

Category: Support request » Bug report
Priority: Normal » Major
StatusFileSize
new29.61 KB

Yes. I got the same issue

issue

pvsureshmca’s picture

Status: Active » Needs work
atuljaiswal1296@gmail.com’s picture

Priority: Major » Critical
Issue tags: +Getting same issue
StatusFileSize
new23.04 KB

Hi,
I am facing the same issue that is the reason google adSense is not able to verify the account. Can you please let me know when this issue will be fixed or anyone have any alternative way to fix it. Please help me.

Regards,
Atul

jcnventura’s picture

Priority: Critical » Major
Status: Needs work » Postponed (maintainer needs more info)

Can you try to replace 'script' with '' in line 61 of adsense.module and tell me if that fixes the problem?

jwillers’s picture

Yes, it fixed it for me, but now there are empty tags.

<> at the beginning and </> at the end

This puts the <> at the top of each page. But at least the ads work...

EDIT: Changing it to "meta" also lets it work and doesn't add the <> to the top of the site.
EDIT2: If you also change the tags of the previous two arrays to "meta" you remove the <> at the top of the pages when ads are disabled or not on the list as well.

nuwans’s picture

StatusFileSize
new1.92 KB

Hello,
For this issue, I m using the following patch.

nuwans’s picture

StatusFileSize
new1.89 KB
asak’s picture

Stumbled across this as well. I believe it's related to changes in D8.4 (upgraded from 8.3)

Patch #9 solves it.

tamerzg’s picture

tamerzg’s picture

Status: Postponed (maintainer needs more info) » Needs review
StatusFileSize
new2.77 KB

Attached is improved patch from #9:
- removed code related to #2948088: <> as its seperate issue with a different patch
- pagead2.googlesyndication.com/pagead/js/adsbygoogle.js is added as library instead of being attached through html_tag. html_tag is intended for inline JS code or metatags not external scripts. Also will open another issue to replace loading of pagead2 from library on other places too.
- removed adsense-managed-page-level.html.twig and adsense_managed_page_level theme declaration.

jcnventura’s picture

Status: Needs review » Needs work

Library attachment is OK, but why remove the html twig?

tamerzg’s picture

That Twig file is not used anymore. It only purpose was to add inline JS via html_tag but thats wrong, now inline JS is added proper way via html_tag.

tamerzg’s picture

Following up on our email conversation I am pasting my answer here so others could potentially be included in converstation too:

jcnventura: Please keep the twig template. PHP code should not be generating HTML (even if that HTML is Javascript).

tamerzg: As for PHP code generating HTML, issue here is that Drupal html_tag is not envisioned to add inline scripts in that way. We would need to remove script> part from twig since those tags will already be generated by html_tag so we would be left with a twig file which has only following:

(adsbygoogle = window.adsbygoogle || []).push({
  google_ad_client: "ca-{{ client }}",
  enable_page_level_ads: true
});

Which for me is bit more messy than putting it directly in #value part of render array. The later approach is also documented in https://www.drupal.org/docs/8/creating-custom-modules/adding-stylesheets... under "Inline JavaScript that affects the entire page".

skymen’s picture

Hi, guys. Replace string 61 in adsense.module '#tag' => 'script', by '#type' => 'markup',. This fix this issue.

jcnventura’s picture

Using 'inline_template' (and providing an empty template...) seems to be the best work-around for this issue.

  • jcnventura committed 356c72b on 8.x-1.x
    Issue #2929151 by NuWans, tamerzg, jcnventura, jwillers, asak, Skymen:...
jcnventura’s picture

Status: Needs work » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

VishalKumarSahu’s picture

To temporarily solve the problem I followed this approach-

<?php
function MYMODULE_page_attachments_alter(array &$attachments) {
  $remove_adsense_comments = [
    'adsense_unmatched_page',
    'adsense_ads_disabled'
  ];
  if (!isset($attachments['#attached']['html_head'])) {
    return;
  }
  foreach ($attachments['#attached']['html_head'] as $key => $value) {
    if (isset($value[1]) && in_array($value[1], $remove_adsense_comments)) {
      unset($attachments['#attached']['html_head'][$key]);
    }
  }
}
?>

And another solution is to adjust the twig templates accordingly.