{"id":1997,"date":"2023-08-16T12:50:01","date_gmt":"2023-08-16T05:50:01","guid":{"rendered":"https:\/\/dev.artru.io.vn\/?p=1997"},"modified":"2023-10-27T14:38:06","modified_gmt":"2023-10-27T07:38:06","slug":"delay-js-adsense-tang-diem-pagespeed-insights","status":"publish","type":"post","link":"https:\/\/artru.net\/en\/delay-js-adsense-tang-diem-pagespeed-insights\/","title":{"rendered":"Apply Delay JS technique for AdSense to increase PageSpeed Insights score"},"content":{"rendered":"<p class=\"wp-block-paragraph\">When it comes to AdSense a lot, you will be afraid to attach it to your Website. Because it affects website loading speed and reduces Google&#039;s PageSpeed Insights score.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Website rankings on search engines have also dropped since then.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Speed before optimizing AdSense<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Access <a href=\"https:\/\/pagespeed.web.dev\/\" target=\"_blank\" rel=\"noreferrer noopener\">Page Speed insights<\/a> or Lighthouse tool in Chrome browser to check Website speed when AdSense is not optimized.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"702\" height=\"858\" src=\"https:\/\/cdn.artru.eu.org\/wp-content\/uploads\/2023\/08\/PageSpeed-khi-chua-toi-uu-AdSense.png\" alt=\"PageSpeed without optimizing AdSense\" class=\"wp-image-2034\"\/><figcaption class=\"wp-element-caption\">PageSpeed without optimizing AdSense<\/figcaption><\/figure>\n<\/div>\n\n\n<h2 class=\"wp-block-heading\">AdSense delay<\/h2>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"699\" height=\"394\" src=\"https:\/\/cdn.artru.eu.org\/wp-content\/uploads\/2023\/08\/AdSense-code-in-between-the-head-tags.png\" alt=\"AdSense code in between the head tags\" class=\"wp-image-2019\"\/><figcaption class=\"wp-element-caption\">AdSense code in between the head tags<\/figcaption><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">Instead use the embed code AdSense provides. I will use the technique <code data-no-translation=\"\" data-no-auto-translation=\"\">setTimeout<\/code> in JavaScript to delay execution of the following AdSense code <strong>3.5 seconds<\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The trade-off of Delay JS is that advertising revenue will definitely decrease if the time a user views your page is less than \u201c3.5 seconds + the number of seconds it takes to load the page\u201d.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For Units ads you just need to paste the tag <code data-no-translation=\"\" data-no-auto-translation=\"\">&lt;ins&gt;<\/code> to the position to display without having to add the <code data-no-translation=\"\" data-no-auto-translation=\"\">&lt;script&gt;<\/code>.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"771\" height=\"479\" src=\"https:\/\/cdn.artru.eu.org\/wp-content\/uploads\/2023\/08\/AdSense-unit-code.png\" alt=\"AdSense unit code\" class=\"wp-image-2029\"\/><figcaption class=\"wp-element-caption\">AdSense unit code<\/figcaption><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">Here is the JavaScript code with the Delay function that executes AdSense:<\/p>\n\n\n\n<pre class=\"wp-block-code\" data-no-translation=\"\" data-no-auto-translation=\"\"><code data-no-translation=\"\" data-no-auto-translation=\"\">window.addEventListener(\"load\", () =&gt; {\n\tsetTimeout(function () {\n\t\t\/\/ Load adsbygoogle.js after 3,5s\n\t\tvar script_adsense = document.createElement('script');\n\t\t\/\/ Replace \"src\" with the url provided for you by AdSense\n\t\tscript_adsense.src = 'https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-YOURCODE';\n\t\tscript_adsense.setAttribute('crossOrigin', 'anonymous');\n\t\tdocument.body.appendChild(script_adsense);\n\t\t\/\/ Push multiple Ads\n\t\tjQuery('.adsbygoogle').each(function () { (adsbygoogle = window.adsbygoogle || &#91;]).push({}); });\n\t}, 3500);\n});<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You can apply this code to all Ad types provided by Google AdSense, including &quot;Auto ads&quot; and &quot;Ad units&quot; without having to repeat the functions. <code data-no-translation=\"\" data-no-auto-translation=\"\">push({})<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can use plugins <code data-no-translation=\"\" data-no-auto-translation=\"\">Code snippets<\/code> to paste this code at the bottom of the page and before the closing tag <code data-no-translation=\"\" data-no-auto-translation=\"\">&lt;\/body&gt;<\/code> to ensure that the ad is fully displayed.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This is how I put the AdSense Delay code in Oxygen Builder.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1210\" height=\"670\" src=\"https:\/\/cdn.artru.eu.org\/wp-content\/uploads\/2023\/08\/Delay-js-AdSense-trong-Oxygen-Builder.png\" alt=\"Delay js AdSense in Oxygen Builder\" class=\"wp-image-2039\"\/><figcaption class=\"wp-element-caption\">Delay js AdSense in Oxygen Builder<\/figcaption><\/figure>\n<\/div>\n\n\n<h2 class=\"wp-block-heading\">New way. Use event capture technique<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In the process of writing this tutorial. Reminds me of the JavaScript event catching function.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">So started rewriting the code and testing the speed. Surprisingly, this approach achieves the same absolute score as the 3.5s delay, but the ad is still loaded early when the user interacts.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Because I use Wordpress and the jQuery file is preloaded, I wrote the jQuery code to keep it short:<\/p>\n\n\n\n<pre class=\"wp-block-code\" data-no-translation=\"\" data-no-auto-translation=\"\"><code data-no-translation=\"\" data-no-auto-translation=\"\">jQuery(window).one(\"touchmove.adsense mousemove.adsense scroll.adsense\", () =&gt; {\n\t\/\/ Load adsbygoogle.js\n\tvar script_adsense = document.createElement('script');\n\t\/\/ Replace \"src\" with the url provided for you by AdSense\n\tscript_adsense.src = 'https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-YOURCODE';\n\tscript_adsense.setAttribute('crossOrigin', 'anonymous');\n\tdocument.body.appendChild(script_adsense);\n\t\/\/ Push multiple Ads\n\tjQuery('.adsbygoogle').each(function () { (adsbygoogle = window.adsbygoogle || &#91;]).push({}); });\n\t\/\/ Turn off events\n\tjQuery(window).off(\".adsense\");\n});<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">After completing and the results for both ways are scored <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-green-cyan-color\">100\/100<\/mark> like when not attached AdSense.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"793\" height=\"810\" src=\"https:\/\/cdn.artru.eu.org\/wp-content\/uploads\/2023\/08\/PageSpeed-sau-khi-toi-uu-AdSense.png\" alt=\"PageSpeed after optimizing AdSense\" class=\"wp-image-2050\"\/><figcaption class=\"wp-element-caption\">PageSpeed after optimizing AdSense<\/figcaption><\/figure>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>Khi nh\u1eafc \u0111\u1ebfn AdSense nhi\u1ec1u b\u1ea1n s\u1ebd ng\u1ea1i g\u1eafn l\u00ean Website c\u1ee7a m\u00ecnh. V\u00ec n\u00f3 \u1ea3nh h\u01b0\u1edfng \u0111\u1ebfn t\u1ed1c \u0111\u1ed9 load trang web v\u00e0 gi\u1ea3m \u0111i\u1ec3m PageSpeed Insights c\u1ee7a Google. Th\u1ee9 h\u1ea1ng website tr\u00ean c\u00e1c c\u00f4ng c\u1ee5 t\u00ecm ki\u1ebfm c\u0169ng t\u1eeb \u0111\u00f3 m\u00e0 t\u1ee5t theo. T\u1ed1c \u0111\u1ed9 tr\u01b0\u1edbc khi t\u1ed1i \u01b0u AdSense Truy c\u1eadp Page [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2060,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[112],"class_list":["post-1997","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-website","tag-adsense"],"_links":{"self":[{"href":"https:\/\/artru.net\/en\/wp-json\/wp\/v2\/posts\/1997","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/artru.net\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/artru.net\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/artru.net\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/artru.net\/en\/wp-json\/wp\/v2\/comments?post=1997"}],"version-history":[{"count":0,"href":"https:\/\/artru.net\/en\/wp-json\/wp\/v2\/posts\/1997\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/artru.net\/en\/wp-json\/wp\/v2\/media\/2060"}],"wp:attachment":[{"href":"https:\/\/artru.net\/en\/wp-json\/wp\/v2\/media?parent=1997"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/artru.net\/en\/wp-json\/wp\/v2\/categories?post=1997"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/artru.net\/en\/wp-json\/wp\/v2\/tags?post=1997"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}