Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[amp-analytics] Added ability to use variables in selectors. #3281

Merged
merged 1 commit into from Jun 28, 2016

Conversation

avimehta
Copy link
Contributor

@avimehta avimehta commented May 20, 2016

This allows remote trigger config to specify a config like this:

Remote config that could be provided by a vendor (like Google Tag Manager or something else). This allows GTM to provide a config that can work across various publishers.

{
  on: 'click',
  selector: '${likeButtonSelector}',
  request: 'foo',
}

In-page config provided by a publisher like nytimes.com. This way, they can use the remote config from GTM and give any id/class to their like button.

{
  vars: { likeButtonSelector: '#myLikeButton' }
}

Overall, this will be used like this:

<amp-analytics config="https://gtm.com/someconfig.json">
<script type='application/json'>
{
  'vars': { likeButtonSelector: '#myLikeButton' }
}
</script>
</amp-analytics>

@dvoytenko
Copy link
Contributor

@avimehta Could you please explain why this is needed?

@avimehta
Copy link
Contributor Author

Updated #3281 (comment) with a usecase.

// Expand the selector using variable expansion.
trigger['selector'] = this.expandTemplate_(trigger['selector'],
trigger);
return urlReplacementsFor(this.getWin()).expand(trigger['selector'])
Copy link
Contributor

Choose a reason for hiding this comment

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

One concern is that since uppercase CSS are allowed and the var names are pretty logical - we have a meaningful possibility of an accidental collision. WDYT? Is url replacements part necessary here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For the usecases I am aware of and the ones described in this issue, I think going with this.expandTemplate_ is enough. In that case, only the vars that publishers specify will get expanded and not the ones that platform specifies.

The reason to add urlReplacementsFor is of course: uniformity across all expansions.

One solution could be to remove urlReplacementsFor for now and add it back if someone wants it.

Copy link
Contributor

Choose a reason for hiding this comment

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

One think it's a good first step. We'll just have to make a note in docs that only author-based vars are allowed here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done. Can you take a look again? The test failure seems to be unrelated.

@avimehta
Copy link
Contributor Author

@dvoytenko ptal? The test failure is not related to this PR.

@avimehta
Copy link
Contributor Author

@cramforce Can you take a look? I think @dvoytenko won't be able to get to this till late next week.

Thanks

@cramforce
Copy link
Member

Should this be documented anywhere?

@avimehta
Copy link
Contributor Author

I was hoping to get this in prod without documentation and used by a select few before full documentation. Would that work?

Opened #3809

@cramforce
Copy link
Member

LGTM

This allows remote trigger config to specify a config like this:

Remote config:
```
{
  on: 'click',
  selector: '${likeButtonSelector}',
  request: 'foo',
}
```

In page config:
```
{
  vars: { likeButtonSelector: '#myLikeButton' }
}
```
@avimehta avimehta merged commit 7c6b1a1 into ampproject:master Jun 28, 2016
@Gueorgi
Copy link

Gueorgi commented Jul 6, 2016

It looks all good but for one small issue: it URI encodes the values and this is the wrong encoding for a CSS selectors.

For example a valid selector like this: "img, div, button" (selecting either an image a div or a button) if used as a value of an AMP Var, becomes after expansion into "img%2Cdiv%2Cbutton" if the AMP var is used in the selector. Probably there are issues with other valid CSS selector characters as well.

@rudygalfi
Copy link
Contributor

@Gueorgi Can you open a separate issue for what you mentioned?

@avimehta
Copy link
Contributor Author

opened #4055

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants