2017-08-19 45 views
0

首先,瞭解我只知道基本的編程。我不編程自己。提前感謝您瞭解我的情況,也許您可​​以幫助我。使用Google Analytics進行窗口小部件事件跟蹤

我想跟蹤用戶點擊我的號召性用語側邊小部件的次數。問題是有人爲我做了這件事,我不知道在哪裏放置跟蹤代碼。

小部件代碼位於主題functions.php中。下面是代碼:

// Register and load the widget 
function wpb_load_widget() { 
    register_widget('wpb_widget'); 
} 
add_action('widgets_init', 'wpb_load_widget'); 

// Creating the widget 
class wpb_widget extends WP_Widget { 

function __construct() { 
parent::__construct(

// Base ID of your widget 
'wpb_widget', 

// Widget name will appear in UI 
__('Call to action', 'wpb_widget_domain'), 

// Widget description 
array('description' => __('This is a call to action widget', 'wpb_widget_domain'),) 
); 
} 

// Creating widget front-end 

public function widget($args, $instance) { 
$title = apply_filters('widget_title', $instance['title']); 
$wtext = apply_filters('widget_title', $instance['wtext']); 
$link = $instance['link']; 
$btnTxt = $instance['btnTxt']; 

// before and after widget arguments are defined by themes 
echo $args['before_widget']; 
echo '<div class="widgetbody">'; 
if (! empty($title)) 
echo $args['before_title'] . $title . $args['after_title']; 
echo '<img src="'.get_template_directory_uri().'/img/imagewidget.jpg">'; 

echo $args['before_title'] .''. $args['after_title']; 
echo '<center><h5 style="text-transform: none;">'. $wtext .'</h5></center>'; 
echo '<center><a class="btn btn-primary" href="'.$link.'">'.$btnTxt.'</a></center>'; 
echo '</div>'; 
echo $args['after_widget']; 
} 

// Widget Backend 
public function form($instance) { 
if (isset($instance[ 'title' ])) { 
$title = $instance[ 'title' ]; 
} 

if (isset($instance[ 'wtext' ])) { 
$wtext = $instance[ 'wtext' ]; 
} 

if (isset($instance[ 'link' ])) { 
$link = $instance[ 'link' ]; 
} 

if (isset($instance[ 'btnTxt' ])) { 
$btnTxt = $instance[ 'btnTxt' ]; 
} 
else { 
    $btnTxt = "Click Here"; 
} 
// Widget admin form 
?> 
<p> 
<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label> 
<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" /> 
</p> 

<p> 
<label for="<?php echo $this->get_field_id('wtext'); ?>"><?php _e('Text:'); ?></label> 
<input class="widefat" id="<?php echo $this->get_field_id('wtext'); ?>" name="<?php echo $this->get_field_name('wtext'); ?>" type="text" value="<?php echo esc_attr($wtext); ?>" /> 
</p> 

<p> 
<label for="<?php echo $this->get_field_id('link'); ?>"><?php _e('Button Link:'); ?></label> 
<input class="widefat" id="<?php echo $this->get_field_id('link'); ?>" name="<?php echo $this->get_field_name('link'); ?>" type="text" value="<?php echo esc_attr($link); ?>" /> 
</p> 

<p> 
<label for="<?php echo $this->get_field_id('btnTxt'); ?>"><?php _e('Button Text:'); ?></label> 
<input class="widefat" id="<?php echo $this->get_field_id('btnTxt'); ?>" name="<?php echo $this->get_field_name('btnTxt'); ?>" type="text" value="<?php echo esc_attr($btnTxt); ?>" /> 
</p> 
<?php 
} 

// Updating widget replacing old instances with new 
public function update($new_instance, $old_instance) { 
$instance = array(); 
$instance['title'] = (! empty($new_instance['title'])) ? strip_tags($new_instance['title']) : ''; 
$instance['wtext'] = (! empty($new_instance['wtext'])) ? strip_tags($new_instance['wtext']) : ''; 
$instance['link'] = (! empty($new_instance['title'])) ? strip_tags($new_instance['link']) : ''; 
$instance['btnTxt'] = (! empty($new_instance['btnTxt'])) ? strip_tags($new_instance['btnTxt']) : ''; 
return $instance; 
} 
} // Class wpb_widget ends here 

我重視與小部件的外觀上的後端和前端的圖片 Link to picture

現在不要笑我,但我試過要做的,就是改變線

echo '<center><a class="btn btn-primary" href="'.$link.'">'.$btnTxt.'</a></center>'; 

echo '<center><a class="btn btn-primary" href="'.$link.'" onclick="ga('send', 'event', 'Video course', 'Click', 'Side banner');">'.$btnTxt.'</a></center>'; 

當我這樣做時,當我登錄到wp-admin時,出現錯誤500。網站不會崩潰。我不得不進入c面板文件管理器將文件恢復爲原始格式。

請幫

+0

不是遺傳算法有這種跟蹤它的本地形式。現在不記得它的名字。 – Andreas

+0

我似乎無法再找到它了,但是當我使用Google Chrome瀏覽器時,出現了這個Chrome插件。https://support.google.com/analytics/answer/6047802?hl=zh-CN – Andreas

+0

我安裝了擴展程序, t請點擊側面小部件或關於此事的帖子內鏈接。它在所有其他方面都有所作爲。對我的情況不是特別有用。 –

回答

0

爲了追蹤事件,只需更換這行代碼:

echo '<center><a class="btn btn-primary" href="'.$link.'">'.$btnTxt.'</a></center>'; 

這一個:

echo '<center><a onclick="ga(\'send\', \'event\', \'Call-To-Action\');" class="btn btn-primary" href="'.$link.'">'.$btnTxt.'</a></center>'; 

任何時候BTN被點擊,它將向谷歌分析師發起一個名爲「呼籲行動」的活動。我在我的網站上成功使用了這個功能。

+0

如果我寫這篇文章的代碼 '代碼 回聲「

'.$btnTxt.'
」的結果;' 我得到「語法錯誤,意想不到的T_STRING,期待','或';' –

+0

這應該是一個錯字,現在我在打電話,不能檢查 –

+0

我已經編輯了答案,通過在ga函數中轉義字符 –

0

好吧,終於有人讓它工作。 正確的行是:

echo '<center><a class="btn btn-primary" href="'.$link.'" target="_blank"'; ?> onclick="ga('send', 'event', 'Video course', 'Click', 'Side banner');" <?php echo '>'.$btnTxt.'</a></center>'; 
相關問題