I’ve got a TV that I want to be tracking page hits for finding the most popular documents, so far I have:
<?php
$id = $modx->documentIdentifier;
$resource = $modx->getObject('modResource',$id);
$tvVal = $resource->getTVValue('ViewCount')
Which is fine I can get the value of the ViewCount and do whatever with it - I just don’t know how to save it again!
Any help?
-thanks
-sean
*** Not just websites, we also create signage, banners, print, trade show displays and more! ***
Sean Kimball CLP, CLS.
Technical Director / Sr. Developer | BigBlock Studios
._______________________________________________.
Bigblock Studios
http://www.bigblockstudios.ca Web site design & development.
27-1300 King Street East. Box 167 Oshawa, Ontario L1H8J4 Canada.
phone/fax: 905-426-5525
I ended up with this:
<?php
$docID = $modx->resource->get('id');
$doc=$modx->resource;
$tv = $modx->getObject('modTemplateVar',array('name'=>'ViewCount'));
$oldValue= $modx->getObject('modResource',$docID );
//echo $oldValue;
$newValue = $oldValue->getTVValue('ViewCount') + 1;
//echo $newValue ;
$tv->setValue($docID, $newValue);
$tv->save();
$doc->save();
feels messy, but it works... the clues were in this thread:
http://modxcms.com/forums/index.php?topic=52849.0
*** Not just websites, we also create signage, banners, print, trade show displays and more! ***
Sean Kimball CLP, CLS.
Technical Director / Sr. Developer | BigBlock Studios
._______________________________________________.
Bigblock Studios
http://www.bigblockstudios.ca Web site design & development.
27-1300 King Street East. Box 167 Oshawa, Ontario L1H8J4 Canada.
phone/fax: 905-426-5525
Looks good. FYI, there should be no need to retrieve or save the resource object. TV values are stored in a separate table, so this should be a little faster:
<?php
$docID = $modx->resource->get('id');
$tv = $modx->getObject('modTemplateVar',array('name'=>'ViewCount'));
$tv->setValue($docID, $tv->getValue($docID) + 1 );
$tv->save();
If you know the ID of the TV, you can replace the second line with this and it will be even faster:
$tv = $modx->getObject('modTemplateVar',$tvId);
Thanks Bob;
I’m all about speed, the revised code now looks like this
<?php
$docID = $modx->resource->get('id');
$tvId = 9;
$tv = $modx->getObject('modTemplateVar',$tvId);
$tv->setValue($docID, $tv->getValue($docID) + 1 );
$tv->save();
works perfectly on the ’onWebPageComplete’ event....
thanks again.
*** Not just websites, we also create signage, banners, print, trade show displays and more! ***
Sean Kimball CLP, CLS.
Technical Director / Sr. Developer | BigBlock Studios
._______________________________________________.
Bigblock Studios
http://www.bigblockstudios.ca Web site design & development.
27-1300 King Street East. Box 167 Oshawa, Ontario L1H8J4 Canada.
phone/fax: 905-426-5525
I’m glad it worked. You could also just make it a snippet and put a tag for it in your template.
I’m not sure if that would be faster or not.
Thanks Bob;
I think ’plugin’ is the way to go here - there are currently 2 different templates, possibly 1 or 2 more on the horizon... I’m a big fan of not duplicating any code if at all possible... even if it’s just to include a snippet
-sean
*** Not just websites, we also create signage, banners, print, trade show displays and more! ***
Sean Kimball CLP, CLS.
Technical Director / Sr. Developer | BigBlock Studios
._______________________________________________.
Bigblock Studios
http://www.bigblockstudios.ca Web site design & development.
27-1300 King Street East. Box 167 Oshawa, Ontario L1H8J4 Canada.
phone/fax: 905-426-5525
It’s tough to tell. With a plugin, the code will execute for all resources and all tempaltes, whether they have that TV or not, unless you put in some code to prevent that. Since it’s a pagecount snippet, though, that may be appropriate. Putting a snippet tag in a template, it will only execute for resources that use that template.
hmm - yes, it’s going to have to get smarter ... I guess we don’t want it counting 404 pages, login pages, utility pages basically, & we only really need it in the web context.
Utility pages will use the same ’generic’ template that most content pages will use so I guess, we need to set another TV that states ’I am a utility page - do not count me!’ - find that TV with the plugin & filter out any mgr context pages as well then also filter on the utility TV in the getResources call [which is the whole root of this - finding the most viewed pages]
Will have to work on that another day - been a long one today.
-sean
*** Not just websites, we also create signage, banners, print, trade show displays and more! ***
Sean Kimball CLP, CLS.
Technical Director / Sr. Developer | BigBlock Studios
._______________________________________________.
Bigblock Studios
http://www.bigblockstudios.ca Web site design & development.
27-1300 King Street East. Box 167 Oshawa, Ontario L1H8J4 Canada.
phone/fax: 905-426-5525