Reusing Tag Definitions

You can define your tags separately from your filters. For example, you can define a set of tags once and then reuse them in multiple filters, or use several different sets of tags in one filter.

Including tags in a new filter

To include a set of tags in a filter:

  1. Create standalone tag definitions using the tag keyword.
  2. Compile the tags.
  3. Make a note of the hash for that definition.
  4. Write your filter.
  5. Use the tags keyword to Include them in the CSDL code that defines a filter.
  6. Compile the filter, either in DataSift's user interface or via an API call to the /compile endpoint.

Here's an example of a tags definition:

tag.device.model "iPhone"     { interaction.source contains "iPhone" }
tag.device.model "Blackberry" { interaction.source contains "Blackberry" }

There is no return statement here and no filtering logic. These tags are self-contained and they will work with any filter.

The hash for this tag definition is a8979ef1255a7d511998553f2f22819d and we can include it in another filter using the tags keyword.

tags "a8979ef1255a7d511998553f2f22819d"

return { 
  interaction.content contains "Venice" OR links.title contains "Venice"
}

The tags keyword has to appear before the return statement.

If you change the tag definition in any way, its hash will change. At that point, you can either continue to use the old hash, and hence the old definition of the tags, or use our new tag definitions by updating the hash in the tags command:

tags "15068d2476bedd162530702700d63547"

Make sure that any definitions you include using the tags keyword contain nothing but tag definitions; you cannot use this keyword to include code that has a filter definition.

Including tags into a filter that already has tags

You can use the tags keyword to include tags in a filter than already has tags defined locally. DataSift applies both sets of tags to the interactions.

tag.device.model "iPhone"     { interaction.source contains "iPhone" }
tag.device.model "iPad"       { interaction.source contains "iPad" }
tag.device.model "Blackberry" { interaction.source contains "Blackberry" }

// Import tag definition
tags "9676453865e27de008a1d874d483bc46"

// Additional tags
tag.device.format "Mobile" { interaction.source contains_any "iPhone,Blackberry" }
tag.device.format "Tablet" { interaction.source contains "iPad" }

// Filter
return { 
  interaction.content contains "Venice" OR links.title contains "Venice"
}

Including tags in multiple filters

You can include a tag definition in any number of filters.

tag.model "iPhone"     { interaction.source contains "iPhone" }
tag.model "iPad"       { interaction.source contains "iPad" }
tag.model "Blackberry" { interaction.source contains "Blackberry" }

You can include the tags here:

// Import tag definition
tags.device "55a1eeab06858259a401bfc16b7771ce"

// Filter
return { 
  interaction.content contains "Venice" OR links.title contains "Venice"
}

You can use them here too:

// Import tag definition
tags.device "55a1eeab06858259a401bfc16b7771ce"

// Additional tags
tag.device.format "Mobile" { interaction.source contains_any "iPhone,Blackberry" }
tag.device.format "Tablet" { interaction.source contains "iPad" }

// Filter
return { 
  interaction.content contains "Venice" OR links.title contains "Venice"
}

Notice that this filter includes external tag definitions and also has local tag definitions. If you need to do this, include the external tags before the local tag definitions.

Including multiple sets of tag definitions

You can include more than one set of tag definitions; for example, if you define these tags:

tag.model "iPhone"     { interaction.source contains "iPhone" }
tag.model "iPad"       { interaction.source contains "iPad" }
tag.model "Blackberry" { interaction.source contains "Blackberry" }

And these tags:

tag.device.format "Mobile" { interaction.source contains_any "iPhone,Blackberry" }
tag.device.format "Tablet" { interaction.source contains "iPad" }

You can include both sets of definitions in one filter:

// Import tags from definition 1
tags "55a1eeab06858259a401bfc16b7771ce"

// Import tags from definition 2
tags "2ff3a1745c92503d1a534228856ba4e4"

// An additional tag
tag.device.model "Android" { interaction.source contains "Android" }

// Filter
return { 
  interaction.content contains "Venice" OR links.title contains "Venice"
}