Add logging to local NTP doodle using gen_204.

Bug: 896461
Change-Id: Ie408a670722b91e10b740c36a810d9d6af1eb611
Reviewed-on: https://chromium-review.googlesource.com/c/1381639
Commit-Queue: David Lu <davidluphd@google.com>
Reviewed-by: Kyle Milka <kmilka@chromium.org>
Cr-Commit-Position: refs/heads/master@{#620520}
diff --git a/chrome/browser/resources/local_ntp/doodles.js b/chrome/browser/resources/local_ntp/doodles.js
index 870a6e8..98e6c2b 100644
--- a/chrome/browser/resources/local_ntp/doodles.js
+++ b/chrome/browser/resources/local_ntp/doodles.js
@@ -61,6 +61,20 @@
 
 
 /**
+ * Subset of gws.plugins.doodle.SharingLightbox.LogType in
+ * googledata/html/templates/gws/head/xjs/plugins/doodle/sharing_lightbox.js.
+ * @enum {number}
+ * @const
+ */
+doodles.SHARE_TYPE = {
+  FACEBOOK: 2,
+  TWITTER: 3,
+  EMAIL: 5,
+  LINK_COPY: 6,
+};
+
+
+/**
  * The ID of the doodle app for Facebook. Used to share doodles to Facebook.
  * @type {number}
  */
@@ -266,6 +280,29 @@
 
 
 /**
+ * TODO(896461): Add more click tracking parameters.
+ * Logs a doodle sharing event.
+ * Uses the ct param provided in metadata.onClickUrl to track the doodle.
+ *
+ * @param {string} platform Social media platform the doodle will be shared to.
+ */
+doodles.logDoodleShare = function(platform) {
+  if (doodles.targetDoodle.metadata.onClickUrl) {
+    const onClickUrl = new URL(doodles.targetDoodle.metadata.onClickUrl);
+    const ct = onClickUrl.searchParams.get('ct');
+    if (ct && ct != '') {
+      const url = new URL('/gen_204', configData.googleBaseUrl);
+      url.searchParams.append('atyp', 'i');
+      url.searchParams.append('ct', 'doodle');
+      url.searchParams.append('cad', 'sh,' + platform + ',ct:' + ct);
+      url.searchParams.append('ntp', 1);
+      navigator.sendBeacon(url.toString());
+    }
+  }
+};
+
+
+/**
  * Returns true if the target doodle is currently visible. If |image| is null,
  * returns true when the default logo is visible; if non-null, checks that it
  * matches the doodle that is currently visible. Here, "visible" means
@@ -619,6 +656,7 @@
         '&href=' + encodeURIComponent(shortLink) +
         '&hashtag=' + encodeURIComponent('#GoogleDoodle');
     window.open(url);
+    doodles.logDoodleShare(doodles.SHARE_TYPE.FACEBOOK);
   };
   facebookButton.title = configData.translatedStrings.shareFacebook;
 
@@ -626,6 +664,7 @@
     var url = 'https://twitter.com/intent/tweet' +
         '?text=' + encodeURIComponent(title + '\n' + shortLink);
     window.open(url);
+    doodles.logDoodleShare(doodles.SHARE_TYPE.TWITTER);
   };
   twitterButton.title = configData.translatedStrings.shareTwitter;
 
@@ -633,6 +672,7 @@
     var url = 'mailto:?subject=' + encodeURIComponent(title) +
         '&body=' + encodeURIComponent(shortLink);
     document.location.href = url;
+    doodles.logDoodleShare(doodles.SHARE_TYPE.EMAIL);
   };
   mailButton.title = configData.translatedStrings.shareMail;
 
@@ -645,6 +685,7 @@
   copyButton.onclick = function() {
     linkText.select();
     document.execCommand('copy');
+    doodles.logDoodleShare(doodles.SHARE_TYPE.LINK_COPY);
   };
   copyButton.title = configData.translatedStrings.copyLink;
 };