{"action":"created","comment":{"url":"https://api.github.com/repos/ampproject/amphtml/pulls/comments/71796877","id":71796877,"diff_hunk":"@@ -15,43 +15,125 @@\n  */\n \n import {isExperimentOn} from '../../../src/experiments';\n-import {getService} from '../../../src/service';\n+import {xhrFor} from '../../../src/xhr';\n+import {viewerFor} from '../../../src/viewer';\n import {dev} from '../../../src/log';\n \n /** @private @const {string} */\n const TAG = 'amp-share-tracking';\n \n-class AmpShareTracking extends AMP.BaseElement {\n+export class AmpShareTracking extends AMP.BaseElement {\n+  /**\n+    * @return {boolean}\n+    * @private\n+    */\n+  isExperimentOn_() {\n+    return isExperimentOn(this.win, TAG);\n+  }\n+\n+  /** @override */\n+  isLayoutSupported(unusedLayout) {\n+    return true;\n+  }\n \n   /** @override */\n   buildCallback() {\n-    this.isExperimentOn_ = isExperimentOn(this.win, TAG);\n-    if (!this.isExperimentOn_) {\n+    if (!this.isExperimentOn_()) {\n       dev.warn(TAG, `TAG ${TAG} disabled`);\n       return;\n     }\n+\n+    /** @private @const {!Window} */\n+    this.win_ = this.win;\n+\n+    this.vendorHref = this.element.getAttribute('data-href');\n+    dev.fine(TAG, 'vendorHref: ', this.vendorHref);\n+\n+    this.shareTrackingFragments = Promise.all([\n+      this.getIncomingFragment(),\n+      this.getOutgoingFragment(this.vendorHref)]).then(results => {\n+        const fragments = {};\n+        fragments.incomingFragment = results[0];\n+        fragments.outgoingFragment = results[1];\n+        return fragments;\n+      });\n+\n   }\n-}\n \n-/**\n- * ShareTrackingService processes the incoming and outcoming url fragment\n- */\n-export class ShareTrackingService {\n-  constructor(window) {\n-    this.win = window;\n+  /**\n+   * Get the incoming share-tracking fragment from the viewer\n+   * @return {!Promise<string>}\n+   * @private\n+   */\n+  getIncomingFragment() {\n+    return viewerFor(this.win_).getFragment().then(hash => {\n+      if (!hash) {\n+        return Promise.resolve();\n+      }\n+\n+      if (hash.indexOf('.') == 0) {\n+        const endIndex = hash.indexOf('&');\n+        if (endIndex != -1) {\n+          return Promise.resolve(hash.substr(1, (endIndex - 1)));\n+        } else {\n+          return Promise.resolve(hash.substr(1));\n+        }\n+      }\n+      return Promise.resolve();\n+    });\n   }\n-}\n \n-/**\n- * @param {!Window} window\n- * @return {!ShareTrackingService}\n- */\n-export function installShareTrackingService(window) {\n-  return getService(window, 'shareTrackingService', () => {\n-    return new ShareTrackingService(window);\n-  });\n-}\n+  /**\n+   * Get an outgoing share-tracking fragment\n+   * @param {string=} vendorUrl\n+   * @return {!Promise<string>}\n+   * @private\n+   */\n+  getOutgoingFragment(vendorUrl) {\n+    if (vendorUrl) {\n+      return this.getOutgoingFragmentFromVendor(vendorUrl).then(\n+        response => {\n+          if (!response.outgoingFragment) {\n+            dev.warn(TAG, 'The response from [' + vendorUrl +\n+                '] does not have an outgoingFragment value. ' +\n+                'Generating outgoing fragment using random generator.');\n+            return this.getOutgoingFragmentRandomly();\n+          } else {\n+            return response.outgoingFragment;\n+          }\n+        }\n+      );\n+    } else {\n+      return this.getOutgoingFragmentRandomly();\n+    }\n+  }\n+\n+  /**\n+   * Get an outgoing share-tracking fragment from vendor\n+   * by issueing a post request to the url the vendor provided\n+   * @return {!Promise<!{","path":"extensions/amp-share-tracking/0.1/amp-share-tracking.js","position":121,"original_position":121,"commit_id":"274f3810d1bcf4db786e182cfae057a4bd117640","original_commit_id":"274f3810d1bcf4db786e182cfae057a4bd117640","user":{"login":"lannka","id":8496897,"avatar_url":"https://avatars.githubusercontent.com/u/8496897?v=3","gravatar_id":"","url":"https://api.github.com/users/lannka","html_url":"https://github.com/lannka","followers_url":"https://api.github.com/users/lannka/followers","following_url":"https://api.github.com/users/lannka/following{/other_user}","gists_url":"https://api.github.com/users/lannka/gists{/gist_id}","starred_url":"https://api.github.com/users/lannka/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lannka/subscriptions","organizations_url":"https://api.github.com/users/lannka/orgs","repos_url":"https://api.github.com/users/lannka/repos","events_url":"https://api.github.com/users/lannka/events{/privacy}","received_events_url":"https://api.github.com/users/lannka/received_events","type":"User","site_admin":false},"body":"The return type is not intuitive. How about extracting out the outgoingFragment in this function so we can just return a Promise<string>","created_at":"2016-07-21T22:10:04Z","updated_at":"2016-07-21T22:10:04Z","html_url":"https://github.com/ampproject/amphtml/pull/4019#discussion_r71796877","pull_request_url":"https://api.github.com/repos/ampproject/amphtml/pulls/4019","_links":{"self":{"href":"https://api.github.com/repos/ampproject/amphtml/pulls/comments/71796877"},"html":{"href":"https://github.com/ampproject/amphtml/pull/4019#discussion_r71796877"},"pull_request":{"href":"https://api.github.com/repos/ampproject/amphtml/pulls/4019"}}},"pull_request":{"url":"https://api.github.com/repos/ampproject/amphtml/pulls/4019","id":77034983,"html_url":"https://github.com/ampproject/amphtml/pull/4019","diff_url":"https://github.com/ampproject/amphtml/pull/4019.diff","patch_url":"https://github.com/ampproject/amphtml/pull/4019.patch","issue_url":"https://api.github.com/repos/ampproject/amphtml/issues/4019","number":4019,"state":"open","locked":false,"title":"Make share-tracking extension to get incoming/outcoming fragments","user":{"login":"muxin","id":3897214,"avatar_url":"https://avatars.githubusercontent.com/u/3897214?v=3","gravatar_id":"","url":"https://api.github.com/users/muxin","html_url":"https://github.com/muxin","followers_url":"https://api.github.com/users/muxin/followers","following_url":"https://api.github.com/users/muxin/following{/other_user}","gists_url":"https://api.github.com/users/muxin/gists{/gist_id}","starred_url":"https://api.github.com/users/muxin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/muxin/subscriptions","organizations_url":"https://api.github.com/users/muxin/orgs","repos_url":"https://api.github.com/users/muxin/repos","events_url":"https://api.github.com/users/muxin/events{/privacy}","received_events_url":"https://api.github.com/users/muxin/received_events","type":"User","site_admin":false},"body":"#3135 \r\n* `AmpShareTracking` will get incoming and outgoing share tracking identifier\r\n* Get incoming fragment: from url hash directly or ask viewer to get it\r\n* Get outgoing fragment: from url endpoint provided by vendor or generate randomly","created_at":"2016-07-11T22:38:00Z","updated_at":"2016-07-21T22:10:05Z","closed_at":null,"merged_at":null,"merge_commit_sha":"a388ddc5e38f1a8784f06c42d7f57db3ee09acef","assignee":{"login":"cramforce","id":89679,"avatar_url":"https://avatars.githubusercontent.com/u/89679?v=3","gravatar_id":"","url":"https://api.github.com/users/cramforce","html_url":"https://github.com/cramforce","followers_url":"https://api.github.com/users/cramforce/followers","following_url":"https://api.github.com/users/cramforce/following{/other_user}","gists_url":"https://api.github.com/users/cramforce/gists{/gist_id}","starred_url":"https://api.github.com/users/cramforce/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cramforce/subscriptions","organizations_url":"https://api.github.com/users/cramforce/orgs","repos_url":"https://api.github.com/users/cramforce/repos","events_url":"https://api.github.com/users/cramforce/events{/privacy}","received_events_url":"https://api.github.com/users/cramforce/received_events","type":"User","site_admin":false},"assignees":[{"login":"cramforce","id":89679,"avatar_url":"https://avatars.githubusercontent.com/u/89679?v=3","gravatar_id":"","url":"https://api.github.com/users/cramforce","html_url":"https://github.com/cramforce","followers_url":"https://api.github.com/users/cramforce/followers","following_url":"https://api.github.com/users/cramforce/following{/other_user}","gists_url":"https://api.github.com/users/cramforce/gists{/gist_id}","starred_url":"https://api.github.com/users/cramforce/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cramforce/subscriptions","organizations_url":"https://api.github.com/users/cramforce/orgs","repos_url":"https://api.github.com/users/cramforce/repos","events_url":"https://api.github.com/users/cramforce/events{/privacy}","received_events_url":"https://api.github.com/users/cramforce/received_events","type":"User","site_admin":false},{"login":"lannka","id":8496897,"avatar_url":"https://avatars.githubusercontent.com/u/8496897?v=3","gravatar_id":"","url":"https://api.github.com/users/lannka","html_url":"https://github.com/lannka","followers_url":"https://api.github.com/users/lannka/followers","following_url":"https://api.github.com/users/lannka/following{/other_user}","gists_url":"https://api.github.com/users/lannka/gists{/gist_id}","starred_url":"https://api.github.com/users/lannka/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lannka/subscriptions","organizations_url":"https://api.github.com/users/lannka/orgs","repos_url":"https://api.github.com/users/lannka/repos","events_url":"https://api.github.com/users/lannka/events{/privacy}","received_events_url":"https://api.github.com/users/lannka/received_events","type":"User","site_admin":false}],"milestone":null,"commits_url":"https://api.github.com/repos/ampproject/amphtml/pulls/4019/commits","review_comments_url":"https://api.github.com/repos/ampproject/amphtml/pulls/4019/comments","review_comment_url":"https://api.github.com/repos/ampproject/amphtml/pulls/comments{/number}","comments_url":"https://api.github.com/repos/ampproject/amphtml/issues/4019/comments","statuses_url":"https://api.github.com/repos/ampproject/amphtml/statuses/274f3810d1bcf4db786e182cfae057a4bd117640","head":{"label":"muxin:share-tracking-service","ref":"share-tracking-service","sha":"274f3810d1bcf4db786e182cfae057a4bd117640","user":{"login":"muxin","id":3897214,"avatar_url":"https://avatars.githubusercontent.com/u/3897214?v=3","gravatar_id":"","url":"https://api.github.com/users/muxin","html_url":"https://github.com/muxin","followers_url":"https://api.github.com/users/muxin/followers","following_url":"https://api.github.com/users/muxin/following{/other_user}","gists_url":"https://api.github.com/users/muxin/gists{/gist_id}","starred_url":"https://api.github.com/users/muxin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/muxin/subscriptions","organizations_url":"https://api.github.com/users/muxin/orgs","repos_url":"https://api.github.com/users/muxin/repos","events_url":"https://api.github.com/users/muxin/events{/privacy}","received_events_url":"https://api.github.com/users/muxin/received_events","type":"User","site_admin":false},"repo":{"id":58148082,"name":"amphtml","full_name":"muxin/amphtml","owner":{"login":"muxin","id":3897214,"avatar_url":"https://avatars.githubusercontent.com/u/3897214?v=3","gravatar_id":"","url":"https://api.github.com/users/muxin","html_url":"https://github.com/muxin","followers_url":"https://api.github.com/users/muxin/followers","following_url":"https://api.github.com/users/muxin/following{/other_user}","gists_url":"https://api.github.com/users/muxin/gists{/gist_id}","starred_url":"https://api.github.com/users/muxin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/muxin/subscriptions","organizations_url":"https://api.github.com/users/muxin/orgs","repos_url":"https://api.github.com/users/muxin/repos","events_url":"https://api.github.com/users/muxin/events{/privacy}","received_events_url":"https://api.github.com/users/muxin/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/muxin/amphtml","description":"AMP HTML source code, samples, and documentation.  See below for more info.","fork":true,"url":"https://api.github.com/repos/muxin/amphtml","forks_url":"https://api.github.com/repos/muxin/amphtml/forks","keys_url":"https://api.github.com/repos/muxin/amphtml/keys{/key_id}","collaborators_url":"https://api.github.com/repos/muxin/amphtml/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/muxin/amphtml/teams","hooks_url":"https://api.github.com/repos/muxin/amphtml/hooks","issue_events_url":"https://api.github.com/repos/muxin/amphtml/issues/events{/number}","events_url":"https://api.github.com/repos/muxin/amphtml/events","assignees_url":"https://api.github.com/repos/muxin/amphtml/assignees{/user}","branches_url":"https://api.github.com/repos/muxin/amphtml/branches{/branch}","tags_url":"https://api.github.com/repos/muxin/amphtml/tags","blobs_url":"https://api.github.com/repos/muxin/amphtml/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/muxin/amphtml/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/muxin/amphtml/git/refs{/sha}","trees_url":"https://api.github.com/repos/muxin/amphtml/git/trees{/sha}","statuses_url":"https://api.github.com/repos/muxin/amphtml/statuses/{sha}","languages_url":"https://api.github.com/repos/muxin/amphtml/languages","stargazers_url":"https://api.github.com/repos/muxin/amphtml/stargazers","contributors_url":"https://api.github.com/repos/muxin/amphtml/contributors","subscribers_url":"https://api.github.com/repos/muxin/amphtml/subscribers","subscription_url":"https://api.github.com/repos/muxin/amphtml/subscription","commits_url":"https://api.github.com/repos/muxin/amphtml/commits{/sha}","git_commits_url":"https://api.github.com/repos/muxin/amphtml/git/commits{/sha}","comments_url":"https://api.github.com/repos/muxin/amphtml/comments{/number}","issue_comment_url":"https://api.github.com/repos/muxin/amphtml/issues/comments{/number}","contents_url":"https://api.github.com/repos/muxin/amphtml/contents/{+path}","compare_url":"https://api.github.com/repos/muxin/amphtml/compare/{base}...{head}","merges_url":"https://api.github.com/repos/muxin/amphtml/merges","archive_url":"https://api.github.com/repos/muxin/amphtml/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/muxin/amphtml/downloads","issues_url":"https://api.github.com/repos/muxin/amphtml/issues{/number}","pulls_url":"https://api.github.com/repos/muxin/amphtml/pulls{/number}","milestones_url":"https://api.github.com/repos/muxin/amphtml/milestones{/number}","notifications_url":"https://api.github.com/repos/muxin/amphtml/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/muxin/amphtml/labels{/name}","releases_url":"https://api.github.com/repos/muxin/amphtml/releases{/id}","deployments_url":"https://api.github.com/repos/muxin/amphtml/deployments","created_at":"2016-05-05T17:18:38Z","updated_at":"2016-05-05T17:18:42Z","pushed_at":"2016-07-21T20:46:38Z","git_url":"git://github.com/muxin/amphtml.git","ssh_url":"git@github.com:muxin/amphtml.git","clone_url":"https://github.com/muxin/amphtml.git","svn_url":"https://github.com/muxin/amphtml","homepage":"https://ampproject.org","size":127329,"stargazers_count":0,"watchers_count":0,"language":"JavaScript","has_issues":false,"has_downloads":false,"has_wiki":false,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"ampproject:master","ref":"master","sha":"337ca8ba43e9e3bcb45d739a8335449a03fbfc40","user":{"login":"ampproject","id":14114390,"avatar_url":"https://avatars.githubusercontent.com/u/14114390?v=3","gravatar_id":"","url":"https://api.github.com/users/ampproject","html_url":"https://github.com/ampproject","followers_url":"https://api.github.com/users/ampproject/followers","following_url":"https://api.github.com/users/ampproject/following{/other_user}","gists_url":"https://api.github.com/users/ampproject/gists{/gist_id}","starred_url":"https://api.github.com/users/ampproject/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ampproject/subscriptions","organizations_url":"https://api.github.com/users/ampproject/orgs","repos_url":"https://api.github.com/users/ampproject/repos","events_url":"https://api.github.com/users/ampproject/events{/privacy}","received_events_url":"https://api.github.com/users/ampproject/received_events","type":"Organization","site_admin":false},"repo":{"id":41766002,"name":"amphtml","full_name":"ampproject/amphtml","owner":{"login":"ampproject","id":14114390,"avatar_url":"https://avatars.githubusercontent.com/u/14114390?v=3","gravatar_id":"","url":"https://api.github.com/users/ampproject","html_url":"https://github.com/ampproject","followers_url":"https://api.github.com/users/ampproject/followers","following_url":"https://api.github.com/users/ampproject/following{/other_user}","gists_url":"https://api.github.com/users/ampproject/gists{/gist_id}","starred_url":"https://api.github.com/users/ampproject/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ampproject/subscriptions","organizations_url":"https://api.github.com/users/ampproject/orgs","repos_url":"https://api.github.com/users/ampproject/repos","events_url":"https://api.github.com/users/ampproject/events{/privacy}","received_events_url":"https://api.github.com/users/ampproject/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/ampproject/amphtml","description":"AMP HTML source code, samples, and documentation.  See below for more info.","fork":false,"url":"https://api.github.com/repos/ampproject/amphtml","forks_url":"https://api.github.com/repos/ampproject/amphtml/forks","keys_url":"https://api.github.com/repos/ampproject/amphtml/keys{/key_id}","collaborators_url":"https://api.github.com/repos/ampproject/amphtml/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/ampproject/amphtml/teams","hooks_url":"https://api.github.com/repos/ampproject/amphtml/hooks","issue_events_url":"https://api.github.com/repos/ampproject/amphtml/issues/events{/number}","events_url":"https://api.github.com/repos/ampproject/amphtml/events","assignees_url":"https://api.github.com/repos/ampproject/amphtml/assignees{/user}","branches_url":"https://api.github.com/repos/ampproject/amphtml/branches{/branch}","tags_url":"https://api.github.com/repos/ampproject/amphtml/tags","blobs_url":"https://api.github.com/repos/ampproject/amphtml/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/ampproject/amphtml/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/ampproject/amphtml/git/refs{/sha}","trees_url":"https://api.github.com/repos/ampproject/amphtml/git/trees{/sha}","statuses_url":"https://api.github.com/repos/ampproject/amphtml/statuses/{sha}","languages_url":"https://api.github.com/repos/ampproject/amphtml/languages","stargazers_url":"https://api.github.com/repos/ampproject/amphtml/stargazers","contributors_url":"https://api.github.com/repos/ampproject/amphtml/contributors","subscribers_url":"https://api.github.com/repos/ampproject/amphtml/subscribers","subscription_url":"https://api.github.com/repos/ampproject/amphtml/subscription","commits_url":"https://api.github.com/repos/ampproject/amphtml/commits{/sha}","git_commits_url":"https://api.github.com/repos/ampproject/amphtml/git/commits{/sha}","comments_url":"https://api.github.com/repos/ampproject/amphtml/comments{/number}","issue_comment_url":"https://api.github.com/repos/ampproject/amphtml/issues/comments{/number}","contents_url":"https://api.github.com/repos/ampproject/amphtml/contents/{+path}","compare_url":"https://api.github.com/repos/ampproject/amphtml/compare/{base}...{head}","merges_url":"https://api.github.com/repos/ampproject/amphtml/merges","archive_url":"https://api.github.com/repos/ampproject/amphtml/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/ampproject/amphtml/downloads","issues_url":"https://api.github.com/repos/ampproject/amphtml/issues{/number}","pulls_url":"https://api.github.com/repos/ampproject/amphtml/pulls{/number}","milestones_url":"https://api.github.com/repos/ampproject/amphtml/milestones{/number}","notifications_url":"https://api.github.com/repos/ampproject/amphtml/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/ampproject/amphtml/labels{/name}","releases_url":"https://api.github.com/repos/ampproject/amphtml/releases{/id}","deployments_url":"https://api.github.com/repos/ampproject/amphtml/deployments","created_at":"2015-09-01T22:10:53Z","updated_at":"2016-07-21T16:32:12Z","pushed_at":"2016-07-21T22:07:56Z","git_url":"git://github.com/ampproject/amphtml.git","ssh_url":"git@github.com:ampproject/amphtml.git","clone_url":"https://github.com/ampproject/amphtml.git","svn_url":"https://github.com/ampproject/amphtml","homepage":"https://ampproject.org","size":137593,"stargazers_count":7943,"watchers_count":7943,"language":"JavaScript","has_issues":true,"has_downloads":false,"has_wiki":false,"has_pages":false,"forks_count":1129,"mirror_url":null,"open_issues_count":613,"forks":1129,"open_issues":613,"watchers":7943,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/ampproject/amphtml/pulls/4019"},"html":{"href":"https://github.com/ampproject/amphtml/pull/4019"},"issue":{"href":"https://api.github.com/repos/ampproject/amphtml/issues/4019"},"comments":{"href":"https://api.github.com/repos/ampproject/amphtml/issues/4019/comments"},"review_comments":{"href":"https://api.github.com/repos/ampproject/amphtml/pulls/4019/comments"},"review_comment":{"href":"https://api.github.com/repos/ampproject/amphtml/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/ampproject/amphtml/pulls/4019/commits"},"statuses":{"href":"https://api.github.com/repos/ampproject/amphtml/statuses/274f3810d1bcf4db786e182cfae057a4bd117640"}}}}
{"actor":{"display_login":"lannka"}}