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

⛓Linker PT 2 #17616

Merged
merged 27 commits into from Aug 23, 2018
Merged

⛓Linker PT 2 #17616

merged 27 commits into from Aug 23, 2018

Conversation

calebcordry
Copy link
Member

Introduces the Linker logic into amp-analytics.

* before they are ready.
* @private
*/
initLinker_() {
Copy link
Contributor

Choose a reason for hiding this comment

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

let's separate this into a separate file linker-manager.js

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

const vendorConfig = this.config_['linkers'][name];

if (vendorConfig['enabled'] !== true) {
user().warn(TAG, `linker config for ${name} is not enabled and` +
Copy link
Contributor

Choose a reason for hiding this comment

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

this should be a debugging message

Copy link
Member Author

Choose a reason for hiding this comment

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

done.

}

const ids = vendorConfig['ids'];
dev().assert(ids,
Copy link
Contributor

Choose a reason for hiding this comment

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

user()

Copy link
Member Author

Choose a reason for hiding this comment

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

done.

* @param {./amp-analytics.AmpAnalytics} analytics
* @param {JsonObject} config
*/
constructor(analytics, config) {
Copy link
Contributor

Choose a reason for hiding this comment

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

let's avoid the reverse dependency on analytics

Copy link
Member Author

Choose a reason for hiding this comment

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

done.

* @param {!Element} element
* @private
*/
linkerCallback_(element) {
Copy link
Contributor

Choose a reason for hiding this comment

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

handleAnchorMutation_

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

*/
init() {
if (!this.config_['linkers']) {
return;
Copy link
Contributor

Choose a reason for hiding this comment

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

can we have the optin check here to return early?
might be worth to just make a copy of enabled linkers in the config.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

}

// See if any domains match.
for (let i = 0; i < domains.length; i++) {
Copy link
Contributor

Choose a reason for hiding this comment

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

domains.indexOf(hostname) >= 0

Copy link
Member Author

Choose a reason for hiding this comment

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

how about domains.includes()

@@ -42,6 +42,11 @@ const EVENT_TYPE_CONTEXT_MENU = 'contextmenu';
/** @private @const {string} */
const ORIG_HREF_ATTRIBUTE = 'data-a4a-orig-href';

/** @enum {number} */
export const Priority = {
LINKER: 2,
Copy link
Contributor

Choose a reason for hiding this comment

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

ANALYTICS_LINKER

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

import {Priority} from '../../../../src/service/navigation';
import {Services} from '../../../../src/services';

describe('Linkers', () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

LinkerManager

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

manager.init();

return Promise.all(manager.allLinkerPromises_).then(() => {
const res1 = (manager.resolvedLinkers_['testLinker1']).split('~');
Copy link
Contributor

Choose a reason for hiding this comment

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

avoid exposing resolvedLinkers_ to test, we can test the strings in a.href.

* Called on click on any anchor element. Adds linker param if a match for
* given linker configuration.
* @param {!Element} element
* @private
Copy link
Contributor

Choose a reason for hiding this comment

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

visibleForTesting

Copy link
Member Author

Choose a reason for hiding this comment

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

I tried this, but if you add @visibleForTesting and it is used in the class it throws an error

Copy link
Contributor

Choose a reason for hiding this comment

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

I think you will need to remove @private and also the ending _ in method name.

Copy link
Member Author

Choose a reason for hiding this comment

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

Got it. Done.

const linkerNames = Object.keys(this.config_['linkers']);

if (!linkerNames.length) {
return user().error(TAG,
Copy link
Contributor

Choose a reason for hiding this comment

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

It might worth to support syntax below to completely disable linkers:

{
  linkers: false
}

So, we might not want to print this error.

Copy link
Member Author

Choose a reason for hiding this comment

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

Should we check both? linkers: false is good config, but linkers: {} is bad?

Copy link
Member Author

Choose a reason for hiding this comment

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

Actually I think this is covered by line 65. if (!this.config_['linkers']) should cover the linkers: false case and exit before this code

Copy link
Contributor

Choose a reason for hiding this comment

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

honestly i don't see strong reason to now allow linkers: {}, especially with increasing binary size.

Copy link
Member Author

Choose a reason for hiding this comment

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

ok removed.

});
});

if (this.allLinkerPromises_.length) {
Copy link
Contributor

Choose a reason for hiding this comment

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

nice!

@calebcordry calebcordry merged commit 0beef60 into ampproject:master Aug 23, 2018
Enriqe pushed a commit to Enriqe/amphtml that referenced this pull request Nov 28, 2018
* add priority

* analytics changes

* tests

* example

* fix import

* types

* move files

* move files

* lint

* add enabled flag

* add enabled to example

* fix types

* lint

* comma

* whitelist dep

* add legacy opt-in

* remove reverse dep

* better warnings

* lint

* comments

* includes

* comments

* make sure array

* fix domains check

* remove check

* visible for test

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

Successfully merging this pull request may close these issues.

None yet

3 participants