Skip to content

Instantly share code, notes, and snippets.

@digitalchild
Created May 16, 2016 20:16
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save digitalchild/f05852d925c8f00fa83aaace210dbee3 to your computer and use it in GitHub Desktop.
Save digitalchild/f05852d925c8f00fa83aaace210dbee3 to your computer and use it in GitHub Desktop.
Google Analytics Code for Vendors
<?php
// Place this code in your themes functions.php
// Add google analytics code to vendor pages for tracking
function wcv_shop_name_google_analytics() {
if ( is_singular( 'product') ){
$product = get_queried_object();
if ( WCV_Vendors::is_vendor_product_page( $product->post_author ) ) {
$vendor_id = $product->post_author;
}
}
if ( WCV_Vendors::is_vendor_page() ) {
$vendor_shop = urldecode( get_query_var( 'vendor_shop' ) );
$vendor_id = WCV_Vendors::get_vendor_id( $vendor_shop );
}
$vendor_shop_name = get_user_meta( $vendor_id, 'pv_shop_name', true );
echo "
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'Analytics Property ID Goes Here', 'auto');
ga('set', 'dimension1', '".$vendor_shop_name."');
ga('send', 'pageview');
</script>";
} // wcv_shop_name_google_analytics()
add_action ('wp_head', 'wcv_shop_name_google_analytics');
@canyasa
Copy link

canyasa commented Apr 18, 2019

Is this still working without an issue?

@canyasa
Copy link

canyasa commented Apr 18, 2019

  • What does it track?

@digitalchild
Copy link
Author

This code is outdated and should not be used. Please see our documentation on how to do this now.

https://docs.wcvendors.com/knowledge-base/allow-vendors-to-add-google-analytics-tracking-code/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment