aws-analytics icon indicating copy to clipboard operation
aws-analytics copied to clipboard

Add support for audiences defined in code

Open goldenapples opened this issue 3 years ago • 2 comments

As a developer building a feature with complex business logic for an audience definition I would like to be able specify audiences in code To prevent editorial users from changing or deleting these audience values

The interface for defining audiences is great for those audiences which users can build on their own. However, there are some common audience definitions which are cumbersome to build out, or which should be enforced in code for legal compliance reasons. An example might be "visitors from countries subject to GDPR regulation".

I think a nice feature would be the ability to register read-only audiences in code which are merged with the user-defined ones.

goldenapples avatar Jun 23 '22 19:06 goldenapples

We can provide code samples of how this is being handled today in our project, but the process we had to take to "hard-code" an audience looked like this:

  • Filter on posts_results to create WP_Post objects for our custom audiences and set them in the cache global, short-circuiting future requests to those audience post objects and mocking their existence without any DB backing
  • Filter on get_post_metdata to return a schema-compliant audience object when querying one of our custom audiences with that meta key
  • Filter on posts_pre_query to do our own lookup in the event of a "field" => "ids" query and add the relevant IDs, because those queries don't go through other normal filters

A hook within get_audience_config and/or query_audiences would allow us to add new audience objects to the relevant lists, although it doesn't solve the problem of what to use for their ID: At present, the ID we are using for our custom audiences is a crc32( $audience_name ). This generates a pretty big integer, but there would still be theoretical conflict between this big integer and the space of possible post IDs.

kadamwhite avatar Sep 02 '22 14:09 kadamwhite

One additional preference for this issue: The custom audiences render within the list in the admin alongside the user-managed audiences, but they cannot be edited or re-ordered because they do not exist in the database. This means that you see "ordering" arrows image when viewing the list of audiences in the admin. image

A code-manged audience probably can't be easily reordered, but a solution to this ticket would ideally

  • Permit the code-managed audiences to be given a specific order in code upon registration
  • hide these arrows and otherwise update the UI so that we don't (a) imply that users can alter values they can't, and/or (b) explicitly call out that this audience is locked in code.

kadamwhite avatar Oct 03 '22 17:10 kadamwhite