如何向Google Analytics gtag添加动态参数?可能存在这样的情况:我想传递特定操作的自定义维度(链接)并将其从不同的操作中排除。下面是一个示例,我试图弄清楚如何生成动态参数。
<script
src="https://code.jquery.com/jquery-3.2.1.min.js"
integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
crossorigin="anonymous"></script>
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-123456"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-123456', {
'custom_map': { 'dimension1': 'Page',
'dimension2': 'View',
'dimension3': 'Page Area',
'dimension4': 'GUID',
'dimension5': 'Link'}
});
$(document).ready(function () {
$.trackPageAction = function(pageObj) {
gtag('event', 'page_view', {
'event_category': pageObj.category,
'event_action': pageObj.action,
'event_label': pageObj.label,
'Page': pageObj.page,
'View': pageObj.view,
'Page Area': pageObj.area,
'GUID': <?php echo session('customer_id'); ?>
});
<!-- HOW WOULD I ADD A DYNAMIC PARAMETER?? -->
<!--
if(typeof pageObj.link != 'undefined'){
gtag['Link'] = pageObj.link;
}
-->
}
});
</script>
<!-- WITH LINK PARAM -->
<a href="#" onclick="$.trackPageAction({category : 'Navigation', action : 'Page Navigation', label : 'Timeline Page', view : 'you', area : 'Footer', page : 'timeline', 'Link' : 'http://yahoo.com'})">TIMELINE</a>
<!-- WITHOUT LINK PARAM -->
<a href="#" onclick="$.trackPageAction({category : 'Navigation', action : 'Page Navigation', label : 'About Page', view : 'you', area : 'Footer', page : 'about'})">ABOUT</a>
答案 0 :(得分:-1)
您可以在将JavaScript对象传递给函数之前创建它。
$(document).ready(function () {
$.trackPageAction = function(pageObj) {
let options = {
'event_category': pageObj.category,
'event_action': pageObj.action,
'event_label': pageObj.label,
'Page': pageObj.page,
'View': pageObj.view,
'Page Area': pageObj.area,
'GUID': '<?php echo session('customer_id'); ?>'
};
if(typeof pageObj.link != 'undefined'){
options['Link'] = pageObj.link;
}
gtag('event', 'page_view', options);
}
});