Adding Namespaces

Tags can belong to namespaces, which are analogous to folders in a file system: they provide a way for you to add structure and grouping, they can fit inside each other as many levels deep as you want, and you can call them whatever you choose.

Here's an example that does not have any namespaces yet:

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

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

You can modify it like this to place all the tags in a namespace called "device":

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

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

If an input object looks like this:

{
    "interaction": {
        "content": "Check out my holiday pics, Venice was beautiful!",
        "source": "Twitter for iPhone"
    }
}

The output will look like this:

{
    "interaction": {
        "content": "Check out my holiday pics, Venice was beautiful!",
        "source": "Twitter for iPhone",
        "tag_tree": {
            "device": [
                "iPhone"
            ]
        }
    }
}

Valid characters for namespaces

When you define namespaces you can use the following valid characters:

  • latin letters excluding accents
  • hyphens
  • underscores

For example the following namespaced tags are valid:

tag.restaurant.café "café" { fb.all.content any "café" } 
tag.restaurant.coffee "Peets Coffee" { fb.all.content any "Peets, Peet's" } 
tag.clothing.levis "501" { fb.all.content any "Levis 501" }

Whereas the following tags are invalid:

tag.restaurant.peets coffee "coffee" { fb.all.content any "Peets, Peet's" } 
tag.levis.501 "501 Jeans" { fb.all.content any "Levis 501" }

The first fails because there is a space in the namespace which is not allowed. The second fails because there are digits in the namespace.

Using multiple levels of namespacing

You can use as many levels of namespace as you want.

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

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

For this input:

{
    "interaction": {
        "content": "Check out my holiday pics, Venice was beautiful!",
        "source": "Twitter for iPhone"
    }
}

The output will be:

{
    "interaction": {
        "content": "Check out my holiday pics, Venice was beautiful!",
        "source": "Twitter for iPhone",
        "tag_tree": {
            "device": {
                "model": [
                    "iPhone"
                ]
            }
        }
    }
}

Adding a namespace when you include tags

The previous example showed how to specify a namespace at the time you defined tags. You can also add a namespace to a set of tag definitions when you include them in a filter. This tag definition does not have any namespaces yet:

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

The hash of this tag definition is 7fe07f1488f9bfb0c251a1a79a4ec6fa. To add these predefined tags to a namespace called "device.model" at the time you include the tag definitions:

tags.device.model "7fe07f1488f9bfb0c251a1a79a4ec6fa"

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

It does not matter whether we add it at the time we define the tags or when we include them. The resulting JSON is the same.

{
    "interaction": {
        "content": "Check out my holiday pics, Venice was beautiful!",
        "source": "Twitter for iPhone",
        "tag_tree": {
            "device": {
                "model": [
                    "iPhone"
                ]
            }
        }
    }
}

Adding a namespace to imported tags that already have namespaces

This tag definition belongs to a namespace called "model":

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

You can include it in a namespace called "device" this this:

tags.device "202ef449bc2d9043121151d1db745347"

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

The JSON output combines both namespaces. DataSift places tags into the "model" namespace and places that in the "device" namespace. Again the output is:

{
    "interaction": {
        "content": "Check out my holiday pics, Venice was beautiful!",
        "source": "Twitter for iPhone",
        "tag_tree": {
            "device": {
                "model": [
                    "iPhone"
                ]
            }
        }
    }
}

Note the order of the namespacing here: tag.device.model.

In other words, it's just as if you had defined your original tags like this:

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