重要事項:本文件不適用於您目前選取的格式 ads!
amp-link-rewriter
說明
允許發布商根據可設定的模式重新編寫 URL
必要指令碼
<script async custom-element="amp-link-rewriter" src="https://cdn.ampproject.org/v0/amp-link-rewriter-0.1.js"></script>
支援版面配置
用法
amp-link-rewriter
允許發布商根據可設定的模式重新編寫 URL。
必要元素結構
新增必要指令碼
在您的 AMP 頁面的 <head>...</head>
區段中,於 <script async src="https://cdn.ampproject.org/v0.js"></script>
行之前插入以下程式碼。
<script async custom-element="amp-link-rewriter" src="https://cdn.ampproject.org/v0/amp-link-rewriter-0.1.js" ></script>
新增 amp-link-rewriter
元件
在您的 AMP 頁面的 <body>...</body>
區段中,插入如下範例所示的程式碼 (必須針對每個供應商的使用案例進行自訂)
<amp-link-rewriter layout="nodisplay"> <script type="application/json"> { "output": "https://visit.foo.net?pid=110&url=${href}&cid=${customerId}", "section": ["#product-listing-1", "#product-listing-2"], "attribute": { "href": "((?!(https:\/\/skip\.com)).)*", "id": "comments", "class": "sidebar", "rel": "(?!(skip))*" }, "vars": { "customerId": "12345" } } </script> </amp-link-rewriter>
完整範例
最終程式碼應如下所示
<!DOCTYPE html> <html ⚡ lang="en"> <head> ... <script async custom-element="amp-link-rewriter" src="https://cdn.ampproject.org/v0/amp-link-rewriter-0.1.js" ></script> ... <script async src="https://cdn.ampproject.org/v0.js"></script> </head> <body> ... <amp-link-rewriter layout="nodisplay"> <script type="application/json"> { "output": "https://visit.foo.net?pid=110&url=${href}&cid=${customerId}", "section": ["#product-listing-1", "#product-listing-2"], "attribute": { "href": "`((?!(https:\/\/skip\.com)).)*`", "id": "comments", "class": "sidebar", "rel": "(?!(skip))*" }, "vars": { "customerId": "12345" } } </script> </amp-link-rewriter> .... </body> </html>
JSON 設定
output
(必填)
output
屬性必須是字串值,其中包含重新導向網址,外加預留位置的查詢字串,這些預留位置將會與組態 JSON vars
屬性中定義的值,或錨點本身做為資料屬性來進行位移。
<amp-link-rewriter layout="nodisplay"> <script type="application/json"> { "output": "https://visit.foo.net?pid=110&cid=${customerId}`", "vars": { "customerId": "12345" } } </script> </amp-link-rewriter>
我們也可以在錨點資料屬性中定義資料值,如下列範例所示
<a href="https://www.amazon.de/s?i=computers&rh=n%3A340843031&ref=sn_gfs_co_computervs_AM_5" data-vars-event-id="231" ></a>
組態可能如下所示
{ "output": "https://visit.foo.net?eid=${eventId}&cid=${customerId}" }
重新編寫後的結果網址會是
`https://visit.foo.net?eid=231&cid=12345`
除了在 JSON 設定的 vars
屬性中定義的 vars,或做為資料屬性之外,還有其他預先定義的 vars
,即 AMP URL MACRO 以及錨點屬性 id
和 href
,可用於取代輸出模式中的值。可用的 AMP URL MACRO 為 AMP_GEO
、DOCUMENT_REFERRER
和 SOURCE_URL
。下表顯示定義的資料與預留位置之間的關係。
值 | 來源 | 範例 | 預留位置 |
---|---|---|---|
國家 | geo | us |
AMP_GEO(ISOCountry) |
data-vars-* | anchor | <a href="..." data-vars-merchant-id="123" /> |
${merchantId} |
href | anchor | <a href="https://retailer.com" /> |
${href} |
id | anchor | <a href="..." id="link" /> |
${id} |
位置 | 網頁 | https://www.partnerweb.com/ |
SOURCE_URL |
隨機 | 網頁 | Math.random().toString(32).substr(2) |
${random} |
參照位址 | 網頁 | https://google.de/ |
DOCUMENT_REFERRER |
rel | anchor | <a href="..." rel="pass" /> |
${rel} |
rev | anchor | <a href="..." rev="author" /> |
${rev} |
vars.* | 設定 | { "vars": { "publisherId": "123" } } |
${publisherId} |
section (選填)
section
屬性定義 css 選取器運算式陣列,用於篩選應執行網址重新編寫的區域。
{ "output": "https://visit.foo.net?pid=110&url=${href}&cid=${customerId}", "section": ["#product-listing-1", "#product-listing-2"] }
在先前的範例中,以屬性 ID 定義為 product-listing-1
和 product-listing-2
的 html 區段將會被視為進行網址重新編寫。
attribute (選填)
attribute
屬性定義規則清單,以比對從篩選區段擷取的錨點元素。這些規則是從與 html 元素屬性 (例如 id
、href
、class
或 rel
) 相關聯的規則運算式所建構。
{ "section": ["#product-listing-1"], "attribute": { "href": "((?!(https://skip.com)).)*", "class": "comments" } }
ID 為 product-listing-1
的 html 區域內的錨點必須比對針對屬性 href 和 class 定義的規則運算式。規則運算式會以 ^
和 $
包裝,以進行完整比對。
在這個範例中,這表示所有具有 youtube.com
和 mobile.vodafone.de
的錨點都將排除在外。此外,包含的錨點需要具有類別屬性,且值為 comments
。
scopeDocument (選填)
根據預設,如果頁面沒有任何連結符合組態中指定的 attribute
和 section
範圍,則所有錨點都會重新編寫。
如要變更此行為,您需要將 scopeDocument
設定為 false
。
驗證
請參閱 AMP 驗證器規格中的 amp-link-rewriter
規則。
您已經讀過這份文件十幾次,但還是沒有真正涵蓋您的所有問題嗎?或許其他人也有同感:請在 Stack Overflow 上與他們聯絡。
前往 Stack Overflow 發現錯誤或缺少功能嗎?AMP 專案大力鼓勵您的參與和貢獻!我們希望您能成為我們開放原始碼社群的長期參與者,但我們也歡迎針對您特別熱衷的問題提供一次性貢獻。
前往 GitHub