Reference specific occurrences of events or hit attributes

Events

By default, when you reference an event from another event you are referencing the value from most recent previous occurrence of the event.

For example, this Advanced mode event fires if the value of the referenced event equals 10:

For example, for an Advanced mode event that fires if the value of the references event equals 10, the code for the condition looks like the following.

If $F.getLastFact("NS.F_E__REFERENCED_EVENT__634291458413861581").NumericValue == 10)

In Advanced mode, the method is getLastFact. This method gathers the most recent occurrence. If you want to specify another occurrence, the syntax is the following:

$F.getFact("<FACT>", <OCCURRENCE>)

For example, if you want the Advanced mode Event to fire on the third occurrence in the session of when the referenced event = 10, then the condition is the following:

If $F.getFact("NS.F_E__REFERENCED_EVENT__634291458413861581", 3).NumericValue == 10)

Hit attributes

The Event Manager GUI allows you to specify whether to use the first or last match of the hit attribute on a hit in the condition.

if ($P["NS.P__CUST_HIT_ATTRIBUTE__634291466155274364"].firstValue() == "10")

In the above condition, the hit attribute object ($P) named P__CUST_HIT_ATTRIBUTE__634291466155274364 for the customer namespace NS is configured to test whether the first value is set to the literal value 10.

If you want to specify a specific match on a hit, the syntax is:

$P["<CUST HIT ATTRIBUTE">].valueAt(<OCCURRENCE>).

To get the value the third occurrence of the hit attribute on the page, the condition would be:

if ($P["NS.P__CUST_HIT_ATTRIBUTE__634291466155274364"].valueAt(3) == "10")

Note: Retrieving a specific instance of a hit attribute on a page is not supported in Basic mode.

It may be common that you do not know which occurrence of a pattern on a hit contains the wanted value. If you want to check to see if any of the pattern occurrences have the wanted value, you can use a FOR loop:


for (var i = 0; i < $P["<CUST HIT ATTRIBUTE">].matchCount(); i++)
  {
  var p = $P["<CUST HIT ATTRIBUTE">].valueAt(i);
  if (p == "<DESIRED VALUE>")
    {
    $F.setFact("<FACT>", p);
    }
}

Examining the code:


for (var i = 0; i < $P["<CUST HIT ATTRIBUTE">].matchCount(); i++)
  • Determine how many times the pattern occurs on a hit, then runs the for loop that many times, using i as the counter.
    
    var p = $P["<CUST HIT ATTRIBUTE">].valueAt(i);
    
  • Get the value of the pattern at occurrence i and assign it to variable p.
    
    if (p == "<DESIRED VALUE>")
    
  • If the value of p equals the wanted value then:
    
    $F.setFact("<FACT>", p);
    
  • Write the value of p as the value of the fact.

In this code, the for loop continues even after a match is found, until all occurrences of the pattern on the hit were evaluated. Since no wanted value is found again, no other fact is written. If it does find the wanted value again, it writes the wanted value, but you still get that same value. You can choose to insert the break; command to exit after the value was found.