Added element-level overrides for variables for amp-analytics#4072
Added element-level overrides for variables for amp-analytics#4072avimehta merged 8 commits intoampproject:masterfrom rajkumarsrk:master
Conversation
|
Thanks for your pull request. It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). 📝 Please visit https://cla.developers.google.com/ to sign. Once you've signed, please reply here (e.g.
|
|
This is specific to click trigger, so documentation within https://github.com/ampproject/amphtml/blob/master/extensions/amp-analytics/amp-analytics.md#click-trigger-on-click would make the most sense. I was wondering if it makes any sense at all to broaden this capability to other trigger types? I guess specifically https://github.com/ampproject/amphtml/blob/master/extensions/amp-analytics/amp-analytics.md#vars |
|
@rudygalfi If you have seen requests for data-vars on visible event, we can add them there as well. I have not come across such requests yet. @rajkumarsrk Will look at the PR soon. You should mention the presence of these variables in https://github.com/ampproject/amphtml/blob/master/extensions/amp-analytics/amp-analytics.md#click-trigger-on-click but give details of how to use these variables in interaction section of https://github.com/ampproject/amphtml/blob/master/extensions/amp-analytics/analytics-vars.md. I do something similar for scroll triggers. |
|
@avimehta @rudygalfi . Sure, I will update the documentation |
|
I don't believe I've seen requests. My proposal was purely a hypothetical need and can be ignored for v1 so we just focus on |
|
@rudygalfi actually, If you think it makes sense, let's do it in this PR. I am happy with whatever makes more sense to you. |
|
I think it makes sense. If it's not much additional work, I'd encourage adding it (and corresponding doc updates). |
|
@rudygalfi we will look into it and update the PR with analysis for other trigger types. If it is simple enough we can accommodate it in this PR itself. |
|
@avimehta Is the Currently we have it as having the following would resemble the click trigger types By this way it would resemble the click eventType format where The reason for this question is, I am planning to pass the parsed values of data-attributes from the element level as a sibling to |
There was a problem hiding this comment.
Move this to top of the file with other contants?
|
looks good so far. @cramforce we talked about moving the selector one level up so that click and visible have the same place to specify selector. How do we go about doing a breaking change? Should we put a deprecation warning in the old format and start accepting the new format? |
|
It'll be tough to ever get rid of the old way (without bumping the |
|
@avimehta Thanks for reviewing the changes. I will update this PR with review changes and adding support for the element level overrides for "visible" event. |
|
@avimehta & @cramforce Is it expected that the selector for |
|
@rajkumarsrk I think it can also be a class name, e.g. |
|
Got the answer for this from console "Visibility spec requires an id selector" |
|
@rudygalfi Thanks Rudy. Looks like we support only id only |
|
@rajkumarsrk yes. We might want to add support for other elements formats at a later point but for now it is just ids. |
There was a problem hiding this comment.
I think the latest example brings up an issue with this line. Vars have been case sensitive till now. If we lowercase the key, the casing gets lost. Can we avoid doing toLowerCase?
There was a problem hiding this comment.
Update this to follow the same format of naming convention that is followed for other variables (camelCase) as in domain=${canonicalHost}&path=${canonicalPath}
data-vars-sub-title="Example sub title" will resolve to
{
"varsSubTitle": "Example sub title"
}
Which in turn is resolved to
{
"subTitle": "Example sub title"
}
|
This looks pretty good overall. While we are fixing the final few pieces of feedback and adding documentation, do you mind also signing the CLA? |
There was a problem hiding this comment.
you should extract the variables and merge them with vars here.
There was a problem hiding this comment.
For visibility.listenOnce we need both the visibilitySpec and the merged vars. Merging of vars is done on #L225
|
CLAs look good, thanks! |
|
@avimehta @rudygalfi Thanks for reviewing the changes . I have update the code & documentations |
| if (spec['selector']) { | ||
| const el = this.win_.document.getElementById(spec['selector'] | ||
| .slice(1)); | ||
| Object.assign(vars, this.extractAnalyticsVariables_(el)); |
There was a problem hiding this comment.
Object.assign is not available in all environments! This will fail!
There was a problem hiding this comment.
http://kangax.github.io/compat-table/es6/#test-Object_static_methods_Object.assign says that it is not supported IE11 and below, Safari 8 and below. But Babel and closure support it. What can we use here instead? Is a for-loop the best alternative if .assign is undefined?
There was a problem hiding this comment.
Modified to for loop instead of Object.assign
src/dom.js
Outdated
| const matches = attr.name.match(/^data-param-(.+)/); | ||
| const attrName = attr.name; | ||
| const matches = attrName.match(/^data-param-(.+)/) || | ||
| attrName.match(/^data-vars-(.+)/); |
There was a problem hiding this comment.
I don't think this will work. This changes the behavior for existing pages/clients. I think we should add another parameter to the function that accepts a regex called opt_paramPattern. If the value is not provided, you can default to /^data-param-(.+)/. Then from instrumentation.js, pass in the value of the pattern.
There was a problem hiding this comment.
Yup. Added opt_paramPattern and test case has been updated as well
|
Just a few more comments and we are good to go! Thanks for your patience so far. |
src/dom.js
Outdated
| * @param {!Element} element | ||
| * @param {function(string):string} opt_computeParamNameFunc to compute the parameter | ||
| * name, get passed the camel-case parameter name. | ||
| * @param {string} Regex pattern to match data attributes. |
There was a problem hiding this comment.
please change type to string= and add the parameter name after the type.
@param {string=} opt_paramPattern Regex pattern to match data attributes.
|
@jridgewell would you like to take another look? @rajkumarsrk I just have one small comment. |
|
@avimehta 👍 Updated JS doc |
|
@jridgewell I'll merge this PR(to unblock other PRs). Feel free to comment here with more suggestions changes though. I'll address those in a separate PR. |
|
@avimehta Can you let me know when this feature will be available to consume |
|
@rajkumarsrk I believe this made it into last week's canary build. That means it should be in production by this Friday the 19th. You can target's AMP's canary build if you want to try it out. Opt into the dev channel (first option) on this page: https://cdn.ampproject.org/experiments.html. |
|
Thanks Rudy |
…ject#4072) * Adding element-level overrides for variables on amp-analytics - click, visibile * Updating with review feedback changes * Using dom.js for extracting data attributes * Updating amp-analytics variable overrides with review feedback * Fixing lint errors * Updating documentation for amp-analytics
…ject#4072) * Adding element-level overrides for variables on amp-analytics - click, visibile * Updating with review feedback changes * Using dom.js for extracting data attributes * Updating amp-analytics variable overrides with review feedback * Fixing lint errors * Updating documentation for amp-analytics
Adding element level overrides for amp-analytics variable based on the discussion that we had
#1298 (comment).
Please review it and let me know your feedback
@avimehta @cramforce Could you suggest best place where I can update the documentation for this.
I was thinking to add it here
https://github.com/ampproject/amphtml/blob/master/extensions/amp-analytics/amp-analytics.md#vars. But will wait for you inputs