elgg _set_plugin_user_setting to save the last visited page

i want to save the last visited page of a user,

(get the guid and save it)

should i use elgg _set_plugin_user_setting to save it ? or is there better solution ?



okay, to avoid misunderstanding,  i will explain my purpose :

I want to make a feature :  Recommendation (show entities list that contains user favourite topic)

So i need data of user 's favourite topic

Simple example: Alex read Pokemon Go article , his favourite topic is Pokemon Go

I need the data of last 10 visited page (the title, url or just tags of the article)

I dont know how to save the data , elgg_set_plugin_user_setting , or annotations or something else (safe ,not use big resource and not make problem in the future)

i'm sorry if i make you confused


  • I would just set metadata or set an annotation.
    But I'm curious how would determine which page is the last one?
    Are you gonna do that on every page load?


  • yes , i'm gonna do that on every page load

    save and overwrite so i will get the last visited page

    will it be  a problem ?

  • Don't think so, but I think there are more efficient ways of tracking a user.

    You could look at something like Mautic, there is an elgg plugin somewhere on github for Mautic.
    It gives you a lot more options, is more scalable and you can track every user and what he has done.
    I looked at it briefly months back and I liked it a lot.

  • hmm ithink use Mautic plugin is not necessary for my web, now .

    i just want to get the last visited page of my user,

    okay i will use annotation (like what you said)

     create_annotation ($entity_guid, $name, $value, $value_type= '', $owner_guid=0, $access_id=ACCESS_PRIVATE)

    what should the code be like ?

  • create_annotation($user->guid, 'lastvisitedpage', $_SERVER['REQUEST_URI']);

    You will get 'a lot' of annotations if it's storing every pagevisit of every user.

  • You will get 'a lot' of annotations if it's storing every pagevisit of every user.

    what's the worst if i get a lot of annotations ?

  • I honestly don't know, I'm sure there are better experts to answer your question then I, but you 'could' do something like this, to store only the last 10 annotation and only update the oldest one:
    But again, I don't know if this is the most efficient way to do this.

  • I'm thinking updating isn't a good approach here, it might be better to just delete the oldest one and create a new one, just thinking with you...

    that would be this then:

    $user_guid = elgg_get_logged_in_user_guid()
    $count = (int)elgg_get_annotations(array(
    		'annotation_names' => array('lastvisitedpage'),
    		'annotation_owner_guids' => array($user_guid),
    		'count' => true,
       if ($count > 10) {
           $visitedpages = elgg_get_annotations(array(
    		'annotation_names' => array('lastvisitedpage'),
    		'annotation_owner_guids' => array($user_guid),
    create_annotation($user_guid, 'lastvisitedpage', $_SERVER['REQUEST_URI']);
  • and in the case you only want the latest one (I thought I read somewhere you wanted the last 10 visits, but can't find it anymore)
    It would be:

    $user_guid = elgg_get_logged_in_user_guid();
    if (elgg_annotation_exists($user_guid,'lastvisitedpage',$owner_guid = null)) {
    	$annotation = elgg_get_annotations(array(
    		'annotation_names' => array('lastvisitedpage'),
    		'annotation_owner_guids' => array($user_guid),
    		'limit' => 1,
    	update_annotation($annotation[0]->id, 'lastvisitedpage', $_SERVER['REQUEST_URI']);
    } else {
    	create_annotation($user_guid, 'lastvisitedpage', $_SERVER['REQUEST_URI']);

    But metadata would be much easier then, and you overwrite it on every visit:

    $user = elgg_get_logged_in_user_entity();
    $user->lastpage = $_SERVER['REQUEST_URI'];
  • okay it's too complicated for me what if i just use:

    $value = elgg_get_plugin_setting('lastvisited',$userguid,'lastvisited');

    $url = $entity->getURL();



    advantage: no too many annotation

    but i don't know the possible disadvantages . if there is no disadvantages i will use this one.