1. Home
  2. WC Vendors Pro
  3. Code Snippets
  4. Allow Vendors to Add Google Analytics Tracking Code

Allow Vendors to Add Google Analytics Tracking Code

We have had requests from marketplace admins to allow their vendors to add their Google Analytics tracking code to their stores and pages. The following code snippet will add this functionality.

Requirements

  • WC Vendors Pro
  • Google Analytics 3

Please Note: This has not been tested or created for the new Google Analytics 4

This will add a new field on the Vendor Dashboard Settings page after the store notice

Google Anlytics Tracking ID

Code


 /**
  * Add the Google Analytics Tracking ID field to the settings page for vendors 
  */
add_action( 'wcvendors_settings_after_vendor_store_notice', 'wcv_add_ga_code' ); 
function wcv_add_ga_code(){ 

	$value = get_user_meta( get_current_user_id(), '_wcv_custom_settings_ga_tracking_id', true ); 

	// Output GA property field data 
	WCVendors_Pro_Form_Helper::input(
		apply_filters(
			'wcv_vendor_ga_code',
			array(
				'id'            => '_wcv_custom_settings_ga_tracking_id',
				'label'         => __( 'Google Analytics Tracking ID', 'wcvendors-pro' ),
				'wrapper_start' => '
', 'wrapper_end' => '
', 'value' => $value ) ) ); } /** * Output the vendor google analytics code if they have added their tracking ID to their settings page */ add_action( 'wp_head', 'wcv_add_vendor_ga_code' ); function wcv_add_vendor_ga_code() { global $post; $vendor_id = 0; // Not on vendor store page or vendor single product bail out if ( WCV_Vendors::is_vendor_page() ){ $vendor_shop = urldecode( get_query_var( 'vendor_shop' ) ); $vendor_id = WCV_Vendors::get_vendor_id( $vendor_shop ); } elseif ( is_singular( 'product' ) && WCV_Vendors::is_vendor_product_page( $post->post_author ) ) { $vendor_id = $post->post_author; } $vendor_ga_code = wcv_output_vendor_ga_code( $vendor_id ); echo $vendor_ga_code; } /** * Output the vendor tracking code * * @param int $vendor_id - the vendor user ID * @return string $ga_code - the google analytics code */ function wcv_output_vendor_ga_code( $vendor_id ){ // Not a vendor? return nothing if ( ! WCV_Vendors::is_vendor( $vendor_id ) ) { return ''; } $vendor_tracking_id = get_user_meta( $vendor_id, '_wcv_custom_settings_ga_tracking_id', true ); // No tracking code added, return nothing if ( empty( $vendor_tracking_id ) ){ return ''; } $ga_code = sprintf(' <!-- Global site tag (gtag.js) - Google Analytics added by WC Vendors Pro --> <script async src="https://www.googletagmanager.com/gtag/js?id=' . $vendor_tracking_id . '"></script> <script> window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag(\'js\', new Date()); gtag(\'config\', \' ' .$vendor_tracking_id . ' \'); </script> ' ); return $ga_code; }

This code will output the gtags script on the vendor store page and the vendor single product pages as shown in the example below.

Does Google Allow this?

According to Google Analytics help you can have multiple tags on a single site. Some configurations may not be supported. You can read about this in more detail on the following Google Help page.

https://support.google.com/analytics/answer/1032400?hl=en

actions

Updated on February 11, 2021

Was this article helpful?