2019-01-30 02:41:49 +00:00
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
// ABOUT THIS NODE.JS SAMPLE: This sample is part of the SDK for JavaScript Developer Guide topic at
// https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/kinesis-examples-capturing-page-scrolling-full.html
2019-02-02 02:05:23 +00:00
// snippet-start:[kinesis.JavaScript.kinesis-example.complete]
// snippet-start:[kinesis.JavaScript.kinesis-example.config]
2019-01-30 02:41:49 +00:00
// Configure Credentials to use Cognito
AWS . config . credentials = new AWS . CognitoIdentityCredentials ( {
IdentityPoolId : "IDENTITY_POOL_ID" ,
} ) ;
AWS . config . region = "REGION" ;
2019-02-02 02:05:23 +00:00
// We're going to partition Amazon Kinesis records based on an identity.
2019-01-30 02:41:49 +00:00
// We need to get credentials first, then attach our event listeners.
AWS . config . credentials . get ( function ( err ) {
// attach event listener
if ( err ) {
alert ( "Error retrieving credentials." ) ;
console . error ( err ) ;
return ;
}
2019-02-02 02:05:23 +00:00
// create Amazon Kinesis service object
2019-01-30 02:41:49 +00:00
var kinesis = new AWS . Kinesis ( {
apiVersion : "2013-12-02" ,
} ) ;
2019-02-04 19:12:04 +00:00
// snippet-end:[kinesis.JavaScript.kinesis-example.config]
2019-01-30 02:41:49 +00:00
2019-02-04 19:12:04 +00:00
// snippet-start:[kinesis.JavaScript.kinesis-example.addEventListener]
// Get the ID of the Web page element.
2019-01-30 02:41:49 +00:00
var blogContent = document . getElementById ( "BlogContent" ) ;
// Get Scrollable height
var scrollableHeight = blogContent . clientHeight ;
var recordData = [ ] ;
var TID = null ;
blogContent . addEventListener ( "scroll" , function ( event ) {
clearTimeout ( TID ) ;
// Prevent creating a record while a user is actively scrolling
TID = setTimeout ( function ( ) {
// calculate percentage
var scrollableElement = event . target ;
var scrollHeight = scrollableElement . scrollHeight ;
var scrollTop = scrollableElement . scrollTop ;
var scrollTopPercentage = Math . round ( ( scrollTop / scrollHeight ) * 100 ) ;
var scrollBottomPercentage = Math . round (
( ( scrollTop + scrollableHeight ) / scrollHeight ) * 100
) ;
2019-02-02 02:05:23 +00:00
// Create the Amazon Kinesis record
2019-01-30 02:41:49 +00:00
var record = {
Data : JSON . stringify ( {
blog : window . location . href ,
scrollTopPercentage : scrollTopPercentage ,
scrollBottomPercentage : scrollBottomPercentage ,
time : new Date ( ) ,
} ) ,
PartitionKey : "partition-" + AWS . config . credentials . identityId ,
} ;
recordData . push ( record ) ;
} , 100 ) ;
} ) ;
2019-02-04 19:12:04 +00:00
// snippet-end:[kinesis.JavaScript.kinesis-example.addEventListener]
2019-01-30 02:41:49 +00:00
2019-02-04 19:12:04 +00:00
// snippet-start:[kinesis.JavaScript.kinesis-example.putRecords]
2019-02-02 02:05:23 +00:00
// upload data to Amazon Kinesis every second if data exists
2019-01-30 02:41:49 +00:00
setInterval ( function ( ) {
if ( ! recordData . length ) {
return ;
2024-01-16 10:41:11 -05:00
}
2019-02-02 02:05:23 +00:00
// upload data to Amazon Kinesis
2019-01-30 02:41:49 +00:00
kinesis . putRecords (
2024-01-16 10:41:11 -05:00
{
2019-01-30 02:41:49 +00:00
Records : recordData ,
2019-02-02 02:05:23 +00:00
StreamName : "NAME_OF_STREAM" ,
2024-01-16 10:41:11 -05:00
} ,
2019-01-30 02:41:49 +00:00
function ( err , data ) {
if ( err ) {
console . error ( err ) ;
}
}
) ;
// clear record data
recordData = [ ] ;
} , 1000 ) ;
} ) ;
2019-02-02 02:05:23 +00:00
// snippet-end:[kinesis.JavaScript.kinesis-example.putRecords]
2019-02-02 02:12:22 +00:00
// snippet-end:[kinesis.JavaScript.kinesis-example.complete]