簡體   English   中英

使用Google Analytics(分析)跟蹤小部件事件

[英]Widget event tracking with 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

我在圖片上附加了小部件在后端和前端的外觀,並鏈接到圖片

現在不要嘲笑我,但是我想做的就是改變思路

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-panel文件管理器才能將文件還原為原始格式。

請幫忙

為了跟蹤事件,只需在代碼中替換以下行:

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時,它將向Google Analystics觸發一個名為“號召性用語”的事件。 我正在我的網站上成功使用它。

好吧,終於有人使它起作用了。 正確的行是:

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>';

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM