Skip to content

Dynamically insert content into anchor href on click#9086

Merged
jridgewell merged 6 commits intoampproject:masterfrom
rajkumarsrk:addclickparams
May 10, 2017
Merged

Dynamically insert content into anchor href on click#9086
jridgewell merged 6 commits intoampproject:masterfrom
rajkumarsrk:addclickparams

Conversation

@rajkumarsrk
Copy link
Copy Markdown
Contributor

@rajkumarsrk rajkumarsrk commented May 2, 2017

Add support to dynamically add URL parameters to the link URL

  1. Follows the same guidance of how the data-amp-replace works where the values are & separated
  2. The dynamic URL parameters will be added only for URL's from
  • Same as that of origin URL
  • https
  • Whitelisted URL
    data-amp-addparams has the same set of rules as that of data-amp-replace
  1. Any value that is part of data-amp-addparams and that need to be replaced should have entry on data-amp-replace

If this changes looks good I will update the Readme.md files as well

Fixes #8594

* @param {!Element} element An anchor element.
* @return {string} url
* @private
*/
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.

We can avoid this entirely by passing in a map as opt_bindings in the #expandSync call above.

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.

As discussed with you on slack opt_bindings can be used for the replace use case only.

* @return {string} url
* @private
*/
extendURLParams_(url, element) {
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.

We already have data-vars-* for <amp-analytics>, maybe use the same?

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.

  • I can make it as data-vars-urlparam from data-amp-addparam but it wont align with naming of data-amp-replace. Is that fine ?
  • Also data-amp-addparam can have token values like c=CLIENT_ID(abc) which should be replaced by entries provided on data-amp-replace like
a.setAttribute('data-amp-replace', 'QUERY_PARAM CLIENT_ID');
a.setAttribute('data-amp-addparams', 'guid=123 c=CLIENT_ID(abc)');

In this if we have a different data attribute name like data-vars-* I guess it might create confusion . Please let me know your suggestion on this

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.

Let's leave as is then.

* @return {string} url
* @private
*/
extendURLParams_(url, element) {
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.

Let's leave as is then.

const additionalURLParamaters = element.getAttribute('data-amp-addparams');
if (!additionalURLParamaters) {
return url;
}
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.

We'll need this to use addParamsToUrl, but unfortunately we'll need to do some parsing.

// This should probably live in src/url.js
function spaceDelimitedParamsToMap(params) {
  return parseQueryString(params.replace(/\s+/, '&'));
}

// Here, in this code
return addParamsToUrl(url, spaceDelimitedParamsToMap(additionalURLParamaters));

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.

Added spaceDelimitedParamsToMap to url.js and used addParamsToUrl. Now because of the url.js usage it got simplified

Copy link
Copy Markdown
Contributor

@jridgewell jridgewell left a comment

Choose a reason for hiding this comment

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

Code wise looks fine (minus the types), @rudygalfi would you like to review?

src/url.js Outdated
/**
* Covert space delimited parameters to Map
* @param {string} url
* @return {string}
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.

This return is incorrect. {!Object<string>}

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.

My bad. Missed the JS doc. Checked in these 2 fixes

src/url.js Outdated

/**
* Covert space delimited parameters to Map
* @param {string} url
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.

This should params, not url

@rajkumarsrk
Copy link
Copy Markdown
Contributor Author

@jridgewell
Copy link
Copy Markdown
Contributor

@rajkumarsrk
Copy link
Copy Markdown
Contributor Author

@jridgewell
Copy link
Copy Markdown
Contributor

These questions are better directed at @bpaduch. 😄

<a href="https://example.com/step2.html?ref_id=CLIENT_ID(uid)" data-amp-replace="CLIENT_ID">
```

**Alternative solution for passing Client ID to the outgoing links:** Define the new query parameter `ref_id` as part of the data attribute `data-amp-addparams` and for queries that needs parameter substitution provide those details as part of `data-amp-replace`. With this approach the URL would look clean and the parameters specified on `data-amp-addparams` will be dynamically added
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

@rudygalfi - As you own this doc and are more familiar with the content, can you review that this is accurate and the correct location for it?

@ghost
Copy link
Copy Markdown

ghost commented May 5, 2017

@rajkumarsrk

@ghost ghost requested a review from rudygalfi May 5, 2017 13:13
@rajkumarsrk
Copy link
Copy Markdown
Contributor Author

I thought amp-var-substitutions.md was dedicated to substitution logic which has been shared with the analytics, anchour, form elements and the current change that we have in the PR is related to anchour and its on variable addition. If there is no concerns from your side I can go ahead and update it on the existing document itself (amp-var-substitutions.md)

@rajkumarsrk
Copy link
Copy Markdown
Contributor Author

Updated the amp-var-substitutions.md as well

@jridgewell
Copy link
Copy Markdown
Contributor

Looks like you have to rebase, then we can merge.

<a href="https://example.com?client_id=CLIENT_ID(bar)&abc=QUERY_PARAM(abc)" data-amp-replace="CLIENT_ID,QUERY_PARAM">Go to my site</a>
```

Variable substitution works well with dynamic parameters added to link href through `data-amp-addparams`. Parameters provided as part of `data-amp-addparams` that requires variable substitution has to be specified on `data-amp-replace` like
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I'd like to suggest you:

  1. Add a level 4 subheading ( #### Appending parameters to the href).
  2. Rewrite the information as follows (of course, assuming I'm technically correct ;) ):

If you need to append dynamic parameters to the href, specify the parameters by using the data-amp-addparams attribute. Any substitution parameters that you specify in data-amp-addparams must also be specified in data-amp-replace, as in the following example:

...

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.

Updated

@rajkumarsrk
Copy link
Copy Markdown
Contributor Author

rajkumarsrk commented May 8, 2017

@jridgewell The data-amp-replace is comma separated and currently I have it as data-amp-addparams space separated. And I am thinking of changing it to comma separating to make it consistent with data-amp-replace. Let me know your thoughts

@jridgewell
Copy link
Copy Markdown
Contributor

Github says it still needs a rebase off current master.

And I am thinking of changing it to comma separating to make it consistent with data-amp-replace

Commas made sense for data-amp-replace because it's supposed to be a list. addparams is really a query string. If anything, I'd like to see us use &.

@jridgewell
Copy link
Copy Markdown
Contributor

Ugh, unrelated docs failures. @erwinmombay, mind merging?

Copy link
Copy Markdown

@ghost ghost left a comment

Choose a reason for hiding this comment

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

Doc change looks good.

@rajkumarsrk
Copy link
Copy Markdown
Contributor Author

After rebasing to the latest changes PR failure got fixed

@jridgewell jridgewell merged commit 21e3eb5 into ampproject:master May 10, 2017
@jridgewell
Copy link
Copy Markdown
Contributor

Thanks for the help @rajkumarsrk!

@rajkumarsrk
Copy link
Copy Markdown
Contributor Author

Thanks @jridgewell.

Can you let me know when we can expect this change on production ?

@jridgewell
Copy link
Copy Markdown
Contributor

It'll be 2 weeks, though you'll be able to test using canary next week.

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.

4 participants