Code

Google Analytics Top Content Widget with Analytify plugin

Google Analytics Top Content Widget with Analytify plugin

An addon plugin to WP Analytify that adds new shortcode: analytify_top_content.

Why do I need it?

To show accurate most popular posts list without killing your server/database

Normally, top content / most popular posts are calculated by adding “+1” in database each time somebody visits certain post or page. This is fine, when there are not many visitors, but makes your database very busy once there are more visits. Caching? Either will stop counting of new visits, or will be passed by through javascript what will lead to heavy load again. But probably – as most of the sites – you’re using analytics. Right? And in analytics panel you can see most visited pages for certain date range. Why not use it instead of duplicating this existing feature on your website by yet another popular posts plugin? That’s exactly what we do here. To keep things simple and don’t repeat what was already done by numerous other plugins, we base our code on free WP Analytify plugin – this way you don’t have to authorize Analytics again if you’re already using it (... or you’ll get some extra analytics within WP admin panel once you install it).

How it works?

Shortcode allows users to fetch top pages from Google Analytics and show them as a list. Global options page allows you to exclude some particular pages across whole website if you’re using more than one shortcode on it. “Hostname” parameter was added to allow WPML / Polylang / other languages solutions work well (this way you can skip particular domain in particular place).

Technical details: when shortcode is fired – plugin will look for data stored in WordPress internal cache (transient). If no luck – new request through Analytify will fetch data from Analytics and keep it in transient for another visitors.

How will it look like?

As you wish! By default, it’s a list of titles with links. If you want to modify default outlook – simply copy “shortcode.php” file from templates directory to “wp-analytify-shortcodes” directory withing your (child) theme. If you want to have different outlook for different post types, simply add there “shortcode-POST_TYPE.php” file – for example “shortcode-post.php”.

Example of code to use in case you’d like to load additional post data, like featured image:

<ul> <?php foreach($rows as $row): ?> <li> <?php global $post; //you can show thumbnail this way echo get_the_post_thumbnail($row->getPostId(), 'thumbnail'); // you can load all post data this way setup_postdata( $post ); //..and then load your template part get_template_part('template-parts/some_template'); //don't forget to reset postdata! wp_reset_postdata(); ?> </li> <?php endforeach; ?> </ul>

How to use?

Available parameters:
  • period – defines time period we want to get (like: “2 months”, “last month”, “last 3 weeks”). Required
  • to – when period does not contain a word “last” we need to define point in time which is the last day of required period (so 2 weeks to 2018-12-01 is between 2018-11-14 and 2018-12-01)
  • limit – number of rows we want to show
  • exclude-home-page – pass “yes” if you want to exclude home page (which gets the most number of views usually) from results
  • post-type – required post type in results
  • title-remove – text we want to remove from the page title
  • except-ids – post IDs we want to skip from the result
  • except-urls – post urls we want to skip from the result
  • hostname – adds ability to filter by hostname (i.e. page in different language). Useful if you use WPML, Polylang, or Multisite in different domain/subdomain mode and want to limit results to current domain only.
  • exclude-items – adds ability to exclude “parents”` or “children”. Useful for example if you want to show only top level pages or only popular child pages.

This plugin has global settings support for except-ids, except-urls and exclude-home-page options

Do you like this plugin?

Changelog

**1.3.1 (2019-05-09)**
* FIX: Invalid class called for admin settings

**1.3.0 (2019-05-03)**
* NEW: Ability to exclude top pages or child pages
* IMPROVEMENT: Code refactoring

**1.2 (2019-01-15)**
    * NEW: Global options for Top Content shortode (found in Analytify -> Settings -> Shortcodes global settings)

**1.1 (2019-01-14)**
    * NEW: Possibility to exclude certain post IDs or urls from appearing in the result
    * NEW: Ability to select base hostname from Google Analytics (i.e. for different language pages)

**1.0 (2018-12-27)**
    * First release

Support available in Polish and English!

by
by
by
by
by
by