未加星标

Add Konami Code with GTM

字体大小 | |
[前端(javascript) 所属分类 前端(javascript) | 发布者 店小二04 | 时间 2019 | 作者 红领巾 ] 0人收藏点击收藏
Add Konami Code withGTM

Jack Tonzing

If you play video games or know easter eggs, you might be familiar with the Konami Code . It was first created by Konami game developers to help with play testing the later stages of their game. Since then it has become integrated into gaming and internet culture. With some websites adding the code to play gifs, sound or even video, such as Overwatch and Buzzfeed .


Add Konami Code with GTM
up up down down left right left right ba Here wego:

Enough about what the Konami Code is, let’s get into putting it on our own site. I have chosen to do it through GTM for a couple of reasons.

Easily transfer the code to other sites (ctrl-c, ctrl-v) Easily add the tracking code on any page (all pages? homepage?) Easily swap out what the code activates (flashing lights? obnoxious noises?) Step 1.

Create a new Tag with type Custom HTML called Konami Code ― Initialise with the below content (tweaked from Peter’s answer here ). Add a Trigger that suits your needs, such as All Pages .

<script> function trackKonamiCode() { var input = ''; var konami_code = '38384040373937396665'; document.addEventListener('keydown', function (e) { input += ("" + e.keyCode); if (input == konami_code) { dataLayer.push({event:"konamiActivated"}); } if (konami_code.indexOf(input)) { input = ("" + e.keyCode); } }); } trackKonamiCode(); </script>

Explanation

Line 3.

- string of key codes that make up konami code (38=up, 40=down, 37=left, 39=right, 66=b, 65=a)

Line 4 12.

- function to run with the pressed key code

Line 5.

- adds pressed key code to inputted key codes

Line 6 8.

- checks if inputted key codes match konami code

- pushes konamiActivated event to dataLayer (we use this later)

Line 9 11.

- resets inputted key codes if it doesn’t match start of konami code

Step 2.

Create a new Trigger with type Custom Event called Konami Code ― Activated which fires on the event we pushed before konamiActivated .

Step 3.

Create a new Tag with type Custom HTML . Add the Trigger above, Konami Code ― Activated . Custom HTML is the preference as you can add HTML or javascript. Check the end of the article for some inspiration, or let your imagination run wild.

Step 4

Publish the Tags. Update the Tags. Add more Tags.

Go Wild!

Snowfall

Get into the Christmas spirit with some falling snow.

<div style="background-image:url("<falling_snow.gif>"); background-repeat:repeat; background-color:rgba(0,0,0,0.1); position:fixed; top:0; left:0; height:100%; width:100%; pointer-events:none; z-index:9999;"> </div>
Add Konami Code with GTM
Sound

Play some sound effects for the people that know the code.

<script> var audio = new Audio("<audio.mp3>"); audio.play(); </script> Links

Direct the user to a new tab/window. Is it useful? Is it a secret?

<script>
window.open("https://www.youtube.com/watch?v=dQw4w9WgXcQ");
</script>

本文前端(javascript)相关术语:javascript是什么意思 javascript下载 javascript权威指南 javascript基础教程 javascript 正则表达式 javascript设计模式 javascript高级程序设计 精通javascript javascript教程

代码区博客精选文章
分页:12
转载请注明
本文标题:Add Konami Code with GTM
本站链接:https://www.codesec.net/view/627757.html


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 前端(javascript) | 评论(0) | 阅读(205)