Viewing 15 replies - 1 through 15 (of 17 total)
  • Thread Starter Dashnorth

    (@dashnorth)

    I’ve also noticed in your code for the “clear cache option” that you assume everybody has as a table prefix wp_ for example I don’t have wp_options but cc666_options

    Thread Starter Dashnorth

    (@dashnorth)

    Reason why transient isn’t saved in DB might be because of Memcache

    Plugin Author thomtels

    (@totels)

    Having similar issues.

    Line 270 and 271 should be using $wpdb->options instead of hard-coding in wp_options as the table name (at the very least). This is also a pretty unreliable manner of deleting transients, hence the transients API having a delete_transient function. Any decently trafficked site is going to be using some sort of caching mechanism and transients will rightly be stored using wp_cache_set rather than the options API. It shouldn’t be assumed that any transient will ever make it to the options table.

    As for hitting API limits, it would be nice if there were some request backoff built in, as with above, any decently busy site is going to hit request limits quite quickly. I’ve been having to reauthenticate about every 3-6 hours due to Quota Error: profileId ga:NNNNNNNN has too many concurrent connections. messages.

    Plugin Author maximevalette

    (@maximevalette)

    @dashnorth So you probably hit the limit because you didn’t cache the values for an enough long time.

    @totels I fixed the wp_ prefix and added a way of handling other cache engines for transients.

    Thread Starter Dashnorth

    (@dashnorth)

    it’s still not working for me… nothing saved in db even after the last update.

    Plugin Author thomtels

    (@totels)

    @dashnorth the latest update still doesn’t save to the DB, everything is saved in transients. I created a custom plugin and added an action hook to remedy this for longer-term persistent caching. Flushing our object cache means all of the post pageviews get dumped and then we end up with all the problems again.

    add_action( 'setted_transient', function ( $name, $value, $expiration ) {
        // intercept gapp setting transient value, as long as it's not zero, and
        // add to the post meta for fallback value when gapp fails
        if ( false !== strpos( $name, 'gapp-transient-' ) and 0 !== intval( $value ) ) {
            preg_match( '/gapp-transient-(\d+)/', $name, $matches );
            update_post_meta( (int) $matches[1], 'analytics-pageviews', $value );
        }
    }, 10, 3 );

    This will create a “forever” cache of the latest non-zero result from google-analytics-post-pageviews in the postmeta table. You’ll want to add some logic to your template or elsewhere to adjust the output to use the postmeta value where necessary.

    e.g.:

    if ( function_exists( 'gapp_get_post_pageviews' ) ) {
      $views = intval( gapp_get_post_pageviews( null, false );
      if ( 0 === $views ) {
        $views = intval( get_post_custom_values( 'analytics-pageviews', get_the_ID() )[0] );
      }
    
      // use $views here to output the number of pageviews in the template
    }

    But realistically this isn’t going to help you stop hitting the concurrent connections limit, there’s nothing in the plugin that will do that, doesn’t matter what you set the cache timeout to, if 11 people visit your site in less than 1 sec it’s going to lock up and you’ll have to reauthenticate. I have written some custom code for that as well. Let me know if you’d like to see it.

    I still haven’t solved having the google auth data in the external object cache being dumped whenever we flush the cache and forcing us to reauthenticate. But at least the latest counts don’t get lost and our pages at least look like they’re supposed to.

    Thank you @totels !
    It worked like a charm!

    Thread Starter Dashnorth

    (@dashnorth)

    @totels wow!! thank you so much for this one! I’m going to implement it later today and see how it goes. If it’s not a problem for you I’d like to see the code you mentioned about the disconnection.

    P.s. I think this must be added in the plugin itself. And maybe think of another way to cache the results, as fallback..

    thank you totels.

    Where do I put this code to get it to work? I’ve posted previously but didn’t get a response. I set my cache to 6 hours but it still goes back to “0 Views” everytime I hit the limit. Please help!

    Hi Sarah,
    Place the “add_action” function code on the bottom of the plugin file, after the line “add_action(‘admin_notices’, ‘gapp_admin_notice’);”.

    And put the “if” condition code on your template. Ok?

    Best regards,
    Sergio

    Thank you so much. This is the code I’m trying to use but it’s breaking everything, I think my syntax is off, can you take a look:

    <?php if($show_post_stats): ?>
    <?php if ( function_exists( ‘gapp_get_post_pageviews’ ) ) {
    $views = intval( gapp_get_post_pageviews( null, false );
    if ( 0 === $views ) {
    $views = intval( get_post_custom_values( ‘analytics-pageviews’, get_the_ID() )[0] );
    } ?>
    <span style=”font-size: 14px; line-height: 27px; font-weight: 300; color: #b7b7b7;”>· <?php echo $views;?> Views</span>
    // use $views here to output the number of pageviews in the template
    } ?>
    <?php endif; ?>

    You can simplify a little bit:

    <?php
        if ($show_post_stats && function_exists('gapp_get_post_pageviews') ) {
            $views = intval( gapp_get_post_pageviews( null, false );
            if ( 0 === $views ) {
                $views = intval( get_post_custom_values( 'analytics-pageviews', get_the_ID() )[0] );
            } 
    
        echo '<span style="font-size: 14px; line-height: 27px; font-weight: 300; color: #b7b7b7;">'. $views. ' Views</span>';
        }
    ?>

    Hope that helps.

    Plugin Author thomtels

    (@totels)

    I would not recommend putting the code in the google-analytics-post-pageviews.php plugin file, you will lose that change when the plugin is updated. Create a new plugin of your own for this.

    This still isn’t working. Am I supposed to call the gapp_get_post_pageviews function somewhere in addition to using this?

    Hi Sarah,

    No, you don’t need to call the function on other places. But tell me, what happen on the page in the browser? Some error appears or just nothing? And what is this variable “$show_post_stats”, could you remove it form the IF conditional?

    Also check the post meta for the key/value “analytics-pageviews”. Just open a post on the dashboard and see if it has this key/value.

Viewing 15 replies - 1 through 15 (of 17 total)
  • The topic ‘No Cache Saved in DB’ is closed to new replies.