Plugin that modifies all widget query strings?


#1

I am writing my first Piwik plugin. Its purpose is to force every widget on the site to have a particular “segment” parameter in its URL (if none exists already). So all URLs of the form:

http://example.com/piwik/index.php?module=Foo&action=bar&idSite=2&

would automatically become (for example):

http://example.com/piwik/index.php?module=Foo&action=bar&idSite=2&…&segment=pageTitle!=Home

What is the correct hook that lets me access this information and modify it? I tried the hook Controller.renderView, which provides me with a ViewDataTable object, but I could not figure out how to modify the URL or API call from here.

Given the correct hook, I am hoping this plugin will take only a few lines to write, looking something like this:


class Piwik_MyStuff extends Piwik_Plugin {
  //...

  public function getListHooksRegistered() {
    return array( 'The.Right.Hook' => 'MyCallback' );
  }

  function MyCallback($x) {
    $stuff = $x->getTheNeededStuff();        // Magic. :-)
    $stuff['segment'] = 'pageTitle!=Home';
  }
}

Any advice on how to implement this?

Thank you very much.


(Matthieu Aubry) #2

That’s a good suggestion. Have you tried $_GET[‘segment’] =$yourSegment; ?


#3

I didn’t try that because I figured it was too late – by the time you have a ViewDataTable, the query string might already have been processed into some other form. But I’ll give that a try and let you know.


#4

It works for some widgets like “Last Visitors Graph” and “Visitor Browsers,” but not others like “Entry Pages”. Do I need a different hook?


(Matthieu Aubry) #5

Thanks for the suggestion.

I added a new hook “FrontController.dispatch” in: http://dev.piwik.org/trac/changeset/5402

See example to use it:


function getListHooksRegistered()
	{
		return array(
			'Menu.add' => 'addMenu',
			'FrontController.dispatch' => 'forceSegmentParameter',
		);
	}
	
	function forceSegmentParameter($notification)
	{
//		$params = array($controller, $action, $parameters);
		$params = &$notification->getNotificationObject();
		$segment = 'country==FR';
		$params[2]['segment'] = $segment;
		$_GET['segment'] = $segment;
	}