Skip to content

Added element-level overrides for variables for amp-analytics#4072

Merged
avimehta merged 8 commits intoampproject:masterfrom
rajkumarsrk:master
Aug 8, 2016
Merged

Added element-level overrides for variables for amp-analytics#4072
avimehta merged 8 commits intoampproject:masterfrom
rajkumarsrk:master

Conversation

@rajkumarsrk
Copy link
Copy Markdown
Contributor

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

@googlebot
Copy link
Copy Markdown

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. I signed it!) and we'll verify. Thanks.


  • If you've already signed a CLA, it's possible we don't have your GitHub username or you're using a different email address. Check your existing CLA data and verify that your email is set on your git commits.
  • If you signed the CLA as a corporation, please let us know the company's name.

@rudygalfi
Copy link
Copy Markdown
Contributor

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 visible now that it also has a selector attribute under visibilitySpec? cc @avimehta for feedback

https://github.com/ampproject/amphtml/blob/master/extensions/amp-analytics/amp-analytics.md#vars

@avimehta
Copy link
Copy Markdown
Contributor

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

@rajkumarsrk
Copy link
Copy Markdown
Contributor Author

@avimehta @rudygalfi . Sure, I will update the documentation

@rudygalfi
Copy link
Copy Markdown
Contributor

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

@avimehta
Copy link
Copy Markdown
Contributor

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

@rudygalfi
Copy link
Copy Markdown
Contributor

I think it makes sense. If it's not much additional work, I'd encourage adding it (and corresponding doc updates).

@senthilp
Copy link
Copy Markdown

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

@rajkumarsrk
Copy link
Copy Markdown
Contributor Author

@avimehta Is the selector inside the "visible" eventType has to a sibling property to on eventType.

Currently we have it as

"triggers": {
  "defaultPageview": {
    "on": "visible",
    "request": "pageview",
    "visibilitySpec": {
      "selector": "#anim-id",
      "visiblePercentageMin": 20,
      "totalTimeMin": 500,
      "continuousTimeMin": 200
    }
  }
}

having the following would resemble the click trigger types

"triggers": {
  "defaultPageview": {
    "on": "visible",
     "selector": "#anim-id",
    "request": "pageview",
    "visibilitySpec": {
      "visiblePercentageMin": 20,
      "totalTimeMin": 500,
      "continuousTimeMin": 200
    }
  }
}

By this way it would resemble the click eventType format where on, selector, request would be siblings

"links": {
        "on": "click",
        "selector": "#test1",
        "request": "click",
        "vars": {
          "label": "clickChapter::clickLabel",
          "level2Click": "12",
          "type": "a"
        }
      }

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 selector on config from createVisibilityListener_ on instrumentation

Copy link
Copy Markdown
Contributor

@avimehta avimehta Jul 15, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move this to top of the file with other contants?

@avimehta
Copy link
Copy Markdown
Contributor

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?

@cramforce
Copy link
Copy Markdown
Member

It'll be tough to ever get rid of the old way (without bumping the amp-analytics version, which we would not for such a small thing), but we can definitely warn and keep supporting both.

@rajkumarsrk
Copy link
Copy Markdown
Contributor Author

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

@rajkumarsrk
Copy link
Copy Markdown
Contributor Author

@avimehta & @cramforce Is it expected that the selector for visible event is always an Id ?

"triggers": {
  "defaultPageview": {
    "on": "visible",
    "request": "pageview",
    "visibilitySpec": {
      "selector": "#anim-id",
      "visiblePercentageMin": 20,
      "totalTimeMin": 500,
      "continuousTimeMin": 200
    }
  }
}

https://github.com/ampproject/amphtml/blob/master/extensions/amp-analytics/0.1/visibility-impl.js#L241

@rudygalfi
Copy link
Copy Markdown
Contributor

@rajkumarsrk I think it can also be a class name, e.g. .foo, but @avimehta should confirm.

@rajkumarsrk
Copy link
Copy Markdown
Contributor Author

Got the answer for this from console "Visibility spec requires an id selector"

@rajkumarsrk
Copy link
Copy Markdown
Contributor Author

@rudygalfi Thanks Rudy. Looks like we support only id only

@avimehta
Copy link
Copy Markdown
Contributor

@rajkumarsrk yes. We might want to add support for other elements formats at a later point but for now it is just ids.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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"
} 

@avimehta
Copy link
Copy Markdown
Contributor

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?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should extract the variables and merge them with vars here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For visibility.listenOnce we need both the visibilitySpec and the merged vars. Merging of vars is done on #L225

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@googlebot
Copy link
Copy Markdown

CLAs look good, thanks!

@rajkumarsrk
Copy link
Copy Markdown
Contributor Author

@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));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Object.assign is not available in all environments! This will fail!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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-(.+)/);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rajkumarsrk Can you address this comment?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup. Added opt_paramPattern and test case has been updated as well

@avimehta
Copy link
Copy Markdown
Contributor

avimehta commented Aug 5, 2016

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.
Copy link
Copy Markdown
Contributor

@avimehta avimehta Aug 8, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please change type to string= and add the parameter name after the type.

 @param {string=} opt_paramPattern Regex pattern to match data attributes.

@avimehta avimehta added LGTM and removed NEEDS REVIEW labels Aug 8, 2016
@avimehta
Copy link
Copy Markdown
Contributor

avimehta commented Aug 8, 2016

@jridgewell would you like to take another look?

@rajkumarsrk I just have one small comment.

@rajkumarsrk
Copy link
Copy Markdown
Contributor Author

@avimehta 👍 Updated JS doc

@avimehta
Copy link
Copy Markdown
Contributor

avimehta commented Aug 8, 2016

@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 avimehta merged commit 5e2eb4a into ampproject:master Aug 8, 2016
@rajkumarsrk
Copy link
Copy Markdown
Contributor Author

@avimehta Can you let me know when this feature will be available to consume

@rudygalfi
Copy link
Copy Markdown
Contributor

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

@rajkumarsrk
Copy link
Copy Markdown
Contributor Author

Thanks Rudy

ariangibson pushed a commit to Mixpo/amphtml that referenced this pull request Sep 7, 2016
…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
mityaha pushed a commit to brightcove-archive/ooyala_amphtml that referenced this pull request Nov 30, 2016
…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants