Created
February 5, 2020 08:33
-
-
Save fergusKe/3ed24a87094be2f385a08eb7bae278f8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script> | |
(function($) { | |
var dataLayer = window.dataLayer || []; | |
var eventString = 'cusevent'; | |
var eventCategoryString = '官網文章'; | |
var title = $('.article-area h1').text().trim(); | |
var intro = $('.article-area .introduction').text().trim(); | |
var inner = $('.article-area .article-inner >').not('.article-lock').text().trim(); | |
var win_width = $(window).width(); | |
var a_link = 'https://shopping.parenting.com.tw/index.php?route=product/search&search=%E6%A1%8C%E9%81%8A'; | |
var img_link = ''; | |
var img_link_pc = 'https://i.imgur.com/H6oCiLU.jpg'; | |
var img_link_mobile = 'https://i.imgur.com/6VAvxb0.jpg'; | |
var img_alt = '桌遊_1/16-1/30'; | |
var keyword = '桌遊'; | |
var isKeyword = false; // 判斷是否有關鍵字 | |
keyword.split('/').forEach(function(keyword, i) { | |
if (title.indexOf(keyword) > -1 || intro.indexOf(keyword) > -1 || inner.indexOf(keyword) > -1) { | |
isKeyword = true; | |
} | |
}); | |
// 找不到關鍵字就不往下執行 | |
if (!isKeyword) { | |
return | |
} | |
// 已經有放其他關鍵字的圖就不往下執行 | |
if ($('.td-link').length > 0) { | |
return | |
} | |
if (win_width > 420) { | |
img_link = img_link_pc; | |
} else { | |
img_link = img_link_mobile; | |
} | |
appendImgToArticle(a_link, img_link, img_alt); | |
// 曝光 | |
pushDatalayer('曝光-文章內導流shopping連結', img_alt); | |
// 點擊 | |
$('.td-link').on('click', function() { | |
pushDatalayer('文章內導流shopping連結', img_alt); | |
}); | |
function appendImgToArticle(a_link, img_link, img_alt) { | |
var img = '<a style="display: block; text-align: center;" class="td-link" target="_black" href="' + a_link + '"><img src="' + img_link + '" alt="' + img_alt + '"></a>'; | |
$('.article-area .article-inner p:eq(1)').after(img); | |
} | |
function pushDatalayer(thisAction, thisLabel) { | |
dataLayer.push({ | |
event: eventString, | |
eventCategory: eventCategoryString, | |
eventAction: thisAction, | |
eventLabel: thisLabel | |
}); | |
} | |
})(jQuery) | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment