Ok so here's my stab in the dark...(好吧,这是我在黑暗中的刺...)
Not knowing much about your situation I've come up with the below code.(我不太了解您的情况,因此提出了以下代码。)
I've basically put the config for each tracking event into an array for looping through calling the same piece of code for each time.(我基本上已经将每个跟踪事件的配置放入数组中,以便每次都调用同一段代码来循环。)
It might be a bit overkill and get messy with future variations but it's something ˉ\_(ツ)_/ˉ(可能有点矫kill过正,并且对将来的变化感到混乱,但这有点ˉ\ _(ツ)_ /)
The method
property in the config objects relates to the function you are calling for each item eg jQuery('#heading-163').one(...)
vs jQuery('#heading-114 > div > div > div > a.outlet_text > img').on(...)
(config对象中的method
属性与您要为每个项目调用的函数相关,例如jQuery('#heading-163').one(...)
vs jQuery('#heading-114 > div > div > div > a.outlet_text > img').on(...)
)
var trackingMap = [
// COLLECTION MAIN
{
selector: '#heading-163',
track: 'main_collection',
method: 'one'
},
// SLNECNE OKULIARE MAIN
{
selector: '#heading-3',
track: 'main_sunglasses',
method: 'one'
},
// DIOPTRICKE OKULIARE MAIN
{
selector: '#heading-9',
track: 'main_glasses',
method: 'one'
},
// LYZIARSKE MAIN
{
selector: '#heading-14',
track: 'main_snow_goggles',
method: 'one'
},
// SPORTOVE MAIN
{
selector: '#heading-38',
track: 'main_sportove',
method: 'one'
},
// ZNACKY MAIN
{
selector: '#heading-48',
track: 'main_brands',
method: 'one'
},
// OUTLET MAIN
{
selector: '#heading-114',
track: 'main_outlet',
method: 'one'
},
{
selector: '#heading-114 > div > div > div > a.outlet_text > img',
track: 'main_outlet_left_img',
method: 'on'
},
{
selector: '#heading-114 > div > div > div > a:nth-child(3) > img',
track: 'main_outlet_right_img',
method: 'on'
},
{
selector: '#heading-114 > div > div > div > a:nth-child(3) > button',
track: 'main_outlet_btn',
method: 'on'
}
];
/**
* Loop through tracking map items
*/
for ( var i = 0; i < trackingMap.length; i++ ) {
/**
* A single tracking item config
* @type {Object}
*/
var trackingItem = trackingMap[ i ];
/**
* Check if method property is valid
*/
if ( typeof jQuery( trackingItem.selector )[ trackingItem.method ] !== 'undefined' ) {
/**
* Attach method listener
*/
jQuery( trackingItem.selector )[ trackingItem.method ]( 'click', function() {
console.log( trackingItem );
/**
* Check that exponea.track exists before calling
*/
if ( typeof exponea !== 'undefined' && 'track' in exponea ) {
exponea.track( trackingItem.track, {
action: 'click'
} );
}
} );
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…