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

[SD-252] Back to top event #1328

Merged
merged 1 commit into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions examples/nuxt-app/test/features/site/analytics.feature
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
Feature: Analytics

Background:
Given I am using a "macbook-16" device

@mockserver
Scenario: DataLayer - page view
Scenario: Page view
Given the site endpoint returns fixture "/site/reference" with status 200
And the page endpoint for path "/" returns fixture "/landingpage/home" with status 200
Given I visit the page "/"
Expand All @@ -10,11 +13,20 @@ Feature: Analytics
| routeChange | Demo Landing Page | / | landing_page |

@mockserver
Scenario: DataLayer - breadcrumbs
Scenario: Breadcrumbs
Given the site endpoint returns fixture "/site/vic" with status 200
And the page endpoint for path "/education" returns fixture "/landingpage/home" with status 200
Given I visit the page "/education"
Then the dataLayer should have the following breadcrumbs
| title |
| Information and services |
| Education |

@mockserver
Scenario: Back to top
Given the site endpoint returns fixture "/site/vic" with status 200
And the page endpoint for path "/" returns fixture "/landingpage/home" with status 200
Given I visit the page "/"
Then I scroll 1900 pixels
And I click the back to top button
Then the dataLayer back to top event should have a value of 25
11 changes: 11 additions & 0 deletions packages/nuxt-ripple-analytics/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,17 @@ export default {
})
}
},
'rpl-layout-back-to-top/navigate': () => {
return (payload: any) => {
trackEvent({
event: `${payload.action}_back_to_top`,
element_text: payload?.text,
value: payload?.value,
component: 'rpl-layout-back-to-top',
platform_event: 'navigate'
})
}
},
// UI Forms components
'rpl-form/submit': () => {
return (payload: any) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,11 @@ Then(
})
}
)

Then('I scroll {int} pixels', (pixels: number) => {
cy.scrollTo(0, pixels)
})

Then('I click the back to top button', () => {
cy.get('.rpl-back-to-top__button').click()
})
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,19 @@ Then(
})
}
)

Then(
'the dataLayer back to top event should have a value of {int}',
(percentage: string) => {
cy.window().then((window) => {
const event = window.dataLayer?.find(
(i) => i.event === 'click_back_to_top'
)

expect(event?.value).to.be.within(
Number(percentage) - 2,
Number(percentage) + 2
)
})
}
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,25 @@
import RplButton from '../button/RplButton.vue'
import { useWindowScroll, useElementBounding } from '@vueuse/core'
import { ref, computed } from 'vue'
import {
useRippleEvent,
rplEventPayload
} from '../../composables/useRippleEvent'

interface Props {
label?: string
topElementId: string
}

withDefaults(defineProps<Props>(), {})
const props = withDefaults(defineProps<Props>(), {
label: 'Back to top'
})

const emit = defineEmits<{
(e: 'navigate', payload: rplEventPayload & { action: 'click' }): void
}>()

const { emitRplEvent } = useRippleEvent('rpl-layout-back-to-top', emit)

// This is the number of pixels down the page you need to scroll before you see
// the back to top button.
Expand All @@ -29,6 +42,21 @@ const isSticky = computed(() => {

return offsetTop.value > window.innerHeight
})

const handleClick = () => {
const pageHeight = document.documentElement.scrollHeight - window.innerHeight
const scrollPercentage = Math.round((scrollY.value / pageHeight) * 100)

emitRplEvent(
'navigate',
{
action: 'click',
text: props.label,
value: scrollPercentage
},
{ global: true }
)
}
</script>

<template>
Expand All @@ -53,8 +81,10 @@ const isSticky = computed(() => {
iconName="icon-arrow-up"
url="#rpl-skip-links"
class="rpl-back-to-top__button"
>Back to top</RplButton
@click="handleClick"
>
{{ label }}
</RplButton>
</div>
</div>
</template>
Expand Down