gaEvent.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. import getParams from './getParams.js';
  2. gaEvent = function () {
  3. //private menbers
  4. const kolMan = ['Yang', 'Hao', 'Drangadrang', 'Cookingdairy', 'Ku', 'Betty', 'Ralf', 'Chefchouchou', 'Albee', 'commute_Chang', 'commute_Li', 'commute_He', 'ordinary']
  5. let kolName; // 指定kol
  6. let currentKol; //目前sroll到的kol
  7. let _keyName; // 參數名稱
  8. let ordinaryId;
  9. let pathAdd = "";
  10. // --------- GA 碼 ---------
  11. window.dataLayer = window.dataLayer || [];
  12. function gtag() { dataLayer.push(arguments); }
  13. gtag('js', new Date());
  14. gtag('config', "G-4DZD17Z80F");
  15. //private methods
  16. function init() {
  17. console.log('gaEvent is loaded.');
  18. allSiteEvent(); // 全站
  19. if(location.host == "www.our-work.com.tw") {
  20. pathAdd = "/demosite/2022/2022-pxgo/justahour"
  21. }
  22. if (location.pathname == pathAdd+"/index.html" || location.pathname == pathAdd+"/") {
  23. // 取得是哪位Kol
  24. if (getParams('kol') != null && kolMan.includes(getParams('kol'))) {
  25. kolName = getParams('kol')
  26. } else {
  27. kolName = 'ordinary' // 預設kol
  28. }
  29. // console.log(kolName)
  30. _keyName = ("Kol_" + kolName)
  31. indexPageEvent(); // intro & index 頁面
  32. // console.log(typeof _keyName)
  33. }
  34. if (location.pathname == pathAdd+"/recipe.html") {
  35. // 取得是哪位Kol
  36. if (getParams('kol') != null && kolMan.includes(getParams('kol'))) {
  37. kolName = getParams('kol')
  38. } else {
  39. kolName = 'Ralf' // 預設kol
  40. }
  41. _keyName = ("Kol_" + kolName)
  42. recipePageEvent(); // 食譜頁
  43. // console.log(typeof _keyName)
  44. }
  45. if (location.pathname == pathAdd+"/search.html") {
  46. searchPageEvent();
  47. }
  48. if (location.pathname == pathAdd+"/recipe_ordinary.html") {
  49. let idRegx = /^(?:[1-9]|[1-4][0-9]|50)$/
  50. // 取得是哪位素人
  51. if (getParams('ordinary') != null && getParams('ordinary').match(idRegx)) {
  52. ordinaryId = getParams('ordinary')
  53. } else {
  54. ordinaryId = 1
  55. }
  56. recipePageOrdinaryEvent();
  57. console.log(ordinaryId)
  58. }
  59. }
  60. // 全站共用
  61. function allSiteEvent(){
  62. let eventKey = "ALL";
  63. $(".header .hamburger").click(function(){
  64. gtag('event', "click", { [eventKey]: "btn_menu" })
  65. // console.log([eventKey]+",btn_menu")
  66. })
  67. $(".header .logo").click(function(){
  68. gtag('event', "click", { [eventKey]: "btn_logo" })
  69. })
  70. $(".header .nav__item a").click(function(){
  71. let eventName = $(this).data("event");
  72. gtag('event', eventName, { [eventKey]: "btn_menu" })
  73. // console.log([eventName]+","+[eventKey]+",btn_menu")
  74. })
  75. // 點擊食譜頁tag
  76. $(".recipe__tag__list").on("click",".recipe__tag__item",function(){
  77. let eventName = "tag_"+ $(this).text();
  78. console.log(eventName)
  79. gtag('event', eventName, { "justahour_search": "tag_function" });
  80. });
  81. }
  82. // intro & index 頁面
  83. function indexPageEvent(){
  84. let goToIndex = setInterval(function(){
  85. if($(".index").css("opacity") == 1){
  86. detectTouch();
  87. detectKol();
  88. clearInterval(goToIndex);
  89. }
  90. },500)
  91. // 神啊~請救救我煮什麼好?
  92. $(".intro__overlay,.intro__tip").click(function(){
  93. gtag('event', "click", { "intro": "btn_god" });
  94. });
  95. // 喇叭
  96. $(".intro__volume").click(function(){
  97. gtag('event', "click", { "intro": "btn_sound" });
  98. });
  99. // 直接找救星
  100. $(".intro__skip").click(function(){
  101. gtag('event', "click", { "intro": "btn_findhelp" });
  102. });
  103. // 點擊kol
  104. $(".index__kv").on("click",".index__slide,.index__dish",function(){
  105. let val = "into_KOL_"+ currentKol
  106. if(currentKol != 'ordinary') {
  107. gtag('event', "click", { "index": val });
  108. // console.log(val)
  109. }
  110. });
  111. $(".modal-promo .modal-body a").click(function(){
  112. if(kolName == "ordinary") {
  113. gtag('event', "amateur_shareback", { "amateur_shareback": "amateur_shareback" });
  114. }else {
  115. gtag('event', "click", { [_keyName]: "btn_shareback" });
  116. }
  117. })
  118. // 偵測手指滑動
  119. function detectTouch(){
  120. var startX, startY, moveX, moveY;
  121. //here clientX, and clientY means X and Y coordinates
  122. function touchStart(e) {
  123. startX = e.touches[0].clientX;
  124. startY = e.touches[0].clientY;
  125. }
  126. function touchMove(e) {
  127. moveX = e.touches[0].clientX;
  128. moveY = e.touches[0].clientY;
  129. }
  130. function touchEnd() {
  131. if (startX < moveX) {
  132. // console.log('right');
  133. gtag('event', "scroll", { "index": "scroll_right" });
  134. } else if (startX > moveX) {
  135. // console.log('left');
  136. gtag('event', "scroll", { "index": "scroll_left" });
  137. }
  138. if (startY < moveY) {
  139. // console.log('down');
  140. } else if (startY > moveY) {
  141. // console.log('up');
  142. }
  143. }
  144. document.addEventListener('touchstart', touchStart,false);
  145. document.addEventListener('touchmove', touchMove, false);
  146. document.addEventListener('touchend', touchEnd, false);
  147. }
  148. // 偵測中間KOL
  149. function detectKol(){
  150. currentKol = kolName;
  151. $(".index__kv").scroll(function(){
  152. let containerLeft = $(".index__kv").scrollLeft();
  153. let containerCenterStart = containerLeft + ($(window).width() / 2);
  154. // console.log("containerLeft: " + containerLeft)
  155. $(".index__slide").each(function(){
  156. let dataKol = $(this).data('kol');
  157. let target = $(this);
  158. let targetW = target.width();
  159. let targetLeft = target.position().left
  160. // console.log(dataKol + "-targetLeft: " + targetLeft);
  161. if (containerCenterStart > targetLeft && containerCenterStart < (targetLeft + targetW)) {
  162. currentKol = dataKol;
  163. // console.log(currentKol + "-最小: " + targetLeft + ", 最大: " + targetLeft + targetW)
  164. if (currentKol == 'commute' || currentKol == 'HaoYang') {
  165. target.find(".index__slide-item").each(function () {
  166. let childDataKol = $(this).data('kol');
  167. let childtarget = $(this);
  168. let childtargetW = childtarget.width();
  169. let childtargetLeft = childtarget.position().left
  170. let childStart = targetLeft + childtargetLeft
  171. let childEnd = targetLeft + childtargetLeft + childtargetW
  172. // console.log(childDataKol+" - childStart: " + childStart + ", childEnd: " + childEnd)
  173. if (containerCenterStart > childStart && containerCenterStart < childEnd) {
  174. currentKol = childDataKol;
  175. // console.log(currentKol + "-最小: " + targetLeft + ", 最大: " + targetLeft + targetW)
  176. // console.log("currentKol: " + currentKol)
  177. }
  178. });
  179. }
  180. }
  181. });
  182. });
  183. }
  184. }
  185. // 食譜頁
  186. function recipePageEvent(){
  187. setTimeout(function(){
  188. scrollDetectPercent();
  189. },500)
  190. // 播放食譜
  191. let isRecipeFirstPlay = false;
  192. $(".recipeWrapper .main").click(function(){
  193. if (!isRecipeFirstPlay) {
  194. gtag('event', "click", { [_keyName]: "btn_play" });
  195. isRecipeFirstPlay = true;
  196. }
  197. });
  198. // 播放/暫停食譜鍵
  199. $(".recipe__teaching").on("click",".plyr__control",function(){
  200. if ($(this).hasClass("plyr__control--pressed")) {
  201. gtag('event', "click", { [_keyName]: "btn_pauseicon" })
  202. }else {
  203. gtag('event', "click", { [_keyName]: "btn_playicon" })
  204. }
  205. });
  206. // 料理步驟
  207. $(".recipe__capture").click(function(){
  208. gtag('event', "click", { [_keyName]: "review_recipe" })
  209. })
  210. // 食材列表
  211. $(".recipe__basket").on("click",".recipe__ingredient",function(){
  212. let num = $(this).index() + 1
  213. let val = "btn_food" + num
  214. // console.log(val)
  215. gtag('event', "click", { [_keyName]: val })
  216. });
  217. // 名菜列表
  218. $(".recipe__more__box").on("click",".recipe__else",function(){
  219. let url = $(this).attr("href");
  220. let man = url.replace("./recipe.html?kol=","").toLowerCase();
  221. let val = "btn_" + man+"_food"
  222. // console.log(val)
  223. gtag('event', "click", { [_keyName]: val });
  224. });
  225. // 直接分享
  226. $(".recipe__btn-share").click(function(){
  227. gtag('event', "click", { [_keyName]: "btn_share" });
  228. });
  229. // 開啟濾鏡
  230. $(".recipe__btn-filter").click(function () {
  231. gtag('event', "click", { [_keyName]: "btn_filter" });
  232. })
  233. // 回首頁
  234. $(".recipe__goback a").click(function(){
  235. gtag('event', "click", { [_keyName]: "btn_backtoindex" });
  236. });
  237. // 一鍵下單
  238. $(".recipe__buy").click(function () {
  239. gtag('event', "click", { [_keyName]: "btn_shopcart" });
  240. });
  241. function scrollDetectPercent(){
  242. let unit = $(".recipeWrapper").height() / 100
  243. const reachNum = 50
  244. let percentH = unit * reachNum
  245. let isOverReach = false;
  246. // console.log(percentH)
  247. $(window).scroll(function(){
  248. if($(window).scrollTop()>percentH) {
  249. if(!isOverReach) {
  250. gtag('event', "scroll", { [_keyName]: "percent_50" });
  251. isOverReach = true;
  252. }
  253. }
  254. })
  255. }
  256. }
  257. // 素人食譜頁
  258. function recipePageOrdinaryEvent(){
  259. // 料理步驟
  260. $(".recipe__open").click(function(){
  261. let eventName = "amateur_"+ordinaryId+"_step"
  262. let eventKey = "amateur_"+ordinaryId
  263. gtag('event', eventName, { [eventKey]: "amateur_step" })
  264. })
  265. // 直接分享
  266. $(".recipe__btn-share").click(function(){
  267. let eventName = "amateur_"+ordinaryId+"_share"
  268. let eventKey = "amateur_"+ordinaryId
  269. gtag('event', eventName, { [eventKey]: "amateur_share" });
  270. });
  271. // 一鍵下單
  272. $(".recipe__buy").click(function () {
  273. let eventName = "amateur_"+ordinaryId+"_btn_shopcart"
  274. let eventKey = "amateur_"+ordinaryId
  275. gtag('event', eventName, { [eventKey]: "amateur_shop" });
  276. });
  277. function scrollDetectPercent(){
  278. let unit = $(".recipeWrapper").height() / 100
  279. const reachNum = 50
  280. let percentH = unit * reachNum
  281. let isOverReach = false;
  282. // console.log(percentH)
  283. $(window).scroll(function(){
  284. if($(window).scrollTop()>percentH) {
  285. if(!isOverReach) {
  286. gtag('event', "scroll", { [_keyName]: "percent_50" });
  287. isOverReach = true;
  288. }
  289. }
  290. })
  291. }
  292. }
  293. // search 頁面
  294. function searchPageEvent(){
  295. // 點擊素人 - 人像
  296. $(".search__kv__container").on("click",".search__kv__slide",function(){
  297. let eventName = "amateur_"+ $(this).data("ordinary");
  298. console.log(eventName)
  299. gtag('event', eventName, { "justahour_search": "banner_enter" });
  300. });
  301. // 點擊素人 - 菜色名
  302. $(".search__kv__container").on("click",".search__kv__dish",function(){
  303. let eventName = "amateur_"+ $(this).data("dish");
  304. console.log(eventName)
  305. gtag('event', eventName, { "justahour_search": "banner_enter" });
  306. });
  307. // 點擊尋找料理靈感tag
  308. $(".search__form").on("click",".search__form__item",function(){
  309. let eventName = "filter_"+ $(this).data("tag");
  310. console.log(eventName)
  311. gtag('event', eventName, { "justahour_search": "filter_function" });
  312. });
  313. // 搜尋菜色 按鍵
  314. $(".search__form").on("click",".search__form__cta",function(){
  315. gtag('event', "result_dish", { "justahour_search": "filter_function" });
  316. });
  317. // 清除全部 按鍵
  318. $(".search__form").on("click",".search__form__clear",function(){
  319. gtag('event', "result_clear", { "justahour_search": "filter_function" });
  320. });
  321. }
  322. {
  323. $(document).ready(function () {
  324. init();
  325. });
  326. }
  327. //public
  328. return {
  329. };
  330. };
  331. var gaEvent = new gaEvent();