Filter by page url (path) for [google_analytics_views]
-
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
-
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 );
- This reply was modified 7 years, 5 months ago by Justin Sternberg.
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
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) :
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?
- This reply was modified 7 years, 5 months ago by Pierre from DesignBots.
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…
- This reply was modified 7 years, 5 months ago by Pierre from DesignBots.
The results are cached. To bust the cache after changing your parameters, add
?delete-trans=1
to your URL.I have tested on this url :
https://www.time2sail.com/membres/time2sail/profile/analytics/?delete-trans=1but it didn’t change anything, no rows…
thanks for the tip i will keep this get param during my test…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 😉
- This reply was modified 7 years, 5 months ago by Pierre from DesignBots.
- This reply was modified 7 years, 5 months ago by Pierre from DesignBots.
- This reply was modified 7 years, 5 months ago by Pierre from DesignBots.
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' );
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.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]
- This reply was modified 7 years, 5 months ago by Pierre from DesignBots.
- This reply was modified 7 years, 5 months ago by Pierre from DesignBots.
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 repoThanks again, hope you will update your plugin soon or add a apply_filter to manipulate the current url.
regards,
- This reply was modified 7 years, 5 months ago by Pierre from DesignBots.
- This reply was modified 7 years, 5 months ago by Pierre from DesignBots.
- This reply was modified 7 years, 5 months ago by Pierre from DesignBots.
- The topic ‘Filter by page url (path) for [google_analytics_views]’ is closed to new replies.