amphtml icon indicating copy to clipboard operation
amphtml copied to clipboard

Add element-level overrides for variables for amp-analytics

Open rudygalfi opened this issue 10 years ago • 41 comments

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

rudygalfi avatar Jan 05 '16 19:01 rudygalfi

@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.

rudygalfi avatar Mar 04 '16 03:03 rudygalfi

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 avatar Mar 04 '16 10:03 georgecrawford

@georgecrawford Can you detail the intended use of barrier?

rudygalfi avatar Mar 07 '16 23:03 rudygalfi

Nothing special, just a value I can send in my analytics payload to indicate the presence of a paywall barrier on the current page.

georgecrawford avatar Mar 07 '16 23:03 georgecrawford

Any further thoughts on this?

georgecrawford avatar Mar 16 '16 13:03 georgecrawford

@georgecrawford I don't think this issue will be able to address what you are asking for. Lets continue the discussion on #2476.

avimehta avatar Mar 16 '16 21:03 avimehta

:+1:

georgecrawford avatar Mar 16 '16 21:03 georgecrawford

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.

rudygalfi avatar Apr 18 '16 22:04 rudygalfi

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.

avimehta avatar Apr 18 '16 22:04 avimehta

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>

senthilp avatar Jun 07 '16 05:06 senthilp

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?

avimehta avatar Jun 13 '16 22:06 avimehta

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 avatar Jun 14 '16 22:06 dvoytenko

@dvoytenko In that case will attribute "track" will be whitelisted from the AMP validation? or can we go with "data-track"

rajkumarsrk avatar Jun 16 '16 00:06 rajkumarsrk

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?

dvoytenko avatar Jun 16 '16 01:06 dvoytenko

@senthilp @rajkumarsrk Any interest in putting together a PR for this feature?

rudygalfi avatar Jun 17 '16 19:06 rudygalfi

Yes that is the plan. We are waiting on @avimehta to finalize the format for the track attribute.

senthilp avatar Jun 17 '16 19:06 senthilp

We could implement this in two ways.

  1. Prescribe only one attribute that can be specified by markup that amp-analytics reads and fills in.
  2. 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.

avimehta avatar Jun 20 '16 22:06 avimehta

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).

rudygalfi avatar Jun 20 '16 23:06 rudygalfi

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.

Gregable avatar Jun 20 '16 23:06 Gregable