Skip to content

✨[amp-analytics] Add Multiple Video Selector Functionality for amp-video#34841

Merged
micajuine-ho merged 30 commits intoampproject:mainfrom
kalemuw:multi-vid-selector
Jun 28, 2021
Merged

✨[amp-analytics] Add Multiple Video Selector Functionality for amp-video#34841
micajuine-ho merged 30 commits intoampproject:mainfrom
kalemuw:multi-vid-selector

Conversation

@kalemuw
Copy link
Copy Markdown
Contributor

@kalemuw kalemuw commented Jun 11, 2021

This PR allows the ability to track multiple videos in one configuration. Added in response to #30860.
Previously, to apply the same video trigger configuration to multiple video elements, the same trigger would be duplicated for each video. However, with the edits in this PR, an array of selectors can be specified in the selector argument to apply and track the video trigger on said videos.
Thus, both selectors in form of an array of query selectors and a single query selector string are supported in the configuration and are tracked.

Edits

  • In events.js
    - Add a condition to treat every selector passed as an array
    - Parse each selector from the array passed in and get the elements. Note: this is conditioned that the video elements do not have data-var-
    - Add event unlisteners
  • In amp-video-multiple-selectors.amp.html
    - Add test with two videos.
  • In [extensions/amp-analytics/0.1/test/test-video-analytics.js] ()
    - Add test cases for VideoTracker class in events.js
  • In analytics-root.js
    - Add boolean useDataVars to condition getElements() function call to getDataVarsElement_() function.
  • In test-analytics-root.js
    - Add test to check the data-vars-* conditionality

Reviewer: @micajuine-ho , @alanorozco

@lgtm-com
Copy link
Copy Markdown

lgtm-com bot commented Jun 11, 2021

This pull request introduces 1 alert when merging 6bf51b3 into 6efb0c0 - view on LGTM.com

new alerts:

  • 1 for Unreachable statement

Copy link
Copy Markdown
Contributor

@micajuine-ho micajuine-ho left a comment

Choose a reason for hiding this comment

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

This PR is looking good so far! Left a few notes below.

One thing I think we also need to support is the fact that a selector can map to multiple elements. i.e. the class myVideoClass can be applied to multiple videos, and so if the analytics config has the selector [.myVideoClass], it should find all the elements with the myVideoClass associated. See analytics-root/#getElements().

Next up as well is to add unit tests. We'd normally add tests for this new functionality to their parent tests in test-events.js. However, tests-events does not have any tests for VideoEventTracker. So instead, I propose that we create a new tests-video-event-tracker.js file and test our new functionality. The tests will include having a fake config and initializing a VideoEventTracker for each trigger, then firing fake VideoAnalyticsEvents and letting the sessionObservable handle the rest (and test the rest as well).

@micajuine-ho micajuine-ho changed the title 🏗 Add Multiple Video Selector Functionality for amp-video -- Draft PR ✨[amp-analytics] Add Multiple Video Selector Functionality for amp-video -- Draft PR Jun 11, 2021
Copy link
Copy Markdown
Contributor

@micajuine-ho micajuine-ho left a comment

Choose a reason for hiding this comment

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

Looking really good! Just a few asks

Copy link
Copy Markdown
Member

@alanorozco alanorozco left a comment

Choose a reason for hiding this comment

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

Nice work on your first PR!

The most important change to make before approval is using a single getElements() result of target[] rather than target[][]. See my corresponding comment.

Copy link
Copy Markdown
Member

@alanorozco alanorozco left a comment

Choose a reason for hiding this comment

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

Nice work!

I'm approving, but also requesting to remove documentation changes before merging to handle them separately.

@micajuine-ho micajuine-ho changed the title ✨[amp-analytics] Add Multiple Video Selector Functionality for amp-video -- Draft PR ✨[amp-analytics] Add Multiple Video Selector Functionality for amp-video Jun 24, 2021
Copy link
Copy Markdown
Contributor

@micajuine-ho micajuine-ho left a comment

Choose a reason for hiding this comment

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

Approving, with the 1 request. And I agree that we should move the documentation to a separate PR

@micajuine-ho micajuine-ho marked this pull request as ready for review June 24, 2021 15:03
@micajuine-ho
Copy link
Copy Markdown
Contributor

Notes from our 1-1:

  • Add a unit test for 1-to-many based on same class on multiple video elements
  • Add flag for no restriction on data-vars*, add unit tests to test-analytics-root.js

@micajuine-ho micajuine-ho self-requested a review June 24, 2021 15:18
* @return {!Promise<!Array<!Element>>} Element corresponding to the selector.
*/
getElementsByQuerySelectorAll_(selectors) {
getElementsByQuerySelectorAll_(selectors, useDataVars) {
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.

Please add jsDoc for this new parameter.

Copy link
Copy Markdown
Contributor

@micajuine-ho micajuine-ho left a comment

Choose a reason for hiding this comment

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

Really great work! Two asks and then we can merge!

@micajuine-ho micajuine-ho merged commit cbf30c1 into ampproject:main Jun 28, 2021
@micajuine-ho
Copy link
Copy Markdown
Contributor

Wooohoo!!

@prathamesh-gharat
Copy link
Copy Markdown

Thank everyone!

I just wanted to check when these changes are expected to go live approximately?

I was trying to refer to this document for understanding how this works: https://amp.dev/documentation/guides-and-tutorials/learn/spec/release-schedule/ but still not getting a clear picture.

Looking forward to be able to use a single CSS selector to target all amp-youtube elements on a page.

@micajuine-ho
Copy link
Copy Markdown
Contributor

They will go live in aprox 2 weeks. Yes, @kalemuw can you provide an example of what that would look like?

@kalemuw
Copy link
Copy Markdown
Contributor Author

kalemuw commented Jul 2, 2021

Thank everyone!

I just wanted to check when these changes are expected to go live approximately?

I was trying to refer to this document for understanding how this works: https://amp.dev/documentation/guides-and-tutorials/learn/spec/release-schedule/ but still not getting a clear picture.

Looking forward to be able to use a single CSS selector to target all amp-youtube elements on a page.

Hey @prathamesh-gharat. Here's a sample config that tracks multiple video elements at once.

"myVideoPlay": {
            "on": "video-play",
            "request": "event",
            "selector": ["#my-video", "#my-video-2"],
            "vars": {
              "eventType": "video-play"
            },
            "extraUrlParams": {
              "videoId": "${id}"
            }
  }

@prathamesh-gharat
Copy link
Copy Markdown

Thanks @kalemuw.

I have a question.
Can it just be a single CSS selector which selects multiple elements?
For example

"myVideoPlay": {
            "on": "video-play",
            "request": "event",
            "selector": "amp-youtube",
            "vars": {
              "eventType": "video-play"
            },
            "extraUrlParams": {
              "videoId": "${id}"
            }
  }```

@kalemuw
Copy link
Copy Markdown
Contributor Author

kalemuw commented Jul 12, 2021

Thanks @kalemuw.

I have a question.
Can it just be a single CSS selector which selects multiple elements?
For example

"myVideoPlay": {
            "on": "video-play",
            "request": "event",
            "selector": "amp-youtube",
            "vars": {
              "eventType": "video-play"
            },
            "extraUrlParams": {
              "videoId": "${id}"
            }
  }```

@prathamesh-gharat
If the CSS selector is a just a class name, only the first instance/element with that class name will be retrieved.
However, if you want all of the elements with that class name, simply enclose the classname in square brackets.
Additionally, you can make use of id names - as simple strings or arrays of strings.
For example:

"myVideoPlay" : {
   ...
   selector: ".youTubeVideos"   // retrieves only the first elements with class name "youTubeVideos"
   ....
}
"myVideoPlay" : {
   ...
   selector: [".youTubeVideos"] // retrieves all elements with class name "youTubeVideos"
   ....
}
"myVideoPlay" : {
   ...
   selector: ["#myVideo", "#myVideo-2"] // retrieves elements with ids "myVideo" and "myVideo-2" only 
   ....
}

@prathamesh-gharat
Copy link
Copy Markdown

prathamesh-gharat commented Aug 26, 2021

@kalemuw, is it possible to access the data-videoid attribute value of an <amp-youtube> tag in analytics configuration?

Need to pass the information to GA based on the video being played.

I already tried finding on following two links but couldn't anything that worked.
https://github.com/ampproject/amphtml/blob/main/docs/spec/amp-var-substitutions.md
https://github.com/ampproject/amphtml/blob/main/extensions/amp-analytics/amp-video-analytics.md

Update: Going to copy data-videoid attribute as data-vars-videoid in the code for the time being.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants