AudienceTargetingBundle ======================= If you want to display different content on the same URL based on some characteristics of the visitor, you can use the AudienceTargetingBundle for that. It allows the content manager to define the audience target groups on his own. Each target group has one or multiple rules, which are used to determine the target group of the visitor. Installation ------------ Enable the bundle in `config/bundles.php`: .. code-block:: php ['all' => true], Add the routes for the administration interface to the routing configuration file (`config/routes/sulu_admin.yaml`): .. code-block:: yaml # ... sulu_audience_targeting_api: type: rest resource: "@SuluAudienceTargetingBundle/Resources/config/routing_api.yml" prefix: /admin/api And the routes for the website in the corresponding configuration file (`config/routes/sulu_website.yaml`): .. code-block:: yaml # ... sulu_audience_targeting: resource: "@SuluAudienceTargetingBundle/Resources/config/routing_website.yml" Finally the cache has to be correctly configured. You have the choice between the Symfony Cache and Varnish. For the `Symfony cache`_ the audience targeting cache listener needs to be added. This is possible by adding a subscriber in the `getHttpCache()` method of the `src/Kernel.php`: .. code-block:: php public function getHttpCache() { if (!$this->httpCache) { $this->httpCache = new SuluHttpCache($this); $this->httpCache->addSubscriber(new AudienceTargetingCacheListener()); } return $this->httpCache; } If you want to use the more powerful `Varnish`_ instead, you have to install it on your machine and configure it using a VCL. The following will add full caching support including audience targeting for Sulu: .. code-block:: varnish4 # /etc/varnish/default.vcl vcl 4.0; include "/vendor/sulu/sulu/src/Sulu/Bundle/HttpCacheBundle/Resources/varnish/sulu.vcl"; include "/vendor/sulu/sulu/src/Sulu/Bundle/AudienceTargetingBundle/Resources/varnish/sulu.vcl"; acl invalidators { "localhost"; } backend default { .host = "127.0.0.1"; .port = "8090"; } sub vcl_recv { call sulu_recv; call sulu_audience_targeting_recv; # Force the lookup, the backend must tell not to cache or vary on all # headers that are used to build the hash. return (hash); } sub vcl_backend_response { call sulu_backend_response; } sub vcl_deliver { call sulu_audience_targeting_deliver; call sulu_deliver; } Finally you have to make sure that the bundle is correctly initialized. This includes the following steps: * Clear the Symfony cache with the `cache:clear` command or manually deleting the cache folder * Update the schema of your database with the `doctrine:schema:update` command or the `doctrine:migrations:diff` command * Make sure that the users the feature should be enabled for have the correct permissions Manually set target group ------------------------- Sulu will try to determine a matching target group based on the rules the content manager defines. But it is also possible to set a target group manually. That might be useful if you want to divide visitors into separate target groups based on some behavior, e.g. filling out a form, starting a download, etc. Therefore we have introduced the `TargetGroupStore`. You can simply call its `updateTargetGroupId` method and Sulu will do the rest for you. This would like this in an action of a Controller: .. code-block:: php get('sulu_audience_targeting.target_group_store') ->updateTargetGroupId($targetGroupId); } } .. note:: The target group that will be set manually should have quite a high priority, otherwise another higher prioritized target group might override that based on its defined rule. Create custom rules ------------------- The cool thing about target groups are the rules you can define on them, which will automatically evaluated by Sulu. There are a few rules built-in, like a referrer rule, browser rule or a page rule. However, you might still have a very specific use case, which requires to implement your own custom rule. Luckily this possibility is also built-in into Sulu. First of all you have to write your own implementation of the `RuleInterface`: .. code-block:: php .. note:: Mind that the `alias` of the tag has to be unique. .. _Symfony Cache: http://symfony.com/doc/current/http_cache.html .. _Varnish: https://www.varnish-cache.org/ .. _varnish-modules: https://github.com/varnish/varnish-modules