• Resolved Pierre from DesignBots

    (@mecanographik)


    Hi,
    Google Api offers the possibility to filter by url path:
    filters=ga:pagePath=~/profile/123/*

    I would like to know if the following shortcode have this type of filter:
    [google_analytics_views]

    What would be great is to have a filter like this :
    [google_analytics_views pagepath=/profile/123/*]

    Is it possible using your filter:
    add_filter( ‘gtc_pages_filter’, ‘gtc_add_viewcount_title’ );

    https://fr.wordpress.org/plugins/google-analytics-top-posts-widget/

    Thanks for your answer

Viewing 11 replies - 1 through 11 (of 11 total)
  • Plugin Author Justin Sternberg

    (@jtsternberg)

    You can use the gtc_views_shortcode_ga_filters_param filter for this. Something like:

    
    function modify_gtc_views_shortcode_ga_filters_param( $filters, $atts, $link_uri ) {
    	$filters = 'ga:pagePath=~/profile/123/*';
    
    	return $filters;
    }
    add_filter( 'gtc_views_shortcode_ga_filters_param', 'modify_gtc_views_shortcode_ga_filters_param', 10, 3 );
    
    
    Thread Starter Pierre from DesignBots

    (@mecanographik)

    Hi Justin,
    Thanks for your quick answer. I’ve tried your snippet with different filters ga combination, but without luck.
    i want to display some goggle analytics stats for example on this page:
    https://www.time2sail.com/membres/jean-baptiste-gellee/

    I’ve some stats in my dashboard for this page path: ‘/jean-baptiste-gellee/’

    to display the stats on the same page i use this code:

    echo do_shortcode( '[google_analytics_views]Your profile has been visited count times.[/google_analytics_views]' );

    is this correct?
    so, what could i do?

    Thanks

    Thread Starter Pierre from DesignBots

    (@mecanographik)

    In addition, i have tested my google analytics query with this awesome tool:
    https://ga-dev-tools.appspot.com/query-explorer/

    in this tool, with those filters i get some results (i have changed my GA account ID in the example) :

    https://www.googleapis.com/analytics/v3/data/ga?ids=ga%123456798&start-date=30daysAgo&end-date=yesterday&metrics=ga%3Apageviews&dimensions=ga%3ApagePathLevel2

    when i convert it to use it with your filters ‘modify_gtc_views_shortcode_ga_filters_param()’ i have this:

    $filters = 'ids=ga:123456798&start-date=30daysAgo&end-date=yesterday&metrics=ga:3Apageviews&dimensions=ga:3ApagePathLevel2';

    Ok? But the filter doesn’t work… any idea?

    Thread Starter Pierre from DesignBots

    (@mecanographik)

    I have also noticed that your gist doesn’t neither work with the top-content widget on my wordpress installation:

    https://gist.github.com/jtsternberg/918238ff899c2762b41b

    when i applis those filters, i get no result with this param $params[‘metrics’] = ‘ga:uniquePageviews’ …

    but this works:

    function gtc_modify_request_metric( $params ) {
      
      // test for 'membres' slug
     
          $custom_filters = 'ga:pagePath=~/membres/*';
          $params['filters'] = urlencode( $custom_filters );
     
      return $params;
    }
    add_filter( 'gtc_analytics_top_content_shortcode_request_params', 'gtc_modify_request_metric' );
    add_filter( 'gtc_analytics_top_content_widget_request_params', 'gtc_modify_request_metric' );

    Interesting…

    Plugin Author Justin Sternberg

    (@jtsternberg)

    The results are cached. To bust the cache after changing your parameters, add ?delete-trans=1 to your URL.

    Thread Starter Pierre from DesignBots

    (@mecanographik)

    I have tested on this url :
    https://www.time2sail.com/membres/time2sail/profile/analytics/?delete-trans=1

    but it didn’t change anything, no rows…
    thanks for the tip i will keep this get param during my test…

    Thread Starter Pierre from DesignBots

    (@mecanographik)

    Where do you define this filter ‘gtc_analytics_top_content_shortcode_request_params’ ?
    This filter seems to work, i would like the same filter for ‘google_analytics_views’ shortcode. it looks like there are other params which generates 0 results when i apply only one filter (ga:pagePath=~/membres/*’;)
    thank for your help !

    to compare, this works:

    
    function gtc_modify_request_metric( $params ) {
      
      $params['dimensions'] = 'ga:pageTitle,ga:pagePathLevel2'; // 2nd level page
      $custom_filters = 'ga:pagePath=~/membres/*';
      $params['filters'] = urlencode( $custom_filters );
     
      return $params;
    }
    add_filter( 'gtc_analytics_top_content_shortcode_request_params', 'gtc_modify_request_metric' );

    this does not works (only the add_filter change):

    function gtc_modify_request_metric( $params ) {
      
      $params['dimensions'] = 'ga:pageTitle,ga:pagePathLevel2'; // 2nd level page
      $custom_filters = 'ga:pagePath=~/membres/*';
      $params['filters'] = urlencode( $custom_filters );
     
      return $params;
    }
    
    add_filter( 'gtc_views_shortcode_ga_filters_param', 'gtc_modify_request_metric' );

    Thanks again if you have a clue 😉

    Plugin Author Justin Sternberg

    (@jtsternberg)

    To make it easier to review, please wrap your code in the backticks (or use the comment editor “code” button).

    The gtc_views_shortcode_ga_filters_param filter can only be used as I demonstrated above. It does not pass the array of parameters. It only passes the filter string.

    If you want to modify the other parameters, you need to use the gtc_analytics_views_shortcode_request_params filter.

    
    function gtc_modify_request_metric( $params ) {
    
    	$params['dimensions'] = 'ga:pageTitle,ga:pagePathLevel2'; // 2nd level page
    	$custom_filters = 'ga:pagePath=~/membres/*';
    	$params['filters'] = urlencode( $custom_filters );
    
    	return $params;
    }
    
    add_filter( 'gtc_analytics_views_shortcode_request_params', 'gtc_modify_request_metric' );
    
    Thread Starter Pierre from DesignBots

    (@mecanographik)

    Thanks Justin,
    I have been trying do accomplish this during 6 hours without luck.
    I think [google_analytics_views] shortcodes use the current page url to generate the results. Are you able to make this filter (gtc_modify_request_metric) working on your sandbox (if you change ‘membres’ slug with another slug you use on your site ?
    My question is: With this shortcode, can i display on “Page A” the number of GA views of page “B” ? do we have to modify ‘$link_uri ‘ founded in plugin class in function views_shortcode() ? Please help.

    Thread Starter Pierre from DesignBots

    (@mecanographik)

    Hi, i have just realised that even without any add_filter on your shortcode doesn’t work anywhere on my wordpress site. For example when i paste the shortcode in my “contact” page i dont get anything… so this shortcode doesnt work :

    [google_analytics_views]count[google_analytics_views]

    but this one does work:

    [google_analytics_views]

    Thread Starter Pierre from DesignBots

    (@mecanographik)

    Justin,
    I have found the solution, but i had to replace a line in your plugin.
    Your method to retrieve current permalink is not ‘universal’ because it doesn’t work with content like buddypress profile or group page (they are not CPT).
    So, in class-ga-top-content, around the line #545, replace :

    $permalink = get_permalink( $atts['post_id'] ); // original method

    with this :

    /*
    Get current page URL in WordPress
    for Buddypress compatibility
    With this method, It doesn’t matter if you need it on single post, page, home, 
    category, tag, Buddypress user profile, group, custom post type or any other WordPress template.
    */
    global $wp;
    $permalink = home_url(add_query_arg(array(),$wp->request));
    /*
    end custom code
    */

    now it works!
    I will post an issue on your Github repo

    Thanks again, hope you will update your plugin soon or add a apply_filter to manipulate the current url.

    regards,

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Filter by page url (path) for [google_analytics_views]’ is closed to new replies.