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

Kixer ads: Fire ad viewability when ad is in viewport #8289

Merged
merged 7 commits into from
May 9, 2017
Merged

Kixer ads: Fire ad viewability when ad is in viewport #8289

merged 7 commits into from
May 9, 2017

Conversation

jvelis-nexstar
Copy link
Contributor

We would like to improve viewability numbers by using the position in the viewport to fire the view.

Using window.context.observeIntersection API to detect the position.

@ampprojectbot
Copy link
Member

Hi, ampproject bot here! Here are a list of the owners that can approve your files.

You may leave an issue comment stating "@ampprojectbot retry!" to force me to re-evaluate this Pull Request's status

/to lannka zhouyx

  • ads/kixer.js

For any issues please file a bug at https://github.com/google/github-owners-bot/issues

@jvelis-nexstar
Copy link
Contributor Author

@ampprojectbot retry!

@lannka lannka self-assigned this Mar 28, 2017
ads/kixer.js Outdated
@@ -39,11 +44,34 @@ export function kixer(global, data) {
d.removeEventListener('load', kxload, false);
if (d.childNodes.length > 0) {
global.context.renderStart();
view_interval = setInterval(function() {
kxview_check(); // Once the ad has loaded, start checking for an ad view
Copy link
Contributor

Choose a reason for hiding this comment

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

is such a poll necessary? observeIntersection will call the callback when there is a change.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi Lanka! Thanks for your comment. It makes sense, however, this helped to make sure the ad was in the viewport for at least 1 second and prevented duplicate/early calls to the callback function.

Copy link
Contributor

Choose a reason for hiding this comment

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

if all you want is "visible for 1s", then isn't setTimeout more appropriate than setInterval?

ads/kixer.js Outdated
const kxview_check = function(coords) {
if (coords.intersectionRect.height > coords.boundingClientRect.height / 2) {
if (viewed === false && view_timer == null) {
view_timer = setTimeout(function() {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Replaced setInterval for setTimeout as suggested by Lannka.

ads/kixer.js Outdated
d.addEventListener('load', kxload, false);
d.addEventListener('load', kxload, false); // Listen for the kixer load event

const kxview_check = function(coords) {
Copy link
Contributor

Choose a reason for hiding this comment

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

please use camel case for all local vars, e.g. kxviewCheck.

ads/kixer.js Outdated
}
}, 900);
}
in_view = true;
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: you can move this out of the if block.

inView = coords.intersectionRect.height > coords.boundingClientRect.height / 2;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks Lannka. This should be out of the block.

ads/kixer.js Outdated
d.addEventListener('load', kxload, false); // Listen for the kixer load event

const kxview_check = function(coords) {
if (coords.intersectionRect.height > coords.boundingClientRect.height / 2) {
Copy link
Contributor

Choose a reason for hiding this comment

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

can you use
coords.intersectionRatio > 0.5?

ads/kixer.js Outdated
d.addEventListener('load', kxload, false);
d.addEventListener('load', kxload, false); // Listen for the kixer load event

const kxview_check = function(coords) {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: coords -> intersectionEntry

@jvelis-nexstar
Copy link
Contributor Author

@ampprojectbot retry!

@jvelis-nexstar
Copy link
Contributor Author

jvelis-nexstar commented Apr 12, 2017

@lannka Hi Lannka, the changes are ready. Please let me know what you think.

@jvelis-nexstar
Copy link
Contributor Author

Hi @zhouyx and @lannka , my changes are ready. I would appreciate if someone could take a look at my PR whenever possible. Thanks for your help guys!

ads/kixer.js Outdated
}, 900);
}
}
};
Copy link
Contributor

Choose a reason for hiding this comment

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

if you want it to be visible for 900ms before firing, should you reset the timer when inView === false?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point, although the timer is effectively recreated when kxviewCheck() is called again by observeIntersection.

Copy link
Contributor

Choose a reason for hiding this comment

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

this is not using the memory wisely. please clear it manually

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@lannka Thanks for noticing that. The timer should now be cleared when inView is false to use memory better.

ads/kixer.js Outdated

const kxviewCheck = function(intersectionEntry) {
inView = intersectionEntry.intersectionRatio > 0.5; // Half of the unit is in the viewport
if (inView === true) {
Copy link
Contributor

Choose a reason for hiding this comment

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

you can just do if (inView)

ads/kixer.js Outdated
inView = intersectionEntry.intersectionRatio > 0.5; // Half of the unit is in the viewport
if (inView === true) {
if (viewed === false && viewTimer == null) {
viewTimer = setTimeout(function() {
Copy link
Contributor

Choose a reason for hiding this comment

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

add a comment what the timer is for.

ads/kixer.js Outdated
__kx_viewability.process_locked(data.adslot);
}
}
}, 900);
Copy link
Contributor

Choose a reason for hiding this comment

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

do you want 900ms? or 1000ms

Copy link
Contributor Author

Choose a reason for hiding this comment

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

900ms would match our current viewability settings.

@jvelis-nexstar
Copy link
Contributor Author

Hi @lannka I made the changes you suggested. Please let me know your thoughts. Thanks!

@zhouyx zhouyx added this to Backlog (Should be Closed) in 3P Ads & Analytics Support May 1, 2017
@zhouyx zhouyx moved this from Backlog to In Review in 3P Ads & Analytics Support May 1, 2017
@lannka
Copy link
Contributor

lannka commented May 4, 2017

@jvelis in case in missed the last comment:
#8289 (comment)

@jvelis-nexstar
Copy link
Contributor Author

@lannka Thanks for the last comments. Per your suggestion, I made some changes to clear the timeout more efficiently.

@lannka lannka merged commit 62e4531 into ampproject:master May 9, 2017
@lannka lannka moved this from In Review to Done in 3P Ads & Analytics Support May 9, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Development

Successfully merging this pull request may close these issues.

None yet

5 participants