使用谷歌分析进行小部件事件跟踪



首先,要明白我只知道基本的编程。我不自己编程。提前感谢您了解我的情况,也许您可以帮助我。

我想跟踪人们点击我的号召性用语侧小部件的次数。问题是有人为我制作了它,我不知道将跟踪代码放在哪里。

小部件代码位于主题函数.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>';

最新更新