Add element-level overrides for variables for amp-analytics
Offshoot from #871.
More detail from @avimehta:
The idea here is to enable certain variable values to be specified in the DOM. For example, sometimes, links/buttons can have additional data-* attributes associated with them in the DOM. amp-analytics could read these to fill in some variable templates.
cc @btownsend
@avimehta I think this is one of the next important projects. Could you put together a design? Let me know if you need any input.
I'm interested in proving a barrier variable to analytics calls, which would depend on the result of the AUTHDATA(access) field as a result of the amp-access callback. So barrier would be true if access was not granted, and false if the user is able to read the full content. Would this request be satisfied by this ticket, perhaps? I could set a variable value for use in amp-analytics, but vary it using amp-access="request" and amp-access="NOT request" markup.
@georgecrawford Can you detail the intended use of barrier?
Nothing special, just a value I can send in my analytics payload to indicate the presence of a paywall barrier on the current page.
Any further thoughts on this?
@georgecrawford I don't think this issue will be able to address what you are asking for. Lets continue the discussion on #2476.
:+1:
Hi, any word on when this will be complete?
I'm working on implementing KISSMetrics tracking using amp-analytics, and this feature would really help with pushing custom event data to KM. (For example, when we send a clicked link event to KM, we encode the title of the linked page in a data-title attribute on the tag.)
@cdukes We haven't gotten to this yet. We're working on the element-level trigger support (#1297) first.
PRs are obviously welcome. We needed per-event variables to get this done and I sent out a PR for that recently. It should be a straightforward addition to support what this issue is asking for. Unfortunately as @rudygalfi mentioned, #1297 is higher priority for now.
One suggestion would be to have the on attribute on elements with values track: prefixed, which will be replaced in the request URL query string when the event happens. This is similar to how we attach events to components like lightbox, sidebar etc. For instance
<a href="#deals" class="deals" on="track:p1233|m222|l111">Daily Deals</a>
In this case if the requests.event URL in the config is https://example.com/analytics?eid=${eventId}&sid=${track}, when the event happens ${track} will be replaced by p1233|m222|l111 and the beacon will be fired.
One thing to note here, the same element can have other events associated with it. For e.g. it should also open a amp-lightbox component. In that case we should come up with a syntax to handle multiple events. Is there one already?
The other approach would be as mentioned above use a data-track attribute, whose value will be replaced in the request URL query string when the event happens
<a href="#deals" class="deals" data-track="p1233|m222|l111">Daily Deals</a>
I personally like the second proposal. on attribute means something different and overloading the meaning might not be desirable. for example on="click..." does something on clicks. track is not an action though. It is usually click or tap.
The negative of using the second proposal is that some data might have to be duplicated in on and data-*. I think duplication is okay because clarity wins.
@dvoytenko what do you say?
I'll be pretty happy with a track="" attribute - its meaning will be clear and all DOM bubbling/nesting behavior will be obvious. The format itself is tbd.
@dvoytenko In that case will attribute "track" will be whitelisted from the AMP validation? or can we go with "data-track"
Yes, I'd prefer we whitelist track for validation. But first let's confirm a more complete format so that we can make this decision easier. @avimehta could you please summarize this solution?
@senthilp @rajkumarsrk Any interest in putting together a PR for this feature?
Yes that is the plan. We are waiting on @avimehta to finalize the format for the track attribute.
We could implement this in two ways.
- Prescribe only one attribute that can be specified by markup that
amp-analyticsreads and fills in. - Read all the
data-attributes on the clicked element and provide the values for substitution.
A solution like (2) allows one to have markup like this:
<a href="#menu" data-event="click" data-eventLabel="menu" data-eventValue="1234">Link</a>
When the link is clicked, a request of format http://example.com?ev=${event}&el=${eventLabel}&ev=${eventValue} will be expanded to http://example.com?ev=click&el=menu&ev=1234
We can have prescribe use of camelCase for data- attributes and have a limit on the value of the attributes but I think both these are optional.
The disadvantage of (2) is that validation becomes difficult.
Looping in validator folks to help with design direction from validation point of view: cc @Gregable @powdercloud.
To summarize so far, it sounds like we won't go with
on="track:p1233|m222|l111"
Instead, we'll use one or several attributes as @avimehta outlined. The decision point is whether to allow just one or many.
@avimehta, I'm assuming in your proposal (1) above, that would require something like
<a href="#menu" data-track="event=click,label=menu,value=1234">Link</a>
On the other hand, (2) is as you described:
<a href="#menu" data-event="click" data-eventLabel="menu" data-eventValue="1234">Link</a>
And the question hinges on whether (2) presents validation challenges.
I'm leaning toward (1) but have no issues with (2) if it's technically feasible.
Can we talk about the substitution variable part of the picture?
For (1), I'm assuming we'd expose as something like TRACK or DATA_TRACK.
For (2), it could look like DATA(varName) (inspired by AMP Access' AUTHDATA).
Preface: I'm not certain I follow this discussion completely. The data-* attributes are allowed by the validator universally on all tags already with any value, so if all you want is that the validator allows these, we're done already.
If on the other hand, you want to validate the specific attribs/values, then we will be removing allowed currently states from validation. Today, any combination of these attribs are allowed, in the future, some combinations will not be allowed. That's not ideal if any of these are in use already, though in general that's unlikely.