Google AMP – Media

Google AMP – Media ”; Previous Next In this chapter, we will discuss how to display video and audio from third party partners such as jwplayer and Youtube. Let us learn in detail about the following − Google AMP − JwPlayer Google AMP − YouTube Google AMP − Audio Google AMP – JwPlayer If you want to use jwplayer to show videos on the page, amp has amp-jwplayer to do it. To work with amp-jwplayer, include the following script in your page − <script async custom-element = “amp-jwplayer” src = ” https://cdn.ampproject.org/v0/amp-jwplayer-0.1.js”> </script> Amp-jwplayer tag <amp-jwplayer data-playlist-id = “482jsTAr” data-player-id = “uoIbMPm3” layout = “responsive” width = “16” height = “9”> </amp-jwplayer> A working example of jwplayer in amp page is shown below − Example <!doctype html> <html amp lang = “en”> <head> <meta charset = “utf-8”> <script async src = “https://cdn.ampproject.org/v0.js”> </script> <title>Google AMP – Amp Jwplayer</title> <link rel = “canonical” href = “http://example.ampproject.org/article-metadata.html”> <meta name = “viewport” content = “width = device-width, minimum-scale = 1,initial-scale = 1”> <style amp-boilerplate> body{ -webkit-animation: -amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation: -amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation: -amp-start 8s steps(1,end) 0s 1 normal both;animation: -amp-start 8s steps(1,end) 0s 1 normal both } @-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}} </style> <noscript> <style amp-boilerplate> body{ -webkit-animation:none; -moz-animation:none; -ms-animation:none; animation:none} </style> </noscript> <script async custom-element = “amp-jwplayer” src = “https://cdn.ampproject.org/v0/amp-jwplayer-0.1.js”> </script> </head> <body> <h3>Google AMP – Amp Jwplayer</h3> <amp-jwplayer data-playlist-id = “482jsTAr” data-player-id = “uoIbMPm3” layout = “responsive” width = “16” height = “9”> </amp-jwplayer> </body> </html> Output For amp-jwplayer, there are three important attributes data-player-id data-media-id data-playlist-id To get the ids of player , media and playlist, you need to have a login in jwplayer which can be done from here − https://dashboard.jwplayer.com/#/players Player id will be available in jwplayer player section. Media id will be available in jwplayer content section and playlist id in jwplayer playlist section. Jwplayer gives an eight digit alphanumeric id which needs to be used in the amp-jwplayer for the respective attribute. Google AMP – Youtube If you want to show Youtube video on your amp page, amp has amp-youtube to embed youtube videos on the page. To use amp-youtube, you need to add following script to your page − <script async custom-element = “amp-youtube” src = ” https://cdn.ampproject.org/v0/amp-youtube-0.1.js”> </script> Amp-youtube tag <amp-youtube width = “480” height = “270” layout = “responsive” autoplay = “true” data-videoid = “fWZ6-p7mGK0”> </amp-youtube> Let us now work on an example that shows the working of amp-youtube on the page. Example <!doctype html> <html amp lang = “en”> <head> <meta charset = “utf-8”> <script async src = “https://cdn.ampproject.org/v0.js”> </script> <title>Google AMP – Youtube</title> <link rel = “canonical” href = “http://example.ampproject.org/article-metadata.html”> <meta name = “viewport” content = “width = device-width, minimum-scale = 1,initial-scale = 1”> <style amp-boilerplate> body{ -webkit-animation: -amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation: -amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation: -amp-start 8s steps(1,end) 0s 1 normal both;animation: -amp-start 8s steps(1,end) 0s 1 normal both } @-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}} </style> <noscript> <style amp-boilerplate> body{ -webkit-animation:none; -moz-animation:none; -ms-animation:none; animation:none} </style> </noscript> <script async custom-element = “amp-youtube” src = “https://cdn.ampproject.org/v0/amp-youtube-0.1.js”> </script> </head> <body> <h3>Google AMP – Youtube</h3> <h3>Youtube Videos from Tutorialspoint</h3> <amp-youtube width = “480” height = “270” layout = “responsive” autoplay = “true” data-videoid = “fWZ6-p7mGK0”> </amp-youtube> </body> </html> Output To show youtube video you need to give videoid to the amp-youtube as shown below − <amp-youtube width = “480” height = “270” layout = “responsive” autoplay = “true” data-videoid = “fWZ6-p7mGK0”> </amp-youtube> How to get the data-videoid? Consider any Youtube url for example − https://www.youtube.com/watch?v=fWZ6-p7mGK0. The highlighted part is the id to be used in your amp-youtube. We have used the attribute autoplay as true. The video will autoplay as supported by the browser and also the video will play in a muted mode. You will have to tap on the video to unmute it. Video will be paused when it goes out of view and will resume from the paused state when it comes to view. If user pauses the video and goes in/out of view, the video will remain in pause state only. The same is applicable for mute/unmute. Google Amp – Audio Amp has a tag to play audio which is a replacement to html5 audio tag. To play audio in the amp page, we can use amp-audio. To work with amp-audio, we need to add the following script − <script async custom-element = “amp-audio” src = ” https://cdn.ampproject.org/v0/amp-audio-0.1.js”> </script> Amp-audio tag <amp-audio width = “auto” height = “50” src = “audio/test.mp3”> <div fallback> <p>HTML5 audio is not supported on your browser!</p> </div> </amp-audio> Hence, amp-audio will take up src attribute which is a http request to the audio file. The reason we are using amp-audio instead of the standard html5 audio is because amp puts a lazy loading concept in place for elements which require http request. It will start loading the request based on priority.It will be loaded just before or when it is about to reach the viewport. A working example of using amp-audio in your page is shown here − Example <!doctype html> <html amp lang = “en”> <head> <meta charset = “utf-8”> <script async src = “https://cdn.ampproject.org/v0.js”> </script> <title>Google AMP – Audio</title> <link rel = “canonical” href = “http://example.ampproject.org/article-metadata.html”> <meta name = “viewport” content = “width = device-width, minimum-scale = 1,initial-scale = 1”> <style amp-boilerplate> body{ -webkit-animation: -amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation: -amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation: -amp-start 8s steps(1,end) 0s 1 normal both;animation: -amp-start 8s steps(1,end) 0s 1 normal both } @-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}} </style> <noscript> <style amp-boilerplate> body{ -webkit-animation:none; -moz-animation:none; -ms-animation:none; animation:none} </style> </noscript> <script async custom-element = “amp-audio” src = “https://cdn.ampproject.org/v0/amp-audio-0.1.js”> </script> </head> <body> <h3>Google AMP – Audio</h3> <amp-audio width = “auto” height = “50” src=”audio/test.mp3″> <div fallback> <p>HTML5 audio is not supported on your browser!</p> </div> </amp-audio> </body> </html> Output The tag for amp-audio where the attributes like width, height, src is specified is shown

Google AMP – Discussion

Discuss Google AMP ”; Previous Next Accelerated Mobile Pages (AMP) is Google”s open source project specially designed to make the web pages mobile friendly by making it render contents faster, smoother and more responsive in any browser. This tutorial gives you a comprehensive coverage of concepts of Google AMP and makes you comfortable to use it in your software development projects. Print Page Previous Next Advertisements ”;

Google AMP – Cors

Google AMP – Cors ”; Previous Next In this chapter, we will try to understand CORS in AMP. Before we dig deeper into the details, let us understand the basics of CORS and how it is useful. What is CORS? CORS stands for Cross Origin Resource Sharing. CORS is a process that needs extra HTTP header data to tell the browser whether the request made to the url by the web page running at say xyz.com origin should be given permission to access the data from the requested url. We make many http requests from the web page and for that we need to have CORS in place to get the data required. When we make a http request to a different server than the host, we call it as cross origin request which means that either the domain, protocol, and port are different from the host origin. In such case, there should be a permission from the requested url for accessing the data; it means GET/PUT/POST/DELETE request made. This additional data is available in the browser header for the http request call made. This step of permission is basically required for security reasons so that no webpage can make or get data from another domain without the required permission. The header of the browser should have details such as Access-Control-Allow-Origin which can have values as shown below − Access-Control-Allow-Origin : * Having value * to the request URL header means it tells the browsers to allow requesting data from any origin to access the resource. Access-Control-Allow-Origin: https://www.example.com Having value as above tells the browser the request made from web page www.example.com will only be allowed to get the data for the requested url. The server configuration for CORS has to be done keeping in mind how the data that is shared will be used. Depending on that the required headers has to be set on the server side. Now that we know what CORS is, let us go another step ahead. In case of amp, we have components like amp-form, amp-list which uses http endpoints to load data dynamically. In case of amp pages, even if the http request is made from the same origin we need to have CORS setting in place. Questions arise here – why should we have CORS enabled even if the request and response will come from same origin. Technically we do not need CORS enabled in such case because we are requesting and displaying data for the same domain, origin etc. Amp has a feature called caching which is added to get the data faster to the user who hits the page. In case the user has already visited the page, the data will be cached on google cdn the next user will get data served from the cache. The data is stored at amp end which now has a different domain. When the user clicks any button to get fresh data, the amp cache url is compared with the webpage domain to get the new data. Here now if the CORS is not enabled as it deals with amp cached url and the webpage domain the request will not be valid and will fail for CORS permission. This is the reason why we need to have CORS enabled even for same origin in case of amp pages. A working example of working with forms having CORS enabled is shown here − <!doctype html> <html amp lang = “en”> <head> <meta charset = “utf-8”> <script async src = “https://cdn.ampproject.org/v0.js”> </script> <title>Google AMP – Form</title> <link rel = “canonical” href = “ampform.html”> <meta name = “viewport” content = “width = device-width, minimum-scale = 1,initial-scale = 1”> <style amp-boilerplate> body{ -webkit-animation: -amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation: -amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation: -amp-start 8s steps(1,end) 0s 1 normal both;animation: -amp-start 8s steps(1,end) 0s 1 normal both } @-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}} </style> <noscript> <style amp-boilerplate> body{ -webkit-animation:none; -moz-animation:none; -ms-animation:none; animation:none} </style> </noscript> <script async custom-element = “amp-form” src = “https://cdn.ampproject.org/v0/amp-form-0.1.js”> </script> <script async custom-template = “amp-mustache” src = “https://cdn.ampproject.org/v0/amp-mustache-0.2.js”> </script> <style amp-custom> form.amp-form-submit-success [submit-success], form.amp-form-submit-error [submit-error]{ margin-top: 16px; } form.amp-form-submit-success [submit-success] { color: white; background-color:gray; } form.amp-form-submit-error [submit-error] { color: red; } form.amp-form-submit-success.hide-inputs > input { display: none; } </style> </head> <body> <h3>Google AMP – Form</h3> <form method = “post” class = “p2” action-xhr = “submitform.php” target = “_top”> <p>AMP – Form Example</p> <div> <input type = “text” name = “name” placeholder = “Enter Name” required> <br/> <br/> <input type = “email” name = “email” placeholder = “Enter Email” required> <br/> <br/> </div> <input type = “submit” value = “Submit”> <div submit-success> <template type = “amp-mustache”> Form Submitted! Thanks {{name}}. </template> </div> <div submit-error> <template type = “amp-mustache”> Error! {{name}}, please try again. </template> </div> </form> </body> </html> submitform.php <?php if(!empty($_POST)){ $domain_url = (isset($_SERVER[”HTTPS”]) ? “https” : “http”) . “://$_SERVER[HTTP_HOST]”; header(“Content-type: application/json”); header(“AMP-Access-Control-Allow-Source-Origin: ” . $domain_url); header(“Access-Control-Expose-Headers: AMP-Access-Control-Allow-Source-Origin”); $myJSON = json_encode($_POST); echo $myJSON; } ?> Output The details of response headers added to submitform.php − For the form to work, we need to add headers like access-control-expose-headers with value AMP-Access-Control-Allow-Source-Origin and amp-access-control-allow-source-origin − http://localhost:8080. Here we are using php file and apache is the server used. In php file, we have added the required headers as shown below − <?php if(!empty($_POST)){ $domain_url = (isset($_SERVER[”HTTPS”]) ? “https” : “http”) . “://$_SERVER[HTTP_HOST]”; header(“Content-type: application/json”); header(“AMP-Access-Control-Allow-Source-Origin: ” . $domain_url); header(“Access-Control-Expose-Headers: AMP-Access-Control-Allow-Source-Origin”); $myJSON = json_encode($_POST); echo $myJSON; } ?> When the required headers are added, the origin http://localhost:8080 will be allowed to interact and get the data back., Print Page Previous Next Advertisements ”;

Google AMP – Overview

Google AMP – Overview ”; Previous Next Google Accelerated Mobile Pages (Google-AMP) is Google’s new open source project specially designed to build light-weight web pages using amp html. The main aim of this project is to make sure the amp code works fine and loads fast on all possible devices such as smartphones, tablets etc. What is AMP? Accerated Mobile Pages (AMP) is Google”s open source project specially designed to make th web pages mobile friendly by making it render contents faster, smoother and responsive in any browser. The official site of Google amp is − https://www.ampproject.org/ WHY AMP? Any user expects that websites would load the contents very fast. However, this may not be the case when the pages are almost flooded with images, videos, animations, social widgets, which makes page very heavy and thus increases its loading time. Such scenario may cause loss of users to the website in a long term. Google AMP is designed to solve this issue. AMP has a special way to take care of images, iframes, javascripts, ads, videos, animations, css, font loaded etc. The contents of AMP pages are cached at the Google end, so each time the user clicks on the search results the content is served from cache. The cache version is also updated timely so that the user always gets a fresh updated page. Why to Choose AMP? This section tells you why you have to opt for AMP for your site − Prioritized on Google Search Today when you search something on the Google, you will find a Google carousel displayed at the top with pages, followed by the list of pages as a response to your search. The news carousel displayed are all valid AMP sites. It means Google gives priority to amp pages and displays them as per ranking in the news carousel. An example for a search made in Google with the keyword “latest indian news” is given here − All the high ranked pages which are AMP pages are displayed at the start in the Google carousel as shown in the image above. A Google AMP page looks as shown below when user searches something in Google search. Note that there is a Google AMP logo on the AMP pages. Faster Loading Time When your page is AMP converted the load time will be much better in comparison to a non-amp page. Faster load time is also an important factor in page ranking in Google search. No Pop-ups Using Google AMP gives a pleasant web browsing experience as the user will not see any unwanted pop-ups for pages designed with Google AMP. Generates Traffic When loading rate of pages is fast, it automatically increases number of viewers and thus traffic to the page increases. How AMP works? The most important components that are used to build website are javascript, images, videos, fonts, css etc. AMP page design is done by taking care of all these factors in a unique way. In this section, let us discuss in brief what exactly AMP does to make the pages faster. Asynchronous JavaScript Javascript plays an important role on the page, as it helps in adding interactivity to the page in the form of animations, DOM changes etc. It also adds slowness to the page and can block other contents from rendering on the page. How AMP tackles JavaScript? AMP loads JavaScript asynchronously. Custom JavaScript is strictly not allowed inside a AMP page. Note that AMP has lot of components added, some of them are a replacement to the existing html tags; for example amp-img, amp-iframe, amp-video, amp-lightbox, amp-animations etc. For each of this component, there is a JavaScript file to be loaded which has the async attribute added to the script tag. Only JavaScript files related to the amp-components are allowed on the page and any other JavaScript inside a AMP page or third party javascript file is not allowed. As AMP uses Google AMP cache, the files are preloaded from cache making their loading faster. Sizes for HTML Tags It is mandatory to give size for the image, iframe, video tags so that amp page can locate the space on the page without having to load the resource. The resources to be loaded are prioritized by the amp page. The content is given more priority over the resources to be loaded. Social Widgets/ ADs Amp provides special components namely amp-facebook, amp-twitter, amp-ad, amp-sticky to take care of social widgets to be shown on the page. AMP-ad component is used to serve ads on the page. AMP takes special care in handling the components and loads the contents on a priority based on the requirements. CSS External CSS is not allowed in AMP pages. Custom CSS if any can be added inside style tag using amp-custom attribute. Inline CSS is also allowed. AMP reduces http requests in all possible ways. Fonts Fonts are allowed in amp pages and the priority of loading the fonts are decided by AMP. Animation AMP supports amp-animation components and allows transition as supported by modern browsers. Considering all the points listed above, AMP takes very special care for HTTP request made for fonts, images, iframes , ads to be served etc. The resources available above the page fold are rendered first and later preference are given for resources available below the fold. Other Points Google AMP Cache is another important factor which helps in rendering the contents faster as the contents are fetched from the cache. Publisher has to maintain two sites amp and non-amp page. For example, consider that the site has the address − https://www.mypage.com. Then, the pages internally for non-amp to be served on desktop will be https://www.mypage.com/news. For devices or AMP, it will be: https://www.mypage/com/news/amp/ How does Google Identify AMP and non-AMP page? Now, let us understand how Google identifies AMP and non-AMP page. When Google search crawls the page, if it happens to get amp in html or <html amp> or <html ⚡>,

Google AMP – ADS

Google AMP – Ads ”; Previous Next Ads play an important role for publisher pages as they are the source of revenue for the publisher. In the case of amp pages, there is a slight variation. They do not allow to add third party javascript files. In order to show ads on the page, there is an amp component called amp-ad which helps to show ad on the page. Most of the ad-networks which serves ads are compatible with amp-ad tag. The details about how ads work is shown in the following image − To show ads on the publisher page, we need to add <amp-ad> which will act as a placeholder to serve ads on the page. <amp-ad> will call the ad-network specified for type. AD-network internally will call the ad to be shown back on the page which is given by advertiser which is mostly a html or an amphtml ad. To make it work, we need to first add the script to the page. <script async custom-element = “amp-ad” src = ” https://cdn.ampproject.org/v0/amp-ad-0.1.js”> </script> The amp-ad tag for doubleclick looks as follows − <amp-ad width = “300” height = “200” type = “doubleclick” data-slot = “/4119129/ad”> <div placeholder> <b>Placeholder here!!!</b> </div> </amp-ad> There are many ad-networks which support amp-ad. Note that we will take a look at doubleclick amp-ad tag in our examples.The ad-network name has to be specified in thetype attribute. Example <!doctype html> <html amp lang = “en”> <head> <meta charset = “utf-8”> <script async src = “https://cdn.ampproject.org/v0.js”> </script> <title>Google AMP – AD</title> <link rel = “canonical” href= “http://example.ampproject.org/article-metadata.html”> <meta name = “viewport” content = “width = device-width, minimum-scale = 1,initial-scale = 1”> <style amp-boilerplate> body{ -webkit-animation: -amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation: -amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation: -amp-start 8s steps(1,end) 0s 1 normal both;animation: -amp-start 8s steps(1,end) 0s 1 normal both } @-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}} </style> <noscript> <style amp-boilerplate> body{ -webkit-animation:none; -moz-animation:none; -ms-animation:none; animation:none} </style> </noscript> <script async custom-element = “amp-ad” src = “https://cdn.ampproject.org/v0/amp-ad-0.1.js”> </script> <style amp-custom> div { text-align:center; } </style> </head> <body> <h3>Google AMP – AD</h3> <h3>300×250 – Banner AD</h3> <p>This is a example of adnetwork doubleclick banner ad</p> <p>This is a example of adnetwork doubleclick banner ad</p> <p>This is a example of adnetwork doubleclick banner ad</p> <p>This is a example of adnetwork doubleclick banner ad</p> <p>This is a example of adnetwork doubleclick banner ad</p> <p>This is a example of adnetwork doubleclick banner ad</p> <div> <amp-ad data-slot = /30497360/amp_by_example/AMP_Banner_300x250 height = 250 layout = fixed style = width:300px;height:250px; type = doubleclick width = 300> </amp-ad> </div> <p>This is a example of adnetwork doubleclick banner ad</p> <p>This is a example of adnetwork doubleclick banner ad</p> <p>This is a example of adnetwork doubleclick banner ad</p> <p>This is a example of adnetwork doubleclick banner ad</p> <p>This is a example of adnetwork doubleclick banner ad</p> <p>This is a example of adnetwork doubleclick banner ad</p> <p>This is a example of adnetwork doubleclick banner ad</p> <p>This is a example of adnetwork doubleclick banner ad</p> <p>This is a example of adnetwork doubleclick banner ad</p> </body> </html> Output Let us take a look at some of the important attributes available on amp-ad as listed in the table given below − Sr.No Attributes & Description 1 type Name of the ad-network 2 width Width of the ad 3 height Height of the ad 4 placeholder Placeholder used as child element and is displayed to the user when the ad is still loading.Please note this attribute has to be supported by the ad-network end. 5 data-* Data attributes to be passed to the ad-network. For example doubleclick ad-network needs data-slot=/30497360/amp_by_example/AMP_Banner_300x250 to render the ad on the page. There will be data params specified by each ad-network. We can also override the width and height used using data-override-width and data-override-height. 6 fallback Fallback is used as a child element to amp-ad and displayed when there is no ad to serve. Let us understand this with the help of a working example which uses placeholder which is executed when there is no ad to serve. Example <!doctype html> <html amp lang = “en”> <head> <meta charset = “utf-8”> <script async src = “https://cdn.ampproject.org/v0.js”> </script> <title>Google AMP – AD</title> <link rel = “canonical” href = “http://example.ampproject.org/article-metadata.html”> <meta name = “viewport” content=”width = device-width, minimum-scale = 1,initial-scale = 1″> <style amp-boilerplate> body{ -webkit-animation: -amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation: -amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation: -amp-start 8s steps(1,end) 0s 1 normal both;animation: -amp-start 8s steps(1,end) 0s 1 normal both } @-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}} </style&gt <noscript> <style amp-boilerplate> body{ -webkit-animation:none; -moz-animation:none; -ms-animation:none; animation:none} </style> </noscript> <script async custom-element = “amp-ad” src = “https://cdn.ampproject.org/v0/amp-ad-0.1.js”> </script> <style amp-custom> div { text-align:center; } </style> </head> <body> <h3>Google AMP – AD</h3> <h3>300×250 – Banner AD</h3> <p>This is a example of adnetwork doubleclick banner ad</p> <p>This is a example of adnetwork doubleclick banner ad</p> <p>This is a example of adnetwork doubleclick banner ad</p> <p>This is a example of adnetwork doubleclick banner ad</p> <p>This is a example of adnetwork doubleclick banner ad</p> <p>This is a example of adnetwork doubleclick banner ad</p> <div> <amp-ad data-slot = /30497360/amp_by_example/AMP_Banner_300x250 height = 250 layout = fixed style = width:300px;height:250px; type = doubleclick width = 300> <div placeholder> <b>Placeholder Example : Ad is loading!!!</b> </div> </amp-ad> </div> <p>This is a example of adnetwork doubleclick banner ad</p> <p>This is a example of adnetwork doubleclick banner ad</p> <p>This is a example of adnetwork doubleclick banner ad</p> <p>This is a example of adnetwork doubleclick banner ad<p> <p>This is a example of adnetwork doubleclick banner ad</p> <p>This is a example of adnetwork doubleclick banner ad</p> </body> </html> Output We can use fallback attribute which is child element to amp-ad and is displayed when there is no ad to serve. Example <!doctype html> <html amp lang = “en”> <head> <meta charset = “utf-8”> <script async src = “https://cdn.ampproject.org/v0.js”> </script> <title>Google AMP – AD</title> <link rel = “canonical” href = “http://example.ampproject.org/article-metadata.html”> <meta name = “viewport” content = “width =

Google AMP – Introduction

Google AMP – Introduction ”; Previous Next Google Accelerated Mobile Pages (Google-AMP) is Google’s new open source project specially designed to build light-weight web pages using amp html. The main aim of this project is to make sure the AMP code works fine and loads fast on all possible devices such as smartphones and tablets. AMP is just an extension to standard HTML. Few HTML tags have changed and AMP has added restriction on their usage. In this chapter, we will list out the html tags which are changed and the restrictions added onto them. The tags which deals with loading external resources, for example images, css, js, forms submission, video, audio etc., are changed. Also there are lot of new features added to amp, for example amp-date-picker, amp-facebook, amp-ad, amp-analytics, amp-ad, amp-lightbox and much more which can be used directly in html pages. Rest others which are meant for display are used as it is. With all these changes and new features, AMP promises to give faster loading, better performance for pages when used in live environment. When you search anything in Google search on your mobile, the display that is seen in the google carousel at the top are mostly amp pages as shown below − When you click the AMP page, the URL which you get in the address bar is as follows − https://www.google.co.in/amp/s/m.timesofindia.com/sports/cricket/india-in-australia/to-hell-with-the-nets-boys-need-rest-ravi-shastri/amp_articleshow/67022458.cms The URL is not coming from the publisher directly, but Google points it to its own copy on Google server which is a cached version and helps rendering the content faster in comparison to a non-amp page. This will happen only on devices or in Google emulator mode. Sample Amp Page An example for amp page is shown below − <!doctype html> <html amp> <head> <meta charset = “utf-8”> <title>Amp Sample Page</title> <link rel = “canonical” href = “./regular-html-version.html”> <meta name = “viewport” content = “width = device-width, minimum-scale = 1,initial-scale = 1”> <style amp-custom> h1 {color: red} </style> <script type = application/json>{ “vars”: { “uid”: “23870”, “domain”: “dummyurl.com”, “sections”: “us”, “authors”: “Hello World” } } </script> <style amp-boilerplate> body{ -webkit-animation: -amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation: -amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation: -amp-start 8s steps(1,end) 0s 1 normal both;animation: -amp-start 8s steps(1,end) 0s 1 normal both } @-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}} </style> <noscript> <style amp-boilerplate> body{ -webkit-animation:none; -moz-animation:none; -ms-animation:none; animation:none} </style> </noscript> <script async src = “https://cdn.ampproject.org/v0.js”></script> </head> <body> <h1>Amp Sample Page</h1> <p> <amp-img src = “imgurl.jpg” width = “300” height = “300” layout = “responsive”></amp-img> </p> <amp-ad width = “300” height = “250” type = “doubleclick” data-slot = “/4119129/no-ad”> <div fallback> <p style = “color:green;font-size:25px;”>No ads to Serve!</p> </div> </amp-ad> </body> </html> Do’s and Don’ts in an AMP Page Let us understand some do’s and don’ts a programmer has to follow in an AMP page. Mandatory Tags There are some mandatory tags to be included in an amp page as given below − We have to make sure that there is amp or ⚡ added to the html tag as shown below − <html amp> OR <html ⚡> <head> and <body> tags should be added to the html page. The following mandatory meta tags should be added in the head section of the page; otherwise it will fail for amp validation <meta charset = “utf-8”> <meta name = “viewport” content = “width=device-width, minimum-scale = 1, initial-scale = 1″> Link of rel = “canonical” to be added inside head tag <link rel = “canonical” href = “./regular-html-version.html”> Style tag with amp-boilerplate − <style amp-boilerplate> body{ -webkit-animation: -amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation: -amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation: -amp-start 8s steps(1,end) 0s 1 normal both;animation: -amp-start 8s steps(1,end) 0s 1 normal both } @-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}} </style> Noscript tag with amp-boilerplate − <noscript> <style amp-boilerplate> body{ -webkit-animation:none; -moz-animation:none; -ms-animation:none; animation:none } </style> </noscript> Very important the amp script tag with async added to it as shown below − <script async src = “https://cdn.ampproject.org/v0.js”> </script> In case you want to add custom CSS to the page, please make a note here we cannot call external style sheet in amp pages. To add custom CSS , all your CSS has to go here as shown − <style amp-custom> //all your styles here </style> The style tag should have amp-custom attribute added to it. Scripts for AMP components Note that scripts with src and type = ”text/javascript” are strictly not allowed in a amp page. Only script tags which async and related to amp-components are allowed to be added in head section. This section lists few scripts used for amp components as given below − amp-ad <script async custom-element = “amp-ad” src = “https://cdn.ampproject.org/v0/amp-ad-0.1.js”> </script> amp-iframe <script async custom-element = “amp-iframe” src = “https://cdn.ampproject.org/v0/amp-iframe-0.1.js”> </script> Notice that the script has async and custom-element attribute with the name of the amp component to be loaded. Amp validates script tags based on async and custom-element property and does not allow any other script to be loaded. It does take type=application/json which we have added in the sample file as shown below <type = application/json> { “vars”: { “uid”: “23870”, “domain”: “dummyurl.com”, “sections”: “us”, “authors”: “Hello World” } } </script> The above script can be used with other amp-components if required, for example for amp-analytics. HTML Tags So far we have seen the mandatory tags required in the amp page. Now we will discuss the HTML elements which are allowed/not allowed and restrictions imposed on them. Here is the list of HTML tags that are allowed/not allowed − Sr.No HTML Tag & Description 1 img This tag is replaced with amp-img. Using of direct img tag is not allowed in an AMP page 2 video Replaced with amp-video 3 audio Replaced with amp-audio 4 iframe Replaced with amp-iframe 5 object Not allowed 6 embed Not allowed 7 form Can be used as <form>. We need to add the script to work with form in an AMP page. Example − <script async custom-element = “amp-form”

Google AMP – Data Binding

Google AMP – Data Binding ”; Previous Next Amp-bind helps to add interactivity to the amp-components and html tags based on a action using data-binding and JS-like expressions. This chapter discusses data binding in detail. To work with amp-bind, we need to add the following script to our page − <script async custom-element = “amp-bind” src = “https://cdn.ampproject.org/v0/amp-bind-0.1.js”> </script> Let us understand this fully with the help of a working example as shown − Example <!doctype html> <html amp lang = “en”> <head> <meta charset = “utf-8”> <script async src = “https://cdn.ampproject.org/v0.js”></script> <title>Google AMP – Amp Bind</title> <link rel = “canonical” href = “http://example.ampproject.org/article-metadata.html”> <meta name = “viewport” content = “width = device-width, minimum-scale = 1,initial-scale = 1”> <style amp-boilerplate> body{ -webkit-animation: -amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation: -amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation: -amp-start 8s steps(1,end) 0s 1 normal both;animation: -amp-start 8s steps(1,end) 0s 1 normal both } @-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}} </style> <noscript> <style amp-boilerplate> body{ -webkit-animation:none; -moz-animation:none; -ms-animation:none; animation:none } </style> </noscript> <script async custom-element = “amp-bind” src = “https://cdn.ampproject.org/v0/amp-bind-0.1.js”></script> <style amp-custom> button{ background-color: #ACAD5C; color: white; padding: 12px 20px; border: none; border-radius: 4px; } </style> </head> <body> <h3>Google AMP – Amp Bind</h3> <p [text] = “”Hello ” + world + ”.””> Click on the button to change the text </p> <button on = “tap:AMP.setState({world: ”This is amp-bind example”})”> Click Here </button> </body> </html> Output Click the button to see the text changing as shown below − Thus, in the example shown above we have used amp-bind to change the text on click of the button. Amp-bind has three components − State − Initially the state is empty. Once you click the button the state is changed. For example, <button on = “tap:AMP.setState({world: ”This is amp-bind example”})”> Click Here </button> AMP.setState method is used to change the state. The variable world is assigned the value This is amp-bind example. The variable world is used inside the html tag − <p [text] = “”Hello ” + world + ”.””> Click on the button to change the text </p> On click of the button, world is assigned a new value: This is amp-bind example. We can also use amp-state with binding as shown below − <amp-state id = “myState”> <script type = “application/json”> { “foo”: “bar” } </script> </amp-state> The expression will be assigned bmyState.foo during binding. Expressions − The expressions for amp-bind to work is given as follows − ”Hello ” + world world is said to be a state variable. Bindings − Bindings are applied to special attributes in the form [attributes]. For example − <p [text] = “”Hello ” + world + ”.””> Click on the button to change the text </p> In the above example, [text] has the expression which is used for binding the p tag. We can use the following attribute for bindings − [text] [class] [hidden] [width] [height] Bindings is also possible to be done on amp-components and only specific attributes are allowed. The following list shows suh components and attributes − Sr.No Amp component Attributes & Description 1 <amp-carousel type=slides> [slide]* Change the slide using this binding behaviour 2 <amp-date-picker> [min] min -> Sets the earliest selectable date [max] max -> Sets the latest selectable date 3 <amp-iframe> [src] Change src of iframe 4 <amp-img> [alt] [attribution] [src] [srcset] We can change alt , attribution, src and srcset.If src is changed do change srcset as it is used for caching 5 <amp-lightbox> [open]* You can show/hide lightbox by binding to open 6 <amp-list> [src] If expression is a string, fetches and renders JSON from the string URL. If expression is an object or array, renders the expression data. 7 <amp-selector> [selected]* [disabled] Changes the currently selected children element(s) identified by their option attribute values. Supports a comma-separated list of values for multiple selection Binding using Amp-State We can define amp-state with all the data that we would want to use on html element or amp-component. The data used inside amp-state has to be in json format as shown below − <amp-state id = “myCarsList”> <script type = “application/json”> { “currentcar” : “bmw”, “audi”: { “imageUrl”: “images/audi.jpg” }, “bmw”: { “imageUrl”: “images/bmw.jpg” } } </script> </amp-state> Thus, we have defined key-value pairs with the name of the car and the image used for the car. Amp-bind on text and Amp-Image A working example using amp-state with amp-bind is shown below − <!doctype html> <html amp lang = “en”> <head> <meta charset = “utf-8”> <script async src = “https://cdn.ampproject.org/v0.js”></script> <title>Google AMP – Amp Bind</title> <link rel = “canonical” href = “http://example.ampproject.org/article-metadata.html”> <meta name = “viewport” content = “width = device-width, minimum-scale = 1,initial-scale = 1”> <style amp-boilerplate> body{ -webkit-animation: -amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation: -amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation: -amp-start 8s steps(1,end) 0s 1 normal both;animation: -amp-start 8s steps(1,end) 0s 1 normal both } @-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible} } </style> <noscript> <style amp-boilerplate> body{ -webkit-animation:none; -moz-animation:none; -ms-animation:none; animation:none } </style> </noscript> <script async custom-element = “amp-bind” src = “https://cdn.ampproject.org/v0/amp-bind-0.1.js”></script> <style amp-custom> button{ background-color: #ACAD5C; color: white; padding: 12px 20px; border: none; border-radius: 4px; cursor: pointer; float: left; } </style> </head> <body> <h3>Google AMP – Amp Bind</h3> <amp-state id = “myCarsList”> <script type = “application/json”> { “currentcar” : “bmw”, “audi”: { “imageUrl”: “images/audi.jpg”, “style”: “greenBackground” }, “bmw”: { “imageUrl”: “images/bmw.jpg”, “style”: “redBackground” } } </script> </amp-state> <amp-img width = “300” height = “200” src = “images/bmw.jpg” [src] = “myCarsList[currentcar].imageUrl”> </amp-img> <p [text] = “”This is a ” + currentcar + ”.””> This is a BMW. </p> <br/> <button on = “tap:AMP.setState({currentcar: ”audi”})”> Change Car </button> </body> </html> Output Click the button to see the image of the car changing and also the text below. Amp-bind on Video and IFrame We will now see a working example which will change the amp-iframe and amp-video src. <!doctype html> <html amp lang = “en”> <head> <meta charset = “utf-8”> <script async src = “https://cdn.ampproject.org/v0.js”></script> <title>Google AMP – Amp Bind</title> <link rel = “canonical”

Google AMP – Attributes

Google AMP – Attributes ”; Previous Next This chapter will discuss all the common attributes used by the amp-components. The list of common attributes is as follows − fallback heights layout media noloading on placeholder sizes width and height fallback attribute The fallback attribute is mostly used when the browser does not support the element used or has issues with the file loading or having errors with the file used. For example, you are using amp-video and the media file is having issues on the browser so in such cases we can specify the fallback attribute and display a message that the media file cannot be played or not supported by the browser, instead of showing the error message on the page. Fallback used on amp-video <amp-video controls width = “640” height = “360” layout = “responsive” poster = “images/videoposter.png”> <source src = “video/bunny.webm” type = “video/webm” /> <source src = “video/samplevideo.mp4” type = “video/mp4” /> <div fallback> <p>This browser does not support the video element.</p> </div> </amp-video> Let us understand the working os fallback using an example − Example <!doctype html> <html amp lang = “en”> <head> <meta charset = “utf-8”> <script async src = “https://cdn.ampproject.org/v0.js”></script> <title>Google AMP – Amp Video</title> <link rel = “canonical” href = ” http://example.ampproject.org/article-metadata.html”> <meta name = “viewport” content = “width = device-width,minimum-scale = 1,initial-scale = 1”> <style amp-boilerplate> body{ -webkit-animation: -amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation: -amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation: -amp-start 8s steps(1,end) 0s 1 normal both;animation: -amp-start 8s steps(1,end) 0s 1 normal both } @-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}} </style> <noscript> <style amp-boilerplate> body{ -webkit-animation:none;-moz-animation:none; -ms-animation:none;animation:none } </style> </noscript> <script async custom-element = “amp-video” src = “https://cdn.ampproject.org/v0/amp-video-0.1.js”> </script> </head> <body> <h3>Google AMP – Amp Video</h3> <amp-video controls width = “640” height = “360” layout = “responsive” poster = “images/videoposter.png”> <source src = “video/bunny.webm”type=”video/webm” /> <source src = “video/samplevideo.mp4″type = “video/mp4” /> <div fallback> <p>This browser does not support the video element.</p> </div> </amp-video> </body> </html> Output Heights Attribute This attribute is basically supported for a responsive layout. You can use a media expression to the heights attribute and it applies to the height of the element. It also takes the percent values, so the height is calculated based on the percentage width given. Example <!doctype html> <html amp lang = “en”> <head> <meta charset = “utf-8”> <script async src = “https://cdn.ampproject.org/v0.js”></script> <title>Google AMP – heights attribute</title> <link rel = “canonical” href = “http://example.ampproject.org/article-metadata.html”> <meta name = “viewport” content = “width=device-width,minimum-scale = 1,initial-scale = 1”> <style amp-boilerplate> body{ -webkit-animation: -amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation: -amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation: -amp-start 8s steps(1,end) 0s 1 normal both;animation: -amp-start 8s steps(1,end) 0s 1 normal both } @-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}} </style> <noscript> <style amp-boilerplate> body{ -webkit-animation:none;-moz-animation:none; -ms-animation:none;animation:none } </style> </noscript> <style amp-custom> amp-img { border: 1px solid black; border-radius: 4px; padding: 5px; } h1{font-family: “Segoe UI”,Arial,sans-serif;font-weight: 400;margin: 10px 0;} </style> </head> <body> <h1>Google AMP – heights attribute</h1> <amp-img src = “images/christmas1.jpg” width = “320” height = “256” heights = “(min-width:500px) 200px, 80%”> </amp-img> </body> </html> Output layout attribute AMP-Layout is one of the important feature available in google-amp. Amp Layout makes sure the amp components are rendered properly when the page is loaded without causing any flicker or scrolling issue. It also checks the page rendering before any other remote resources like http request for images, data calls are done. The list of layout supported by amp is as follows − Not Present Container fill fixed fixed-height flex-item intrinsic nodisplay Responsive You will learn in detail about the same in the chapter Google AMP − Layout of this tutorial. Let is understand the working of layout= ”responsive” with the help of an example as shown − Example <!doctype html> <html amp lang = “en”> <head> <meta charset = “utf-8”> <script async src = “https://cdn.ampproject.org/v0.js”></script> <title>Google AMP – Image>/title> <link rel = “canonical” href = ” http://example.ampproject.org/article-metadata.html”> <meta name = “viewport” content = “width = device-width,minimum-scale = 1,initial-scale = 1”> <style amp-boilerplate> body { -webkit-animation: -amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation: -amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation: -amp-start 8s steps(1,end) 0s 1 normal both;animation: -amp-start 8s steps(1,end) 0s 1 normal both } @-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}} </style> <noscript> <style amp-boilerplate> body { -webkit-animation:none;-moz-animation:none; -ms-animation:none;animation:none } </style> </noscript> <style amp-custom> amp-img { border: 1px solid black; border-radius: 4px; padding: 5px; } displayitem { display: inline-block; width: 200px; height:200px; margin: 5px; } h1{font-family: “Segoe UI”,Arial,sans-serif;font-weight: 400;margin: 10px 0;} </style> </head> <body> <h1>Google AMP – Layout = responsive Image Example>/h1> <div class = “displayitem”> <amp-img alt = “Beautiful Flower” src = “images/flower.jpg” width = “246” height = “205” layout = “responsive”> </amp-img> </div> </body> </html> Output Media Attribute This attribute can be used on most of the amp components. It takes a media query and if the value does not match the component will not be rendered. Let us understand the working of media attribute with the help of an example − Example <!doctype html> <html amp lang = “en”> <head> <meta charset = “utf-8”> <script async src = “https://cdn.ampproject.org/v0.js”></script> <title>Google AMP – Image</title> <link rel = “canonical” href = ” http://example.ampproject.org/article-metadata.html”> <meta name = “viewport” content = “width = device-width,minimum-scale = 1,initial-scale = 1”> <style amp-boilerplate> body{ -webkit-animation: -amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation: -amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation: -amp-start 8s steps(1,end) 0s 1 normal both;animation: -amp-start 8s steps(1,end) 0s 1 normal both } @-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}} </style> <noscript> <style amp-boilerplate> body { -webkit-animation:none;-moz-animation:none; -ms-animation:none;animation:none } </style> </noscript> <style amp-custom> amp-img { border: 1px solid black; border-radius: 4px; padding: 5px; } h1{font-family: “Segoe UI”,Arial,sans-serif;font-weight: 400;margin: 10px 0;} </style> </head> <body> <h1>Google AMP – Media Attribute</h1> <div class = “displayitem”> <amp-img media = “(min-width: 600px)” src = “images/christmas1.jpg” width = “466” height = “355” layout = “responsive”> </amp-img> </div> </body> </html> We have used media attribute on the <amp-img> tag as shown below − <amp-img media = “(min-width: 600px)” src = “images/christmas1.jpg” width =

Google AMP – Quick Guide

Google AMP – Quick Guide ”; Previous Next Google AMP – Overview Google Accelerated Mobile Pages (Google-AMP) is Google’s new open source project specially designed to build light-weight web pages using amp html. The main aim of this project is to make sure the amp code works fine and loads fast on all possible devices such as smartphones, tablets etc. What is AMP? Accerated Mobile Pages (AMP) is Google”s open source project specially designed to make th web pages mobile friendly by making it render contents faster, smoother and responsive in any browser. The official site of Google amp is − https://www.ampproject.org/ WHY AMP? Any user expects that websites would load the contents very fast. However, this may not be the case when the pages are almost flooded with images, videos, animations, social widgets, which makes page very heavy and thus increases its loading time. Such scenario may cause loss of users to the website in a long term. Google AMP is designed to solve this issue. AMP has a special way to take care of images, iframes, javascripts, ads, videos, animations, css, font loaded etc. The contents of AMP pages are cached at the Google end, so each time the user clicks on the search results the content is served from cache. The cache version is also updated timely so that the user always gets a fresh updated page. Why to Choose AMP? This section tells you why you have to opt for AMP for your site − Prioritized on Google Search Today when you search something on the Google, you will find a Google carousel displayed at the top with pages, followed by the list of pages as a response to your search. The news carousel displayed are all valid AMP sites. It means Google gives priority to amp pages and displays them as per ranking in the news carousel. An example for a search made in Google with the keyword “latest indian news” is given here − All the high ranked pages which are AMP pages are displayed at the start in the Google carousel as shown in the image above. A Google AMP page looks as shown below when user searches something in Google search. Note that there is a Google AMP logo on the AMP pages. Faster Loading Time When your page is AMP converted the load time will be much better in comparison to a non-amp page. Faster load time is also an important factor in page ranking in Google search. No Pop-ups Using Google AMP gives a pleasant web browsing experience as the user will not see any unwanted pop-ups for pages designed with Google AMP. Generates Traffic When loading rate of pages is fast, it automatically increases number of viewers and thus traffic to the page increases. How AMP works? The most important components that are used to build website are javascript, images, videos, fonts, css etc. AMP page design is done by taking care of all these factors in a unique way. In this section, let us discuss in brief what exactly AMP does to make the pages faster. Asynchronous JavaScript Javascript plays an important role on the page, as it helps in adding interactivity to the page in the form of animations, DOM changes etc. It also adds slowness to the page and can block other contents from rendering on the page. How AMP tackles JavaScript? AMP loads JavaScript asynchronously. Custom JavaScript is strictly not allowed inside a AMP page. Note that AMP has lot of components added, some of them are a replacement to the existing html tags; for example amp-img, amp-iframe, amp-video, amp-lightbox, amp-animations etc. For each of this component, there is a JavaScript file to be loaded which has the async attribute added to the script tag. Only JavaScript files related to the amp-components are allowed on the page and any other JavaScript inside a AMP page or third party javascript file is not allowed. As AMP uses Google AMP cache, the files are preloaded from cache making their loading faster. Sizes for HTML Tags It is mandatory to give size for the image, iframe, video tags so that amp page can locate the space on the page without having to load the resource. The resources to be loaded are prioritized by the amp page. The content is given more priority over the resources to be loaded. Social Widgets/ ADs Amp provides special components namely amp-facebook, amp-twitter, amp-ad, amp-sticky to take care of social widgets to be shown on the page. AMP-ad component is used to serve ads on the page. AMP takes special care in handling the components and loads the contents on a priority based on the requirements. CSS External CSS is not allowed in AMP pages. Custom CSS if any can be added inside style tag using amp-custom attribute. Inline CSS is also allowed. AMP reduces http requests in all possible ways. Fonts Fonts are allowed in amp pages and the priority of loading the fonts are decided by AMP. Animation AMP supports amp-animation components and allows transition as supported by modern browsers. Considering all the points listed above, AMP takes very special care for HTTP request made for fonts, images, iframes , ads to be served etc. The resources available above the page fold are rendered first and later preference are given for resources available below the fold. Other Points Google AMP Cache is another important factor which helps in rendering the contents faster as the contents are fetched from the cache. Publisher has to maintain two sites amp and non-amp page. For example, consider that the site has the address − https://www.mypage.com. Then, the pages internally for non-amp to be served on desktop will be https://www.mypage.com/news. For devices or AMP, it will be: https://www.mypage/com/news/amp/ How does Google Identify AMP and non-AMP page? Now, let us understand how Google identifies AMP and non-AMP page. When Google search crawls the page, if it happens to get amp in html or

Google AMP – Button

Google AMP – Button ”; Previous Next Buttons are another feature of AMP. Note that there is no change for buttons in AMP and they are used like standard a HTML button tag. The only difference with buttons in AMP page is the working of events on it. In this chapter, we will see some examples to show the working of button and how to use it with AMP components. Sample Code for Lightbox The following example shows us how to use button to show/hide amp-lightbox as shown below − <!doctype html> <html amp lang = “en”> <head> <meta charset = “utf-8”> <script async src = “https://cdn.ampproject.org/v0.js”></script> <title>Google AMP – Amp Lightbox</title> <link rel = “canonical” href = “http://example.ampproject.org/article-metadata.html”> <meta name = “viewport” content =”width = device-width,minimum-scale = 1,initial-scale = 1″> <style amp-boilerplate> body{ -webkit-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation: -amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation: -amp-start 8s steps(1,end) 0s 1 normal both;animation: -amp-start 8s steps(1,end) 0s 1 normal both } @-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}} </style> <noscript> <style amp-boilerplate> body{ -webkit-animation:none;-moz-animation:none;-ms-an imation:none;animation:none } </style> </noscript> <script async custom-element = “amp-lightbox” src = “https://cdn.ampproject.org/v0/amp-lightbox-0.1.js”> </script> <style amp-custom> amp-img { border: 1px solid #ddd; border-radius: 4px; padding: 5px; } button{ background-color: #ACAD5C; color: white; padding: 12px 20px; border: none; border-radius: 4px; cursor: pointer; float: left; } .lightbox { background: rgba(211,211,211,0.8); width: 100%; height: 100%; position: absolute; display: flex; align-items: center; justify-content: center; } </style> </head> <body> <h3>Google AMP – Amp Lightbox</h3> <button on = “tap:my-lightbox”> Show LightBox </button> <amp-lightbox id = “my-lightbox” layout = “nodisplay”> <div class = “lightbox” on = “tap:my-lightbox.close” tabindex = “0”> <amp-img alt = “Beautiful Flower” src = “images/flower.jpg” width = “246” height = “205”> </amp-img> </div> </amp-lightbox> </body> </html> Output Now, you can click anywhere on the screen to close the lightbox. In the above example, we have used a button using the code as shown below − <button on = “tap:my-lightbox”> Show LightBox </button> Next, we have added action on the button using on attribute as shown: on = “tap:my-lightbox” The action will take place when you tap on the button. Note that the id of the lightbox is given to it. When the user taps on the button, the lightbox will be opened. Similarly, you can use the button with on action with any component to interact with it. Print Page Previous Next Advertisements ”;