Skip to content

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

Merged
lannka merged 7 commits intoampproject:masterfrom
rmm:kixer/amp-ad-view
May 9, 2017
Merged

Kixer ads: Fire ad viewability when ad is in viewport#8289
lannka merged 7 commits intoampproject:masterfrom
rmm:kixer/amp-ad-view

Conversation

@jvelis-nexstar
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Contributor Author

@ampprojectbot retry!

@lannka lannka self-assigned this Mar 28, 2017
ads/kixer.js Outdated
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Contributor Author

@ampprojectbot retry!

@jvelis-nexstar
Copy link
Copy Markdown
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
Copy Markdown
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!

}, 900);
}
}
};
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.

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

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Contributor Author

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

@lannka
Copy link
Copy Markdown
Contributor

lannka commented May 4, 2017

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

@jvelis-nexstar
Copy link
Copy Markdown
Contributor Author

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

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants