Blog Title Scraping - adobe-analytics

I think I might just be over complicating things instead of keeping it simple.
My question is: I want to capture the title of a blog post into a prop variable, and the author who wrote it into another prop variable.
My thought would be to create a page load rule focusing only on the path of /blog. From there I would scrape the page looking for the class that defines it, and then pass it into my prop through DTM.
<div class="field field-name-title">
<h2>Online Education</h2>
<div class="field field-name-body">
<p>
<em> by Author Name</em>
</p>
</div>
</div>
I create a page rule pick my prop and set it as: div.field.field-name-title.innerText But when I set it, all I'm seeing being passed is the "div.field.field-name-title.innerText"
Am I tackling this in the wrong way?

The values you enter in a text field are literal, with the exception of %data_element% syntax, which signifies a reference to a Data Element (there are a couple of other built-in variable references, as well).
Point is, if you want to populate your Adobe Analytics variable from scraping page content, you need to create a Data Element that returns the desired value, and then reference the Data Element in the text field for the Adobe Analytics variable.
That aside, your selector is wrong. What you've done is some weird mix of css selector and javascript syntax.
Below is an example of what you can do, based on your posted HTML:
<div class="field field-name-title">
<h2>Online Education</h2>
<div class="field field-name-body">
<p>
<em> by Author Name</em>
</p>
</div>
</div>
Data Element: Article Title
First, create a Data Element to get the article title from the page, based on your html structure.
Go to Rules > Data Elements > Create New Data Element
Fill out the fields with the following:
Name: article_title
Type: CSS Selector
CSS Selector Chain: div.field-name-title h2
get the value of: text
[X] Scrub whitespace and linebreaks using cleanText
Then, click Save Changes
Data Element: Article Author
Next, create another Data Element to get the article author from the page, based on your html structure.
Go to Rules > Data Elements > Create New Data Element
Fill out the fields with the following:
Name: article_author
Type: CSS Selector
CSS Selector Chain: div.field-name-body em
get the value of: text
[X] Scrub whitespace and linebreaks using cleanText
Then, click Save Changes
Page Load Rule: Populate Variables
Finally, within the various form fields of your Page Load Rule, you can now reference your Data Elements with %data_element_name% syntax.
Tip: Once you start typing the Data Element name out (starting with % prefix), DTM will show an auto-complete dialog, listing Data Elements matched.
If you need to reference the Data Element within a javascript custom code box within the Page Load Rule, you can use the following syntax:
_satellite.getVar('data_element_name');
Where 'data_element_name' is the name of your Data Element.
Example:
s.prop1 = _satellite.getVar('article_title');
Note: Unlike the form field syntax, you should not wrap your Data Element's name with %

Related

Capybara Selecting CSS classes via regexp Ruby

Is there a way to find all div's on a page via their css class using a regexp if the classes match the regex? Below is an example snippet:
<div class="runtime">...</div>
<div class="runtime2">...</div>
<div class="runtime3">...</div>
I was hoping there is a way to get all divs via a regex because there could be more div's I want to find following that class format on the page but they change on a page by page basis.
For that format you can use a starts-with, and contains attribute selector
all('div[class^="runtime"], div[class*=" runtime"]', minimum:1)
The second selector is for the case where there's another class preceeding runtime... in the element. For a more general case there is no built in way to use a regex for class matching, although you could get an array of all the divs and then filter that based on the class attribute yourself (not going to be very performant)

How to fill in input field that is the child of an element that sits next to an element with given text

Inside a capybara test, I need to fill in a text field that doesn't have a unique id or class attribute.
The text field (the field called title in this case) can appear anywhere on the page. The only thing we know is that the text field is wrapped in a div and this div sits immediately after an h3 tag which has the content "Title"
<h3>Title</h3>
<div class="input-row clear">
<input id="ember5046" class="ember-view ember-text-field" type="text">
</div>
<h3>Work Location</h3>
<div class="input-row clear">
<input id="ember5048" class="ember-view ember-text-field" type="text">
</div>
How can I do it?
We are not allowed to use xpath (company policy)
We are not allowed to do index based selectors like all("input")[0]
I think it should be possible with the css adjacent siblings combinator selector. Something like:
h3[text=Title] + div
This gives you the next element matching the thing after the +. If you rather want everything that matches you could use the ~ operator instead. These siblings selector can only help you to find things after a certain node. Going backwards is not possible as far as I know.

append to collabsible content after the header?

i am running into problem, concerning append. i ve a dynacmic collabsible, which i fill with a dynamic list. i want to append this list after the header h3 of the collabsible.
when i append it to the collabsible, it does not appear in the
<div class="ui-collapsible-content ui-collapsible-content-collapsed"> </div>
but after. therefor i get a space between the content header and the list, which i want to avoid.
i tried this:
$('some-selector > ui-collapsible-content ui-collapsible-content-collapsed') but it does not work.
any hints?
If you are trying to append inside
<div class="ui-collapsible-content ui-collapsible-content-collapsed"> </div>
Then you should use:
$('div.ui-collapsible-content.ui-collapsible-content-collapsed').append($content);
Where $content is either a jQuery object, a DOM element or a HTML string. Note the . in the selector, which specifies a class (or two).
See append.

Passing parameters to ASCX user control in MVC

I am trying to pass a string parameter to an ASCX. I want to set this to the text property of a label. In the code below it shows betwen the div tags (ignore the % signs in the html tags).
<# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<string>" >
<%asp:Label ID="Label2" runat="server" **Text="<%# Model %> Test"** CssClass="isslabel"><%/asp:Label>
<%div><%: Model %><%/div>
However in the label tag no matter what I put between the angle brackets (bit it bold) in the text tag I cannot get the parameter to appear. I have tried <%: Model %> to no avail. Is the issue that the code block is inside quotes and am I just missing some character?
Why are you trying to use a web forms user control with MVC?
Assuming the model is the string you want to display and you are passing this through correctly, along the lines of
<% Html.RenderPartial("MyUserControlView", "My String To Display"); %>
In your "parent" page, you will be able to do the following in your ascx:
<%= Html.Label(Model) %>
Instead of <asp:label...
Update
If you need to specify then you have a number of options, you could wrap the Html.Label call in a div and specify the class of the div (updating your css accordingly), you could use a display template, or simply explicitly use the Html like the following:
<label for="someIdThatICouldUseAnotherHtmlExtensionMethodToGet"><%: Model %></label>
The key problem with your code (as now also pointed out in the comments by #mare) is that you are trying to use a web forms control in an MVC view.

How to get the foreign key column data when it is used in a select box

I have this ruby on rails code
<%= builder.select(:serving_size_id, '') %>
I have not specified any options on purpose because I set the options in a different way when the page loads (using jQuery and Ajax).
The question: Is there any way I can get the value from the column "serving_size_id" but not change that line? I have a partial which I use it for new and edit and I think it would be sweet if I can do the setting of the selected index in JS.
Any ideas?
I'm not sure I completely understand your question, but if you want to set the value of the select field with JavaScript, you need to obtain the value in JavaScript at some point. I can think of two ways of doing this:
1) When you get the options via AJAX, have the server indicate which one is selected. This can be done by returning HTML <option> tags with selected="selected" set for one of them. To do this, your AJAX request is going to have to provide information about the object this select field is for (so the server can look up the object's current serving_size_id value).
2) When you render the field in your original partial, also render some JavaScript which sets the current value of the field, for example, underneath what you have above:
<%= javascript_tag "var ssid = '#{builder.object.serving_size_id}';" %>
Then, after the options are retrived via AJAX, the ssid variable is checked and the correct option is selected.
using jQuery in rails is easy but a little more difficult than prototype.
ex: "div id="serving_size" class="nice" rel="<%=h num%>">Stuff Goes Here.../div>"
in application.js do the following:
//application.js
$(document).ready(function(){
if($('#serving_size'){
$('#serving_size').live("mouseover",function(){
//we are hovering over specific div id serving size
if($('#serving_size').hasAttr('rel')){
alert($('#serving_size').attr('rel'); //your dynamic rel value, and fire function
}
}
}
if('.nice'){
$('.nice').live("mouseover",function(){
//we are now hovering over any item on page with class nice
if($(this).hasAttr('rel')){
//we are now using jQuery object ref and finding if that obj has attr rel
alert($(this).attr('rel')); // shows dynamic rel value
}
}
}
});
If you use the above code you should be able to do anything you want and fire any custom code from each of your set event callbacks.
The 'live' function in jQuery is great because it can be called on items that will eventually be on the page (eg. if you fill in something with ajax, jQuery will be prepared for that item being in the page)
I hope this help.

Resources