Collecting that you would like to collect. This data can be hidden in metadata or other information stored in inputs that have been selected. On Single Page Applications, several pieces of information are not automatically captured by CI360 Discover without setting up custom events with Attributes or the JS API.
This is a guide that takes through how to use CSS selectors and how to use lesser known selectors that might prove useful for collecting data from a customer's website without the need to use the JS API which in many cases is limited by the Customer's
Those that need to collect information complex information from a customer's website that isn't readily available using the built-in CI360 interface.
Due to the fast-changing nature of web technology be sure to stay up to date with best practices. This document's purpose is not to replace usage of the JavaScript API which is recommended for Single Page Applications (SPAs) but to supplement it.
CSS selectors are a tool to help select certain elements of a website page, specifically within the Document Object Model (DOM). Within the DOM you will find a hierarchy that defines how the webpage is display and defines how the document formats data. CSS selectors allow you to select elements within the DOM for data collection.
This section contains many of the most basic selectors that can be helpful. Remember when using these selectors that if not used in explicitly or when used with contains function they will capture every element that
You can select any element tag using this selector, and it's the simplest of all the selectors.
A selector of h2 would select a <h2></h2> element, div would select all <div></div> elements and so on.
This selector is very generally and is usually combined with chaining in order to specify specific areas of the DOM and usually never by itself because it can select many elements on a website, including elements we might not want.
ID selectors allow you to select specific ids of an element.
#sas would help to select an element with an id="sas" attribute
Example: <div id="sas"></div>
Like the ID selector, class selectors allow you to select specific div classes
.ci360 would help to select an element with a class="ci360" attribute
Example: <label class="ci360">Hi Mom!</label>
If the basic attributes like id or class aren't present or unique enough, you can always use attribute selector. The attribute selector is far more flexible than either id or class because it can be used on any attribute
[discover="true"] helps to select discover="true"> attribute
Example: <div discover="true"></div>
It's important to remember that the DOM is a hierarchy of elements. Elements can have any number of child elements and those child elements can have any number of child elements and so on. Any element in a DOM can be selected with CSS selectors.
CSS Selectors chain together have the form as below:
#class > element > element.class > element:nth-child(2) > element
An actual example of this:
#root > div > div.page__content > div:nth-child(2) > div > div > div > div
the 'a' div highlighted below:
With this we can see that it starts at:
- #root - using the # it selects an element with an id of root (id="root") in this case it is a div element, but it could be any element (<p>, <a>, etc.)
- div - selects the next div element, since there is no other div elements in this part of the hierarchy, there is no need to specify which one
- div.page__content - the '.' Means that you want to select a class with that name, so in this case we want a div element with a class of 'page_content'
- div:nth-child(2) - this selects the 2nd div of the page__content class div, each of the specific sections under a div (they don't need to be a div, they can be any different type of element underneath (<p>, <a>, etc.). For example, if there were two <p> elements
- div - again selects the next div element, since there is no other div elements in this part of the hierarchy, there is no need to specify which one
This defines exactly where the selector is in the document. In many cases this is what the selector in the GUI interface for CI360 allows us to select. We can select the section that we want but the example selector is very specific and rigid. This can be an issue when the document completely changes. If a developer decides to add another div into one of the sections, we would fail to select the correct div for data collection.
Elements can have multiple classes defined. For example, the following element:
<div class="ci360 sas discover"> </div>
Has three classes:
- ci360
- sas
- discover
We can use this to our advantage as elements with unique classes can be combined using a period.
The above example can be selected using any of the following:
- .ci360
- .ci360.discover
- .ci360.sas
- .ci360.discover.sas
- .ci360.sas.disocver
Not only can you use the above, but any combination of classes combined with a period between selects the example element.
Elements with specific attributes can be selected by simply using a tag combined with attribute selection.
h2[ci-data] can select h2 elements <h2 ci-data="yes"></h2> or <h2 ci-data="no> the value of the attribute in this case does not matter.
This is another tool that allows us to exclude specific tags. Using :not(p) would exclude any p tagged elements. We could combine this with other selectors to great effect. :not(p.ci360) would exclude the following element <p class="ci360"> .
This one is particularly useful to pull information from surveys or forms with inputs. By using input:checked selector you can easily select the value that a user has chosen or filled in.
Remember that many interactions with a site you might not realize are input elements. This one can be interest when combined with a click event to capture user input in a survey or form that is not already tracked automatically by CI360
In many instances, these selectors are selected by CI360 when using the UI. The CI360 user interface places the customer's website in an iFrame and then from there we can select what we want. Whether it be on page load or a page click.
Below is the iFrame selector interface where we load our customer's website to select locations easily.
Then later we can select a target id, or several other attributes of the click target to trigger an event
In many cases this can be all that you need to do to capture this click event. The click event will only happen when you click on a specific element with these attributes:
- id - <div id="example"> this is a div element with an id of example
- name - <a name="example"> 'a' element with a name of example
- href - <a href=www.example.com\> this is generally a URL to another page or an anchor tag (#) to somewhere on the page
- selectorPath - same as earlier where we choose the path to where it goes, above example an exact path but you can also capture clicks from sections of the site instead of a specific element using "contains" functions, which will be covered later in this document
- innerText - This is the text between the opening <a> and closing tags </a> of your elements <a>example<a/>
- onClick - This is generally JavaScript code or a function that is called in order to have an action occur on click <a onClick="example()">
- HTML5 Custom Data attributes <a data-example1="1" data-example2="2">.
In many of the cases above you can use "contains" instead of "equals" to have a more general solution. This allows more flexibility in how you define your event and, in many cases, allows your event to have more longevity when it comes to site changes. Developers might change the site, but your event will still capture what is needed.
In many cases the simplified solution above is easy to implement. It allows for the selection of elements in a visual environment that is easy to understand. But what if you want to capture clicks for an event from a section of the site as opposed to only one place. A user can click on a survey and choose whatever they want from four different choices. You wouldn't want your event to fire on only one because that complicates data management downstream.
In the example below you can see someone is choosing a value from 4 choices based on the question:
In this case we want to capture all clicks on any of the selections from "YES" to "NEARLY EVERY DAY". Using the browser developer tools we can select the area that we want capture click events and check if there are any unique values that would allow us to capture on any of the options
We first select the element using the tool on the left:
Then we can select the tool and the area we want:
Placing your mouse over specific elements in the developer's tools lets you view an overlay over the specific area
We can see from the above, that selecting the third element under answer-options allows us to select that element above. We could at this point choose to set our selector to each of these answers but you would find that it would select the specific element without a general solution, meaning we would have to setup a rule for each answer for each question (up to 40 different options)
We see that the div with a class="answer-options" contains the area where we want to capture clicks. In addition to this, other survey questions later use this same structure allowing us to pull that data in correctly.
We use the contains option to instruct CI360 that any selector path that contains "answer-options" to be eligible to trigger our event. Every child of the class answer-options div and all their children and so on, will be triggered by this. This is also important because any elements under our chosen element will still fire our event, even if changes are made to the underlying DOM but specifically the item that is clicked on in our example is actually an input element nested further down, setting the selectorPath to contain lets us be certain we capture clicks from an area on the page instead of specifying a specific child element.
For example, if we were to use a selector with equals evaluator and use a CSS selector that selected the Div at the top, we would only receive clicks on that Div. This might run into issues where the browser interprets clicking on one of the BLUE a elements or RED inputs and doesn't include the top-level div. We would make sure that we use contains evaluator.
Using contains for example on the input element (red box), we can then capture any of the tree below. All the RED and BLUE squares form the diagram above would be selected and any click on those elements would be captured.
In the case of the example above, simply selecting the correct item is not enough. In many cases we want to capture additional data. For the click event an important distinction to make is that the any additional information captured in custom attributes will be captured from the page as it was when you clicked it. This means that any changes that occur to the webpage/DOM that occur will not be captured. We can use CSS selectors to select things on the page at the time of the click but there are many techniques within CSS selectors that allow us to pull specific items and values based on exclusion or different attributes.
In this example using the same survey from before. We can see that the element nested furthest down the DOM tree is an input element for all our options. When we click on an option, we want to capture which value was selected. In some cases, this is simple but in other cases we can run into issues.
There are several different considerations to make in this case:
- There are several different questions that are hidden in the DOM using JavaScript on the customer's website as a part of their Single Page Application (SPA). We want to make sure we don't select those for capturing the data
- We need to return attribute values from our elements by finding identifying values that we can capture from the list available to us in CI360
- If the attributes don't cover any of items or the development of the page has made it unlikely that we can capture what we want due to misplacement or these variables we can explore other options
- When selecting what to capture, it can be difficult due to JavaScript constantly changing the page
- Text that you want to capture could be in a difficult page, in our example the text is the inner text of the label element and not the input which makes it difficult to use innerHTML
The solution requires advanced usage of CSS selectors. There are many selectors that you can use as mentioned earlier and Linked Here. Several of these allow for selecting specific elements based on their class, multiple classes, their Id, or specific attributes including how those attributes start, ends, contains etc.
Hidden Questions
Using the list of CSS elements, we can see the below:
We can see that there are 10 questions and we are on the first one. The identifying CSS for this is that the class does not contain "ng-hide". Using our CSS selector guide we can see these two items specifically:
- :not(selector) - :not(p) - Selects every element that is not a <p> element
Using the not selector we are able to exclude any of the inactive questions from capturing data.
Further down the DOM tree there is an input element. These elements hold data, in many cases they look like radio buttons or check boxes. But in this case the client cannot see those in the display window. But since we know that it is an input and can be selected, we can use the following selector to select this element:
- :checked - input:checked - Selects every checked <input> element
Luckily by selecting this input element it also has a value attribute that contains a number which corresponds to the selected answer (0,1,2, or 3). In the CI360 interface we can setup a custom attribute for the event to capture the values we want
The following is the selector code used to make sure we capture correctly:
- div.ng-fade.row:not(.ng-hide)>div.answer-options>div>label>input:checked
We have successfully captured the value of the selected answer on click.
Testing the above can be tricky in many cases. The easiest way to test if what you're collecting is correct is through setup of an agent and/or looking through the agent log. For intermediate testing of CSS selectors we can use an addon here: CSS Selector Tester. There are many tools that you can use of similar nature but I find this one to be simplest to use.
With this addon we can simply navigate to a webpage and use the CSS selector code to view if our selector code works. If it does, it will surround the section(s) we are looking for in a red-dotted line.
In this pictured example, we can use ".tile__title" as the selector and the three boxes are shown around the three areas above that we have selected.
There are many other situations where CSS selectors can be used, here is an updated list of most. The list linked is copied into the appendix here for easy reference.
| Selector | Example | Example description |
|---|---|---|
| .class | .intro | Selects all elements with class="intro" |
| .class1.class2 | .name1.name2 | Selects all elements with both name1 and name2 set within its class attribute |
| .class1 .class2 | .name1 .name2 | Selects all elements with name2 that is a descendant of an element with name1 |
| #id | #firstname | Selects the element with id="firstname" |
| * | * | Selects all elements |
| element | p | Selects all <p> elements |
| element.class | p.intro | Selects all <p> elements with class="intro" |
| element,element | div, p | Selects all <div> elements and all <p> elements |
| element element | div p | Selects all <p> elements inside <div> elements |
| element>element | div > p | Selects all <p> elements where the parent is a <div> element |
| element+element | div + p | Selects all <p> elements that are placed immediately after <div> elements |
| element1~element2 | p ~ ul | Selects every <ul> element that are preceded by a <p> element |
| [attribute] | [target] | Selects all elements with a target attribute |
| [attribute=value] | [target=_blank] | Selects all elements with target="_blank" |
| [attribute~=value] | [title~=flower] | Selects all elements with a title attribute containing the word "flower" |
| [attribute|=value] | [lang|=en] | Selects all elements with a lang attribute value starting with "en" |
| [attribute^=value] | a[href^="https"] | Selects every <a> element whose href attribute value begins with "https" |
| [attribute$=value] | a[href$=".pdf"] | Selects every <a> element whose href attribute value ends with ".pdf" |
| [attribute*=value] | a[href*="w3schools"] | Selects every <a> element whose href attribute value contains the substring "w3schools" |
| :active | a:active | Selects the active link |
| ::after | p::after | Insert something after the content of each <p> element |
| ::before | p::before | Insert something before the content of each <p> element |
| :checked | input:checked | Selects every checked <input> element |
| :default | input:default | Selects the default <input> element |
| :disabled | input:disabled | Selects every disabled <input> element |
| :empty | p:empty | Selects every <p> element that has no children (including text nodes) |
| :enabled | input:enabled | Selects every enabled <input> element |
| :first-child | p:first-child | Selects every <p> element that is the first child of its parent |
| ::first-letter | p::first-letter | Selects the first letter of every <p> element |
| ::first-line | p::first-line | Selects the first line of every <p> element |
| :first-of-type | p:first-of-type | Selects every <p> element that is the first <p> element of its parent |
| :focus | input:focus | Selects the input element which has focus |
| :fullscreen | :fullscreen | Selects the element that is in full-screen mode |
| :hover | a:hover | Selects links on mouse over |
| :in-range | input:in-range | Selects input elements with a value within a specified range |
| :indeterminate | input:indeterminate | Selects input elements that are in an indeterminate state |
| :invalid | input:invalid | Selects all input elements with an invalid value |
| :lang(language) | p:lang(it) | Selects every <p> element with a lang attribute equal to "it" (Italian) |
| :last-child | p:last-child | Selects every <p> element that is the last child of its parent |
| :last-of-type | p:last-of-type | Selects every <p> element that is the last <p> element of its parent |
| :link | a:link | Selects all unvisited links |
| :not(selector) | :not(p) | Selects every element that is not a <p> element |
| :nth-child(n) | p:nth-child(2) | Selects every <p> element that is the second child of its parent |
| :nth-last-child(n) | p:nth-last-child(2) | Selects every <p> element that is the second child of its parent, counting from the last child |
| :nth-last-of-type(n) | p:nth-last-of-type(2) | Selects every <p> element that is the second <p> element of its parent, counting from the last child |
| :nth-of-type(n) | p:nth-of-type(2) | Selects every <p> element that is the second <p> element of its parent |
| :only-of-type | p:only-of-type | Selects every <p> element that is the only <p> element of its parent |
| :only-child | p:only-child | Selects every <p> element that is the only child of its parent |
| :optional | input:optional | Selects input elements with no "required" attribute |
| :out-of-range | input:out-of-range | Selects input elements with a value outside a specified range |
| ::placeholder | input::placeholder | Selects input elements with the "placeholder" attribute specified |
| :read-only | input:read-only | Selects input elements with the "readonly" attribute specified |
| :read-write | input:read-write | Selects input elements with the "readonly" attribute NOT specified |
| :required | input:required | Selects input elements with the "required" attribute specified |
| :root | :root | Selects the document's root element |
| ::selection | ::selection | Selects the portion of an element that is selected by a user |
| :target | #news:target | Selects the current active #news element (clicked on a URL containing that anchor name) |
| :valid | input:valid | Selects all input elements with a valid value |
| :visited | a:visited | Selects all visited links |
| Item | Link |
|---|---|
| CSS Selector Table (used above) | https://www.w3schools.com/cssref/css_selectors.asp |
| CSS Selector Tester | https://www.w3schools.com/cssref/trysel.asp |
| CSS Selector Tester Addon (for Chrome) | https://chrome.google.com/webstore/detail/css-selector-tester/bbklnaodgoocmcdejoalmbjihhdkbfon |













