Essentially, the default syntax for event code that is generated from the Event Manager
is an IF/THEN
statement.
- The Condition step in the Basic mode for the event defines the
IF
statement. The Value and Report Groups steps are theTHEN
statement. - There is no strict requirement that code is constructed in an
IF/THEN
statement.
Advanced Mode JavaScript™ example
In Advanced mode, this event looks like the following:
// Generated by Event Manager
// NOTE: Do not change event name
function NS$E__ADVANCED_MODE_EVENT__634339708610416368()
{
if (!($S.IsBot))
{
// Set fact for Report Group: No Dimension Report Group
$F.setFact("NS.F_E__ADVANCED_MODE_EVENT__634336419284199494", "TLT$NULL");
}
}
Examine the components:
function NS$E__ADVANCED_MODE_EVENT__634339708610416368()
- This statement defines the event as a function.
NS$E__ADVANCED_MODE_EVENT__634339708610416368
is the full name of the event.NS
is the namespace, which is your unique identifier. This value cannot be changed within your Experience Analytics solution.$
is a function name delimiter between the namespace and the internal function name.E
signifies that the function implements an event.ADVANCED_MODE_EVENT
is the user-selected name of the event. After the event was created, this name does not change even if the name is changed in the GUI.634339708610416368
is a string that is added to the object name to guarantee uniqueness. It is based on the created time epoch and other factors. This string value is used to enable multiple events that have the same name in the GUI.- Underscores (
_
) are used to separate individual words in the name. - Double underscores (
__
) are used to separate the parts of the internal name.
if (!($S.IsBot))
- This string is the condition definition.
$S
is the type of object that is used in the condition. In this case, it is a session attribute.IsBot
is the name of the session attribute. In this case, it is a system session attribute and not a custom session attribute, although that information is not indicated in the string.!
is theNOT
operator that is applied to the session attributeIsBot
.
$F.setFact("NS.F_E__ADVANCED_MODE_EVENT__634336419284199494", "TLT$NULL");
- This string writes the value of the event.
$F
is the fact collection object. Write the value into the Fact for this event.setFact
is the method in the fact collection object that is used to set the value of the fact specified."NS.F_E__ADVANCED_MODE_EVENT__634336419284199494"
is the name of the fact to which the value is written.NS
is the namespace, which is a unique for the customer.F_E
is the type of the object to which method is writing. In this case, it is the Fact for Event.ADVANCED_MODE_EVENT__634339708610416368
is the internal name of the object.
"TLT$NULL"
is the value to record. In this case, the dimension constant value for null values is recorded.Within the Event Manager, you can define a standard value that is recorded for a dimension when a null value is detected. In the JavaScript, this value is referenced using
TLT$NULL
.
Examining event behavior
Based on the event definition, the following behaviors are occurring for the event:
If this session is not a Bot
Then
Get the last count of the how many times this event fired and
increment that value by 1
Update the count with that incremented value
Report group syntax
Now, add a report group to the event. In Advanced mode, the event now looks like the following:
// Generated by Event Manager
// NOTE: Do not change event name
function NS$E__ADVANCED_MODE_EVENT__634339708610416368()
{
if (!($S.IsBot))
{
// Set fact for Report Group: No Dimension Report Group
$F.setFact
("NS.F_E__ADVANCED_MODE_EVENT__634339708610416368", "TLT$NULL");
// Set fact for Report Group: URL/Host/App/Server
$F.setFact
("NS.F_E__ADVANCED_MODE_EVENT__634339708610416368_FACT1", "TLT$NULL");
}
}
The only change is the addition of the new setFact
call. This call adds a report
group to the event.
There is one major difference in how this fact is specified versus the "default" fact. That is
the fact suffix at the end of the fact name (FACT1
):
NS.F_E__ADVANCED_MODE_EVENT__634286306745774758_FACT1
Fact Suffix
The default fact does not have a suffix at all. Any report groups that are added to the event are assigned a fact suffix, including an index number.
The fact number suffix represents the order in which the report group was added to the event. It
is not necessarily the order that the existing report groups are displayed in Basic mode of the
event. For example, if you add Report Group A to Event X, it is assigned to Fact1
.
But if you remove Report Group A and add Report Group B, then Report Group B is assigned
Fact2
even though there is only one explicitly added report group remaining on
Event X. If you re-add Report Group A, then that added fact is still Fact1
, even
though it is displayed as the second report group on Event X in Basic mode.
The other thing to notice is that the same value (in this case count in session) gets written into both facts by default. Through Advanced mode, it is theoretically possible for a different value to be written into each fact. However, writing a different value in each fact is not recommended and may produce anomalies in reporting. The best practice is to create a separate event for each recorded value.
Current Event Facts
In the Current Event Facts header in the left column of the Event Manager in Advanced mode, there are 2 facts. One is the name of the event, and the other is the name of the event and the name of the added report group.
To find out the fact number, move your mouse over the fact. The internal name of the fact, which is the value that is used in JavaScript, is displayed.
Adding multiple conditions
In this example, we define the arbitrary limit of 5 hits in the session as the baseline for determining if the session is interesting and from a real visitor to the web application.
It is common for customers to want to know the count of real visitor sessions. While this event was filtered out the sessions that are reported as bots, some sessions may be so short as to not be interesting.
In the Basic mode definition, the All of the following conditions must be met
value was selected from the drop-down. This selection defines a logical AND
.
Hit
Count Running Total
is compared to the fixed value 5. If the condition is defined to be
greater than our equal to 5, then the event fires for all remaining hits of the session as soon as
the fifth hit was detected.And conditions in Advanced mode
The Advanced mode for the conditions now looks like the following:
if (!($S.IsBot) && $S.NumberOfHits == 5)
The &&
is a JavaScript
operator for a logical AND
. Both conditions must be true for the Value and Report
steps to run.
Or conditions in Advanced mode
If we only want it to be either, the Advanced mode for the condition now looks like this:
if (!($S.IsBot) || $S.NumberOfHits == 5)
The ||
is a JavaScript operator for
a logical OR
. Either condition must be true for the Value and Report steps to
run.
Recording counts from last hit
Now, record the number of hits in the session. For this event, we change the conditions to be the following:
- Change the drop-down to perform a logical
AND
. (All conditions must be met.) Essentially, fire only on the last hit and it is not a Bot and if the hit count is greater than 5. - The Last Hit trigger ensures that this event fires once only.
- Because this event is now configured to fire only on the last hit of the session, the condition
to test hit count must be changed to
greater than or equal to 5 hits
, instead ofequals 5 hits
. Else, it fires only when theHit Count Running Total
is five hits exactly.
For the value, write the hit number for the last hit as a numeric value for the event.
For simplicity's sake, remove the report group that was added second, just leaving the default fact. The Advanced mode for this event looks like this:
This event writes the current value of the hit attribute Hit Number in Session
whose internal name is $H.HitNumber
. You could use other values, such as the
session attribute Hit Count Running Total
. However, the hit attribute was chosen to
show other objects that are used as values.
Reference specific occurrences of events or hit attributes
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.
Events
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 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")
You might 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, usingi
as the counter.var p = $P["<CUST HIT ATTRIBUTE">].valueAt(i);
- Get the value of the pattern at occurrence
i
and assign it to variablep
.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.