Comments (1)
First off, great plugin. I started to hack together a test for a single page and thought hmm, maybe there is a plugin that has all this figured out. I was in luck. Almost immediately I ran into the caveat @alexei encountered which is the actions and labels auto generated are not very intuitive. I hacked this very simple solution into the code and it works great. It is just 2 new lines and a change to one.
function checkElements(elements, scrollDistance, timing) {
$.each(elements, function(index, elem) {
if ( $.inArray(elem, cache) === -1 && $(elem).length ) {
if ( scrollDistance >= $(elem).offset().top ) {
var action = ($(elem).data('sd-action') != 'undefined' ? $(elem).data('sd-action') : 'Elements');
var label = ($(elem).data('sd-label') != 'undefined' ? $(elem).data('sd-label') : elem);
sendEvent(action, label, scrollDistance, timing);
cache.push(elem);
}
}
});
}
As @alexei points out, I believe the data attributes are the right way to go about it so pages don't need to build and include inline scripts, we can simply tag html with data-* attributes. I even took this a step further to write a global init script so I I only need data attributes to do anything. It looks like this:
jQuery(function() {
var data_sd = $('[data-sd]');
if(data_sd.length) {
jQuery.scrollDepth({percentage: false, pixelDepth: false, userTiming: false});
$.each(data_sd, function(){
jQuery.scrollDepth.addElements(['#' + $(this).attr('id')]);
});
}
});
Now I can just happily add data attributes to any html element and have it tracked with the actions and labels I want:
<div id="track-me" data-sd="true" data-sd-action="AB Test 1" data-sd-label="Comments">
If you decide to go with the data-* approach and allow for a global constructor it would be great if your code looked for $('[data-sd]')
during initialization so we could just call jQuery.scrollDepth()
. Maybe on option can be passed telling the bindScrollDepth
method to look for elements with 'data-sd' so it only bothers to look upon request. Also, addElements()
accepting a jQuery object so an id is not even necessary would be nice too.
from scroll-depth.
Related Issues (20)
- Scroll tracking is not fiering
- please update bower to get v1.0 HOT 2
- Specify page we track HOT 2
- Integrate with Google Tag Manager HOT 2
- Correct way to separate the scrolling trackers on different pages HOT 1
- Correct way to implement using GTM? HOT 8
- Latest Version with GTM - No dataLayer pushes HOT 7
- Install scrolldepth to multiple pages of the website.
- gtag.js
- Surprisingly low number of events HOT 6
- Feature request HOT 5
- Custom names for event category, action, label HOT 1
- Any common issues that would stop any events registering in GA ? HOT 9
- Version bump missed HOT 1
- Current minified version doesn't work HOT 2
- ScrollTiming event doesn't use nonInteraction variable HOT 2
- Update npm version to 1.2 HOT 1
- WordPress Plugin Update HOT 2
- Not firing when using just gtags.js - Global Site Tag HOT 3
- The non-jQuery version seems unable to track in gtag.js HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from scroll-depth.