1

On the page that I display the details of a single Channel entry, I have the following:

{exp:channel:entries
    channel="blogs"
    disable="categories|category_fields|pagination"
    dynamic="no"
    track_views="one"
    url_title="{segment_3}"
}

The URL where this page is displayed looks like this: www.domain.com/blogs/post/hello-world. Because I am not using the second segment to contain the entry id or URL title (I reserve that for the pagination and Low Search hash), I have to set dynamic="no" and url_title="{segment_3}".

The EE documentation says:

Single entry pages are ones that show only a single entry, specified dynamically by the ID number or URL Title in the URL, such as comment or permalink pages.

I have gone to "Tracking Preferences" and all tracking options are enabled. I have also set "Enable Caching?" to "No" for the template file containing my loop.

Does my setup mean I can't track views? Currently, I do not think it is working.

UPDATE 1:

In my page that lists all the blog entries, I create links using {url_title_path='blog/post'}. On my single entry page, I thought I had to set dynamic="no", but it doesn't seem that I need to. The following works:

{exp:channel:entries
    channel="blogs"
    disable="categories|category_fields|pagination"
    track_views="one"
    url_title="{segment_3}"
}

Have I met all the requirements to enable entry tracking? It's still not working for me, though. I am able to make comments work -- doesn't that have similar requirements as entry tracking?

EDIT 2:

Here's my entire template file (which is being embedded in another template):

{exp:low_replace find="\r|\n|\t" replace="" regex="yes"}
    {exp:channel:entries
        channel="blogs"
        disable="categories|category_fields|pagination"
        track_views="one"
        url_title="{segment_3}"
    }
        <h3>{title}</h3>
        <img src="http://placehold.it/640x260/" width="640" height="260" alt=""/>
        <ul class="inline post-details">
            <li>
                <span class="icon-user"></span>By <a href="/profile/#/">{author}</a>
            </li>
            <li>
                <span class="icon-time"></span>{exp:nice_time date="{entry_date}" format="{format.date.international}"}
            </li>
            <li>
                <span class="icon-comment"></span>{comment_total} Comments
            </li>
            <li>
                <span class="icon-eye-open"></span>{view_count_one} Views
            </li>
        </ul>
        {blog}
    {/exp:channel:entries}
    {embed="common/.comments"}
{/exp:low_replace}
  • 1
    When you say it's still not working - how are you testing it? Just on the edit entries backend or are you outputting {view_count_one} in the same channel:entries tag pair in the template? Also check on the database directly. – Peter Lewis Jul 14 '13 at 16:54
  • Hi Peter, I am checking the edit entries backend and I am outputting {view_count_one} in the same channel:entries tag pair in the template. I must be doing something fundamentally wrong. I'm not sure where in the DB to check, but I'm guessing there is no need since I don't see the view count in the CP. – ExpressionEngineNewbie Jul 14 '13 at 21:12
1
+50

Have you tried out relaxed_track_views? You'd add it to /system/expressionengine/config/config.php:

$config['relaxed_track_views'] = 'y';

Based on the comments you have Query Caching on, so in the end it wasn't a template issue at all - but needing to use the config.php option or turn off Query Caching.

| improve this answer | |
  • must I take this approach? Shouldn't it already be working as I have it configured? – ExpressionEngineNewbie Jul 15 '13 at 5:30
  • Well, I'm not actually sure why you have dynamic="no", since segment_2 will look for an entry_id or url_title. But since you do have dynamic set to no, I'd recommend at least trying this in the config.php. What are your concerns with using it? – Lisa Jul 15 '13 at 23:45
  • Hi Lisa, please look at my Update 1. I removed dynamic="no". I'd like to get this working without having to mess with config.php. It should would without me messing with it, right? – ExpressionEngineNewbie Jul 16 '13 at 0:15
  • EEN - have you ruled out all other causes of entry hit tracking not working? For instance, do you have query caching enabled? If so, you'll have to turn that off OR add the config.php option. – Lisa Jul 17 '13 at 15:57
  • I have gone through the docs to see if I'm setting things up properly. I think I am. Disabling query caching to make this works sounds like an expensive solution. I'm almost tempted to remove this feature. Are there alternatives to EE's view counter? – ExpressionEngineNewbie Jul 18 '13 at 0:14
3

Because view tracking has to be unique, if it's not based on a unique dynamic URL then EE doesn't know it's unique, so you could be rendering out multiple entries on the same URL but specifying different segments or even hard-coding url_title and EE won't know which one is the unqiue one for that URL/page.

To answer your question: no you can't use dynamic="no" and expect track_views parameter to save it.

So where does that leave you? Either you have to restructure your URLs so they can be dynamic or cheat:

Use an add-on: http://devot-ee.com/add-ons/external-entries

SQL:

UPDATE exp_channel_titles SET view_count_one = view_count_one + 1 WHERE url_title = "{segment_3}"

Or add PHP to a template (remember to activate PHP on template - set to output):

<?php
    $EE = & get_instance();
    $EE->db->query("UPDATE exp_channel_titles SET view_count_one = view_count_one + 1 WHERE url_title = '".$EE->uri->segment(3)."'");
?>

This shortened version might work (would need to test it):

<?php
    ee()->db->query("UPDATE exp_channel_titles SET view_count_one = view_count_one + 1 WHERE url_title = '".ee()->uri->segment(3)."'");
?>

Or even...:

<?php
    $this->EE->db->query("UPDATE exp_channel_titles SET view_count_one = view_count_one + 1 WHERE url_title = '".$this->EE->uri->segment(3)."'");
?>

Or neater:

<?php
    $this->EE->db->where(url_title, $this->EE->uri->segment(3));
    $this->EE->db->update('channel_titles', view_count_one = view_count_one + 1);
?>

Obviously this would need to be thoroughly tested! You will need EE conditionals around the PHP to prevent false triggering under different circumstances. Then if the conditional is false, the PHP won't get parsed by EE.

Also note, there's a good reason the EE query tags don't support UPDATE and INSERT, so by adding it to your template, not only could there be security issues, but it could also muck up your tables.

Edit: Updated to reflect Yuri's suggestion

| improve this answer | |
  • 1
    $this->EE->db->where('url_title', $this->EE->uri->segment(3)); is better, as it would work with any PHP parsing stage – Yuri Salimovskiy Jul 14 '13 at 12:42
  • Hi Peter, I've put an update to my question. – ExpressionEngineNewbie Jul 14 '13 at 13:29
0

Assuming that blogs is your template group, and post is your template, then you don't need to pass the url_title. So your opening Channel Entries tag should look like this:

{exp:channel:entries
    channel="blogs"
    disable="categories|category_fields|pagination"
    track_views="one"
}

Using the URL structure you mention, and this tag, track_views should indeed work.

Are you perhaps using a caching module like CE Cache? That would prevent the views from incrementing.

If you enable the output profiler, you can actually see which MySQL queries are being executed when the page loads - you should see an entry that looks like:

UPDATE exp_channel_titles SET view_count_one = (view_count_one + 1) WHERE url_title = 'hello-world';
| improve this answer | |
  • Hi Derek, I do have a blogs template group. However, I do not have a post template. My 'blog/index.html` has a check to see if the second segment is post and embeds the template for single entry page if so. – ExpressionEngineNewbie Jul 16 '13 at 0:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.