Introduction
Scripted automation is an advanced tool which allows programmatic creation of automation rules. Using this tool, a combination of SQL and JavaScript can be employed to create powerful and complex automation rules.Requirements
- Basic knowledge of SQL
- Basic knowledge of JavaScript
- A trigger produces 0 results.
- The last (bottom) action or trigger completes execution.
- The StopRule() function is called within an action.
Conceptual
Before diving into scripted rule creation, it is important to understand how a rule is created from a conceptual standpoint. This means understanding how to properly break up your desired automation scenario into triggers and actions. Trigger — Defines what set of conditions must take place in order for this rule to continue execution. Action — Defines what actions to take on each of the results returned by trigger.Trigger: | Campaigns with CPA greater than $5. |
Action: | For each of these campaigns, pause the campaign. |
Trigger: |
Adgroups with less than 20 installs Keywords with bids > $3 Keywords containing “chocolate” |
Action: | For each of these keywords, reduce the bid by 25%. |
Triggers
Triggers represent the set of conditions which must be met in order for the rule to execute further. Triggers are defined using SQL. Click the Add Trigger button to add a trigger to your rule. The contents of a trigger can be thought of as the WHERE clause of an SQL query run against the current snapshot of your ASA data existing as campaign, adgroup and keyword tables. All columns within these tables are available to you as first-class variables in the right pane of the view during trigger construction and can be referenced using the syntax {table}.{column}. When a trigger runs and the SQL query is performed, tables are joined and all campaign, adgroup and keyword entities are recursively returned so that actions may be performed on those results. For example, if a single campaign is returned then all of its child adgroups and all of its child keywords are returned as well. As another example, if a single keyword is returned, its parent adgroup and parent campaign are returned as well. Example — Return all entities where campaign CPA > $10campaign.cpa > 10
- Campaigns: Those with a CPA > 10
- Adgroups: Child adgroups of the returned campaigns
- Keywords: Child keywords of the returned adgroups
keyword.bid > 1.25
- Keywords: Those with a bid > 1.25
- Adgroups: Parent adgroups of the returned keywords
- Campaigns: Parent campaigns of the returned adgroups
campaign.cpa > 10 AND keyword.bid > 1.25
- Campaigns: Those with a CPA > 10 and which also have keywords with bids > 1.25
- Adgroups: Child adgroups of the returned campaigns
- Keywords: Those with a bid > 1.25 and which also belong to campaigns with a CPA > 10
Development Notes
- A scripted automation rule must contain at least one trigger and action.
- If no entities are returned by a trigger, execution of the rule stops and no further actions or triggers are evaluated.
Actions
Actions are broken up by type (campaign, adgroup, keyword and one-time) and represent what actions to take on each of the resulting campaigns, adgroups or keywords of the previous trigger. Click the Add Action button of the desired type to add an action to your rule following a trigger. Actions are defined using JavaScript. A typical action script block consists of calling one or more functions to accomplish a task but may also contain logical operations, variable assignments, cache storage and any other basic JavaScript syntax necessary for your rule. A single action script block applies to each of the entity results of the previous trigger and can be thought of as a for-each iteration over the entity results of that type. For example, let’s look at a scenario where a trigger returns five campaigns, three adgroups, and ten keywords. In this scenario, a campaign action block would run five times (once for each campaign returned), an adgroup action block would run three times, and a keyword action block would run ten times. When an action block runs, it is scoped to each individual entity from the trigger result. The properties of the current entity are visible in the right pane under the Variables tab and can be referenced using the syntax {property}. Functions can also be called within the action block using these properties and are also available in the right pane under the Functions tab. It is important to understand how to reference the entity variables within the action, as they are necessary for most functions and logical operations. Example — Pause Each CampaignPauseCampagn(id);
AdjustBid(id, bid * .75);
if(name == 'horse')
{
AdjustBid(id, bid * .75);
}
Persisting Values
At times you may wish to persist values across multiple actions. This can be accomplished using the SetCachedValue() and GetCachedValue() functions. The caching functions allow you to read and write to a global key value store for the duration of the rule. The key value store is not persisted between separate instances of the same rule. SetCachedValue() allows you to write a cached value to a key name of your choosing, while GetCachedValue() allows you to read the value from the provided key. Other caching functions, such as SumCachedValue() exist as a convenience and are not required but make operations on cached values easier. Example — Store campaign spend from the current campaign action under a key named “mySpend”SetCachedValue("mySpend",spend);
GetCachedValue("mySpend",0);
SumCachedValue("mySpend",spend);
One-Time Actions
These are lesser-used actions which do not execute within the scope of any individual entity but rather execute one time following a trigger. They are often used to perform a single piece of logic following multiple actions or multiple results from a trigger.Testing a Rule
You should test your rule frequently while building it to ensure it is functioning as expected. The Test Run button can be used to accomplish this and may be safely clicked as often as you like (as long as your syntax is valid). When the Test Run button is clicked, the rule will run against your current snapshot of ASA data and a summary of what would have happened will be displayed. No ASA functions will be called and no updates to your ASA integration will be made when using the Test Run functionality.
The Test Results window displays a detailed output of the steps which took place when the rule was run. The ‘output’ chunk of data within each step is the most important part of this information and lists the ASA integration actions which would have taken place.
Disabling a Rule
Scripted rules continue to run at the desired frequency until disabled. You must determine when a rule should no longer run by calling the DisableRule() function within an action block. If you desire a rule to run only once when successful, a one-time action should be added to the end of your rule which calls DisableRule(). If you desire that a rule be disabled only under certain conditions, create an appropriate action which calls DisableRule() gated behind the required conditions. In this case the rule will continue to run until the conditions are met.Rule Scheduling
A variety of additional settings exist below the action and trigger blocks which allow you to specify how often and when the rule runs. Significant consideration should be given to the frequency at which scripted rules are scheduled, as they will continue to run at the desired schedule until stopped manually or disabled within an action block. A rule that reduces keyword bids, for example, could continue to reduce the keyword bid each time it runs. Give plenty of thought to how often a scripted rule should run and be sure to consider when and if the DisableRule() function is called. Once your rule is behaving as expected and additional settings have been provided, click the Save (or update) button to finalize and save the rule. Keep in mind that you can return and edit the rule any time.