Home Forums WC Vendors Pro Support Google Analytics Code for Vendors

NOTICE: We've Moved to a Ticket System for Support

As of August 31, 2017 (12am EST) our support forums will be retired (read-only), and we will be moving to a support ticket system.  This will allow us to better organize and answer support requests, and provide a more personalized experience as we assist our customers.

For the time being, we will leave our forums open for reading and learning while we work on creating a more robust Knowledge Base for everyone to use.

If you are a WC Vendors Pro customer please open a support ticket here. 

If you are a WC Vendors user please open a support ticket on the Wordpress.org forums.

The information on this forum is outdated and in most instances no longer relevant. Please be sure to check our documentation for the most up to date information.

https://docs.wcvendors.com/

Thank you to all of our customers!

 

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #32394
    David R
    Participant

    Below is the code I am using to track views of products for each vendor with a Custom Dimension in Google Analytics. I figured some people would find it helpful.

    I also have a couple questions about improving, which I’ll post after the code.

    //Google Universal Analytics
    function google_analytics() { ?>
    <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');
      <?php if (is_singular('product')) :
        $this_post = get_queried_object();
        $author_id = $this_post->post_author;
      ?>
     ga('set', 'dimension1', '<?php echo get_the_author_meta('display_name', $author_id); ?>');
      <?php endif; ?>
     ga('send', 'pageview');
    </script>    
    <?php }
    add_action ('wp_head', 'google_analytics');

    This code, of course, only works if you’ve already setup a custom dimension in your Analytics account, and ‘dimension1’ ought to be changed to whatever is your custom dimension’s index.

    So, my questions… There are two things I’d like to change about this code. I’m sure their easy, otherwise I wouldn’t be bothering you, but I’m not familiar enough with the WCV functions to know the answers.

    1 – I’d like to use the Vendor’s store name rather than the Vendor’s name. So, what function would I call instead of $this_post->post_author in order to get the store name?

    2 – Secondly, I’m presently only tracking this custom dimension on product pages, but I’d like to include store pages (eg. /shops/Joes-Shoes/ ) as well. What post type could I add to if (is_singular('product')) in order to include vendor’s shop pages, but not other pages or the main shop page?

    Thanks a lot. It’s my hope that this could actually become a Pro feature, in that the Dashboard could have a stats tab for vendors to view their own pageviews by this custom dimension.

    #32401
    WC Vendors Support
    Participant

    1 – I’d like to use the Vendor’s store name rather than the Vendor’s name. So, what function would I call instead of $this_post->post_author in order to get the store name?

    The correct function to fetch the vendors store name would be get_user_meta( $user_id, 'pv_shop_name', true ); where $user_id is the vendors/post_authors user ID

    2 – Secondly, I’m presently only tracking this custom dimension on product pages, but I’d like to include store pages (eg. /shops/Joes-Shoes/ ) as well. What post type could I add to if (is_singular('product')) in order to include vendor’s shop pages, but not other pages or the main shop page?

    WCV_Vendors::is_vendor( $vendor_id ) would tell you if the user ID is a vendor, so you could use that to check the post_author ID.

    WCV_Vendors::get_vendor_shop_page( $vendor_id ) would tell you the shop page for the vendor, so if you compared that to the page you are on, that could work too.

    Have fun and nice code work!

    #32432
    David R
    Participant

    Thanks, Ben. Point 1 works.

    The trouble with point 2 is that $vendor_id doesn’t exist on the vendor’s shop page [atleast not at the point this code is run] and the queried object has no ID for the vendor’s shop page. So, I’m not sure how it’s possible to first find the vendor_ID in order to get_vendor_shop_page with it.

    #32434
    David R
    Participant

    I can begin with

    if ( is_post_type_archive('product') ) { // Since the vendor's shop page returns true here
        
    }

    But as an archive page, I think my only recourse might be to get the url via PHP, not a WP function. Parse the url and grab the last chunk, which would be equal to the store name, roughly. Then use that to find the actual store name. But that would really suck. 🙂 Tell me I’m wrong. Tell me there’s another way to get the vendor_id or the Shop Name.

    #32448
    Jamie
    Keymaster

    Hello,

    Thanks for this snippet, I’ve tidied it up a bit and made it work for both single pages and the shop pages.

    https://gist.github.com/digitalchild/f05852d925c8f00fa83aaace210dbee3

    cheers,

    Jamie.

    #32450
    David R
    Participant

    I actually cleaned mine up and switched it up. Since it’s best practice to enqueue scripts…

    function google_load_file() {
        if ( is_post_type_archive('product') && !is_shop() ) {
            $url = get_permalink();
            $item_id = url_to_postid( $url );
            $vendor = get_post_field( 'post_author', $post_id );
            $name = get_user_meta( $vendor, 'pv_shop_name', true );
        } elseif ( is_singular('product') ) {
            $this_post = get_queried_object();
            $name = get_user_meta( $this_post->post_author, 'pv_shop_name', true );
        }
    	wp_enqueue_script( 'author-tracking', get_stylesheet_directory_uri() . '/js/google.js', array(), NULL, false );
    	wp_localize_script( 'author-tracking', 'author', array( 'name' => $name ) );
    }
    add_action( 'wp_enqueue_scripts', 'google_load_file' );

    google.js sits in the child theme /js/ folder and looks like

    (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', 'Property ID HERE', 'auto');
    ga('set', 'dimension1', author.name );
    ga('send', 'pageview');


    @digitalchild
    , I think I’ll have to combine ours, since yours is clearly more efficient.

    #32453
    Jamie
    Keymaster

    Hello,

    Nice and clean code there, I was going to enqueue with localization but I was feeling lazy 🙂 I’ll make sure this code gets added to our KB.

    Thanks again !

    cheers

    Jamie.

Viewing 7 posts - 1 through 7 (of 7 total)
  • The forum ‘WC Vendors Pro Support’ is closed to new topics and replies.